我是靠谱客的博主 甜美水杯,最近开发中收集的这篇文章主要介绍hadoop-11 自定义OutputFormat步骤Demo :数据源,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

步骤

仿照TextOuputtFormat自定义OutputFormat。实现:根据输入源的某个非key字段输出到不同的路径。

1.自定义类继承FileOutputFormat

  1. 添加静态类MyRecordWriter()继承RecordWriter类,参数为reducermapper的输出k,v
    1. 添加构造方法,参数为FSDataOutputStream
      (conf->Filesystem->FSDataOutputStream)
    2. 覆写write()
    3. 覆写close()

3.覆写getRecordWriter(),返回自定义的MyRecordWriter()

Demo :

//main
public class Main extends Configured implements Tool {
    @Override
    public int run(String[] args) throws Exception {
        Job job = Job.getInstance(super.getConf());

        job.setInputFormatClass(TextInputFormat.class);
        TextInputFormat.addInputPath(job,new Path("file:///F:\BigData\project\hadoop\data\outputformat\input"));

        job.setMapperClass(MyMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(NullWritable.class);


        //设置一个输出目录,这个目录会输出一个success的成功标志的文件
        job.setOutputFormatClass(MyOutPutForamt.class);
        MyOutPutForamt.setOutputPath(job,new Path("file:///F:\BigData\project\hadoop\data\outputformat\out"));

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);


        boolean b = job.waitForCompletion(true);
        return b?0:1;
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        int run = ToolRunner.run(conf, new Main(), args);
        System.exit(run);
    }
}
//mapper
public class MyMapper extends Mapper<LongWritable,Text,Text,NullWritable> {
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        context.write(value,NullWritable.get());
    }
}
//MyOutputFormat
public class MyOutPutForamt extends FileOutputFormat<Text,NullWritable> {

    @Override
    public RecordWriter getRecordWriter(TaskAttemptContext taskAttemptContext) throws IOException, InterruptedException {
        Configuration conf = taskAttemptContext.getConfiguration();
        FileSystem fileSystem = FileSystem.get(conf);
        Path goodCommentPath = new Path("F:\BigData\project\hadoop\data\outputformat\out\goodComment");
        Path badCommentPath = new Path("F:\BigData\project\hadoop\data\outputformat\out\badComment");

        FSDataOutputStream fsOSGood = fileSystem.create(goodCommentPath);
        FSDataOutputStream fsOSBad = fileSystem.create(badCommentPath);


        return new MyRecordWriter(fsOSGood,fsOSBad);
    }

    protected static class MyRecordWriter <Text,NullWritable> extends RecordWriter<Text,NullWritable>{
        private FSDataOutputStream goodStream = null;
        private FSDataOutputStream badSteam = null;

        public MyRecordWriter(FSDataOutputStream goodStream, FSDataOutputStream badSteam) {
            this.goodStream = goodStream;
            this.badSteam = badSteam;
        }

        @Override
        public void write(Text key, NullWritable value) throws IOException, InterruptedException {
            if (key.toString().split("t")[9].equals("0")){
                goodStream.write(key.toString().getBytes());
                goodStream.write("rn".getBytes());
            }
            else {
                badSteam.write(key.toString().getBytes());
                badSteam.write("tn".getBytes());
            }

        }

        @Override
        public void close(TaskAttemptContext taskAttemptContext) throws IOException, InterruptedException {
            if (goodStream != null){
                goodStream.close();
            }
            if (badSteam != null){
                badSteam.close();
            }


        }
    }
}

数据源

