MapReduce基础开发之一词汇统计和排序(wordcount)
生活随笔
收集整理的這篇文章主要介紹了
MapReduce基础开发之一词汇统计和排序(wordcount)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
統計/var/log/boot.log中含k的字符的數量,并對含k的字符按照數量排序。需分兩個job完成,一個用來統計,一個用來排序。
一、統計
1、上傳文件到hadoop:
? ?1)新建文件夾:hadoop fs -mkdir /tmp/fjs
? ?2)上傳文件:hadoop fs -put /var/log/boot.log /tmp/fjs
2、編寫wordcount代碼并導出jar和上傳到namenode
? ?1)掛載共享文件夾,上傳jar包:mount -t cifs //ip/tmp /mnt -o username=xxx,password=xxx
? ?2)移動jar包到tmp目錄下:cp -R /mnt/wordcount.jar /tmp
? ?3)jar包是root權限,更改給hadoop用戶:chown -R hdfs:hdfs /tmp/wordcount.jar
3、執行wordcount.jar并查看結果
? ?1)執行:yarn jar /tmp/wordcount.jar /tmp/fjs /tmp/fjs/out
? ?2)查看:hadoop fs -text /tmp/fjs/out/part-r-0000.bz2 ?
二、排序
1、編寫wordsort代碼并導出jar和上傳namenode,對wordcount執行的結果進行排序;
? ?排序就是利用mapreduce本身的key排序功能,主要是互換key和value。
? ?代碼如下:
package com; import java.io.IOException; import java.util.StringTokenizer;import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.util.GenericOptionsParser;public class WordSort {public static class SortIntValueMapper extends Mapper<LongWritable, Text, IntWritable, Text>{private final static IntWritable wordCount = new IntWritable(1);private Text word = new Text();public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {StringTokenizer tokenizer = new StringTokenizer(value.toString());while (tokenizer.hasMoreTokens()) {word.set(tokenizer.nextToken().trim());wordCount.set(Integer.valueOf(tokenizer.nextToken().trim()));context.write(wordCount, word);//<k,v>互換}}}public static class SortIntValueReduce extends Reducer<IntWritable, Text, Text, IntWritable> {private Text result = new Text();public void reduce(IntWritable key, Iterable<Text> values, Context context) throws IOException, InterruptedException {for (Text val : values) {result.set(val.toString());context.write(result, key);//<k,v>互換}}}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: wordsort <in> <out>");System.exit(2);}Job job = new Job(conf, "word sort");job.setJarByClass(WordSort.class);job.setMapperClass(SortIntValueMapper.class);job.setReducerClass(SortIntValueReduce.class);job.setOutputKeyClass(IntWritable.class);job.setOutputValueClass(Text.class);FileInputFormat.addInputPath(job, new Path(otherArgs[0]));FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));System.exit(job.waitForCompletion(true) ? 0 : 1);} }
2、執行wordsort.jar并查看結果
? ?1)執行:yarn jar /tmp/wordsort.jar /tmp/fjs/out /tmp/fjs/out1
? ?2)查看:hadoop fs -text /tmp/fjs/out1/part-r-0001.bz2
一、統計
1、上傳文件到hadoop:
? ?1)新建文件夾:hadoop fs -mkdir /tmp/fjs
? ?2)上傳文件:hadoop fs -put /var/log/boot.log /tmp/fjs
2、編寫wordcount代碼并導出jar和上傳到namenode
? ?1)掛載共享文件夾,上傳jar包:mount -t cifs //ip/tmp /mnt -o username=xxx,password=xxx
? ?2)移動jar包到tmp目錄下:cp -R /mnt/wordcount.jar /tmp
? ?3)jar包是root權限,更改給hadoop用戶:chown -R hdfs:hdfs /tmp/wordcount.jar
? ?代碼如下:
?
package com;import java.io.IOException; import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.util.GenericOptionsParser;public class WordCount {public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>{private final static IntWritable one = new IntWritable(1);private Text word = new Text();public void map(Object key, Text value, Context context)throws IOException, InterruptedException {StringTokenizer itr = new StringTokenizer(value.toString());while (itr.hasMoreTokens()) {String strVal=itr.nextToken();//獲取字符//if(strVal.contains("k")){//如果字符包含k,則統計word.set(strVal);context.write(word, one);//}}}}public static class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable> {private IntWritable result = new IntWritable();public void reduce(Text key, Iterable<IntWritable> values,Context context) throws IOException, InterruptedException {int sum = 0;for (IntWritable val : values) {sum += val.get();}result.set(sum);context.write(key, result);}}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);}Job job = new Job(conf, "word count");job.setJarByClass(WordCount.class);job.setMapperClass(TokenizerMapper.class);job.setCombinerClass(IntSumReducer.class);job.setReducerClass(IntSumReducer.class);job.setOutputKeyClass(Text.class);job.setOutputValueClass(IntWritable.class);FileInputFormat.addInputPath(job, new Path(otherArgs[0]));FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));System.exit(job.waitForCompletion(true) ? 0 : 1);}}3、執行wordcount.jar并查看結果
? ?1)執行:yarn jar /tmp/wordcount.jar /tmp/fjs /tmp/fjs/out
? ?2)查看:hadoop fs -text /tmp/fjs/out/part-r-0000.bz2 ?
二、排序
1、編寫wordsort代碼并導出jar和上傳namenode,對wordcount執行的結果進行排序;
? ?排序就是利用mapreduce本身的key排序功能,主要是互換key和value。
? ?代碼如下:
package com; import java.io.IOException; import java.util.StringTokenizer;import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.util.GenericOptionsParser;public class WordSort {public static class SortIntValueMapper extends Mapper<LongWritable, Text, IntWritable, Text>{private final static IntWritable wordCount = new IntWritable(1);private Text word = new Text();public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {StringTokenizer tokenizer = new StringTokenizer(value.toString());while (tokenizer.hasMoreTokens()) {word.set(tokenizer.nextToken().trim());wordCount.set(Integer.valueOf(tokenizer.nextToken().trim()));context.write(wordCount, word);//<k,v>互換}}}public static class SortIntValueReduce extends Reducer<IntWritable, Text, Text, IntWritable> {private Text result = new Text();public void reduce(IntWritable key, Iterable<Text> values, Context context) throws IOException, InterruptedException {for (Text val : values) {result.set(val.toString());context.write(result, key);//<k,v>互換}}}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: wordsort <in> <out>");System.exit(2);}Job job = new Job(conf, "word sort");job.setJarByClass(WordSort.class);job.setMapperClass(SortIntValueMapper.class);job.setReducerClass(SortIntValueReduce.class);job.setOutputKeyClass(IntWritable.class);job.setOutputValueClass(Text.class);FileInputFormat.addInputPath(job, new Path(otherArgs[0]));FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));System.exit(job.waitForCompletion(true) ? 0 : 1);} }
2、執行wordsort.jar并查看結果
? ?1)執行:yarn jar /tmp/wordsort.jar /tmp/fjs/out /tmp/fjs/out1
? ?2)查看:hadoop fs -text /tmp/fjs/out1/part-r-0001.bz2
總結
以上是生活随笔為你收集整理的MapReduce基础开发之一词汇统计和排序(wordcount)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 算法导论之数据结构
- 下一篇: MapReduce基础开发之二数据去重和