hadoop 之 Map数据处理(Reduce不参与)

来源:互联网 发布:linux java 短信猫 编辑:程序博客网 时间:2024/06/08 16:38

这几天刚刚接触了MR,发现其对大数据的处理是真的强大。不提执行速度,单单简单的API调用也是非常的容易上手。
不过在学习的过程中,遇到一个需求,就是对一堆数据中的学科编号映射成对应的中文。仔细想一想觉得该过程根本不需要用到Reduce的过程,只要在Map执行之前,调用setUp把学科编号和对应中文存储在HashMap中,然后再在Map对每一行的数据进行处理即可。记录一下该过程,方便以后回顾。

我自定义MapTestMapper来继承Mapper的类,来完成map过程。

/*第一个参数是Mapper的输入key,默认是一个文件的偏移量, *第二个参数是Mapper的输入value,默认就是一行行的读取。 *第三,四个参数是输出的key和value。 *我的输出类型是NullWritable,因为在map输出的时候,我不需要输出任何的value,只要把输入进来的key处理一下,再将输入key处理一下当初输出key写出去即可 */public static class MapTestMapper extends Mapper<Object, Text, Text, NullWritable>{        private Map<String,String> subjectMap =  new HashMap<>();//存放学科编号和中文的容器        private Text text = new Text();        @Override        protected void setup(Mapper<Object, Text, Text, NullWritable>.Context context)                throws IOException, InterruptedException {//会在map运行之前被调用            //数据的位置            String uri="hdfs://10.1.11.111:8020/user/mysql/subject_patent_map/part-m-00000";              Configuration configuration=new Configuration();              FileSystem fileSystem=FileSystem.get(URI.create(uri), configuration);            //数据的读入操作            FSDataInputStream in=null;              in=fileSystem.open(new Path(uri));              InputStreamReader isr = new InputStreamReader(in,"utf-8");            BufferedReader br = new BufferedReader(isr);            String line;            //数据处理后放到subjectMap中            while((line = br.readLine()) != null){                String[] strs = line.split("'");                if(strs.length > 1){//防止缺少areacode的unit_areacode进入                    subjectMap.put(strs[0],strs[1]);                }            }        }        /**真正执行的代码我就不显示了*/        @Override        protected void map(Object key, Text value, Context context)                throws IOException, InterruptedException {            text.set(subjectMap.get("A61K36".substring(0, 3)));            context.write(text, NullWritable.get());        }    }

那么在设置job的时候,就需要表明我们的过程是不需要执行Reduce的。Job的输出就不再看Reduce了,而是要看Map所以Key是Text,value是NullWritable。

public static void main(String[] args) throws Exception {        Configuration conf = new Configuration(); // 启用默认配置        String[] otherArgs = new GenericOptionsParser(conf, args)                .getRemainingArgs();        if (otherArgs.length != 2) {            System.err.println("Usage: wordcount <in> <out>");            System.exit(2);        }        conf.set("mapred.jar","C:\\Users\\GEKL\\Desktop\\a.jar");        Job job = new Job(conf, "word count");// 定义一个job        job.setJarByClass(NewWordCount.class);// 设定执行类        job.setMapperClass(MapTestMapper.class);// 设定Mapper实现类        //job.setCombinerClass(IntSumReducer.class);// 设定Combiner实现类        //job.setReducerClass(IntSumReducer.class);// 设定Reducer实现类        job.setOutputKeyClass(Text.class);// 设定OutputKey实现类,Text.class是默认实现        job.setOutputValueClass(NullWritable.class);// 设定OutputValue实现类        FileInputFormat.addInputPath(job, new Path(otherArgs[0]));// 设定job输入文件夹        FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));// 设定job输出文件夹        System.exit(job.waitForCompletion(true) ? 0 : 1);    }
原创粉丝点击