1	2018-03-15 22:29:06	2018-03-15 22:29:06	我想再来一个	N	1	3	hello	来就来吧	0	2018-03-14 22:29:03
2	2018-03-15 22:42:08	2018-03-15 22:42:08	好的	N	1	1	添加一个吧	说走咱就走啊	0	2018-03-14 22:42:04
3	2018-03-15 22:55:21	2018-03-15 22:55:21	haobuhao	N	1	1	nihao		0	2018-03-24 22:55:17
4	2018-03-23 11:15:28	2018-03-23 11:15:28	店家很好 非常好	N	1	3	666	谢谢	0	2018-03-23 11:15:20
5	2018-03-23 14:52:48	2018-03-23 14:53:22	a'da'd	N	0	4	da'd's	打打操作	0	2018-03-22 14:52:42
6	2018-03-23 14:53:52	2018-03-23 14:53:52	达到	N	1	4	1313	13132131	0	2018-03-07 14:30:38
7	2018-03-23 14:54:29	2018-03-23 14:54:29	321313	N	1	4	111	1231231	1	2018-03-06 14:54:24
8	2018-03-23 14:54:48	2018-03-29 03:00:12	1	N	1	4	1	110000	0	2018-03-23 14:54:42
9	2018-03-29 02:58:34	2018-03-29 02:58:34	666666666666666	N	1	4	4444	7777777777777777	0	2018-03-01 10:58:24
10	2018-03-29 02:58:54	2018-03-29 02:58:54	00000000	N	1	4	55555555555	009999999	1	2018-03-02 10:58:47
11	2018-03-29 02:59:17	2018-03-29 02:59:42	311111111111	N	0	4	999999	1333333333	2	2018-03-03 10:59:10
12	2018-04-04 02:26:32	2018-04-04 02:26:32	好	N	1	91	131******12		0	2017-12-02 23:26:15
13	2018-04-04 02:32:15	2018-04-04 02:32:15	跟当地超市买的一样,攒	N	1	91	171******54	本店原装进口,谢谢惠顾	0	2018-01-09 10:30:39
14	2018-04-04 02:33:34	2018-04-04 02:33:34	做出来非常好吃,女儿非常喜欢,冬天里别样的风味	N	1	91	180******33		0	2018-02-07 10:33:03
15	2018-04-04 02:34:42	2018-04-04 02:34:42	非常好,发货很快	N	1	91	156******62		0	2018-03-21 10:34:34
16	2018-04-04 02:42:10	2018-04-04 02:42:10	 很棒很棒,很喜欢的商品!	N	1	225	170*******87		0	2018-02-05 10:42:01
17	2018-04-04 02:42:36	2018-04-04 02:42:36	好	N	1	225	135*******11		0	2018-02-01 10:42:31
18	2018-04-14 08:14:05	2018-04-14 08:14:05	价格很实惠	N	1	532	138******11		0	2018-02-06 16:13:54
19	2018-04-14 08:15:41	2018-04-14 08:15:41	性价比很高	N	1	532	170******09		0	2018-02-15 08:00:30
20	2018-04-14 08:17:08	2018-04-14 08:17:08	算下来价格十分优惠,赞	N	1	532	130******34		0	2018-03-15 19:55:40
21	2018-04-14 09:44:12	2018-04-14 09:44:12	物有所值!	N	1	531	158******00		0	2018-02-15 08:43:55
22	2018-04-14 09:46:14	2018-04-14 09:46:14	这次旅程让我们在悉尼不仅欣赏到了美丽的风景,沿途还看见了悉尼歌剧院和悉尼海港大桥,同时品尝到各种美食,总体是非常愉快的旅程	N	1	531	131******11		0	2018-03-08 10:05:59
23	2018-04-14 09:47:20	2018-04-14 09:47:20	一路上风景宜人,是一次值得回忆的旅游。尤其是导游,工作非常认真负责,为大家忙前忙后,任劳任怨,值得表扬。	N	1	531	138******11		0	2018-02-15 15:30:10
24	2018-04-14 09:52:19	2018-04-14 09:52:19	这次旅行由墨尔本的长城旅行社提供服务,行程安排合理,景色优美。赞。	N	1	529	130******45		0	2018-02-08 17:52:11
25	2018-04-14 09:53:30	2018-04-14 09:53:30	非常完美的行程,导游李先生是东北人,热情,负责,有耐心,知识面广,一路上给大家讲了很多!又没有购物环节,超赞! 一路行程内容丰富,景色迷人,住的酒店也非常好,近邻海滩,晚上可以散步,早上可以看日出! 景色超美! 点赞~~~	N	1	529	150******92		0	2018-03-23 14:05:03
26	2018-04-14 09:55:14	2018-04-14 09:55:14	很经典的大洋路旅游,行程不会累,基本上把大洋路上景点都走了一遍,比自驾游还好,性价比高,导游兼司机的Jackey非常耐心,脾气好,有问必答。之前询问其他旅行社都不同意当天到达墨尔本就当天参团,事实证明是可以的,感谢途风的客服,耐心讲解和安排,也非常感谢Jackey等待了我们由于塞车的迟到,令这个行程非常完美难忘。	N	1	529	139******73		0	2018-04-01 21:55:03

最后

以上就是甜美水杯为你收集整理的hadoop-11 自定义OutputFormat步骤Demo :数据源的全部内容,希望文章能够帮你解决hadoop-11 自定义OutputFormat步骤Demo :数据源所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(63)

评论列表共有 0 条评论

立即
投稿
返回
顶部