1. 程式人生 > 實用技巧 >mapreduce程式開發簡單例項 WordCount

mapreduce程式開發簡單例項 WordCount

mapreduce的簡單程式設計已經學習得差不多了,抽時間總結下

  WordCount顧名思義,這個程式的作用就是數清一個文字中某關鍵詞的出現次數,通過mapreduce可以輕鬆實現。

首先輸入的文字如下:

  

然後目標就是統計各個賣家id 的出現次數

原理:

大致思路是將hdfs上的文字作為輸入,MapReduce通過InputFormat會將文字進行切片處理,並將每行的首字母相對於文字檔案的首地址的偏移量作為輸入鍵值對的key,文字內容作為輸入鍵值對的value,經過在map函式處理,輸出中間結果<word,1>的形式,並在reduce函式中完成對每個單詞的詞頻統計。整個程式程式碼主要包括兩部分:

Mapper部分和Reducer部分。

程式碼實現:

import java.io.IOException;
import java.util.StringTokenizer;
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;
public class mapreduce {
public static void main(String[] args) throws IOException,ClassNotFoundException,InterruptedException {
Job job = Job.getInstance();
job.setJobName("WordCount");
job.setJarByClass(mapreduce.class);
job.setMapperClass(doMapper.class);
job.setReducerClass(doReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
Path in = new Path("hdfs://192.168.146.131:9000/mymapreduce1/in/buyer_favorite1");
Path out = new Path("hdfs://192.168.146.131:9000/mymapreduce1/out");
FileInputFormat.addInputPath(job,in);
FileOutputFormat.setOutputPath(job,out);
System.exit(job.waitForCompletion(true)?0:1);

}
public static class doMapper extends Mapper<Object,Text,Text,IntWritable>{
public static final IntWritable one = new IntWritable(1);
public static Text word = new Text();
@Override
protected void map(Object key, Text value, Context context)
throws IOException,InterruptedException {
StringTokenizer tokenizer = new StringTokenizer(value.toString(),"  ");
word.set(tokenizer.nextToken());
context.write(word,one);
}
}
public static class doReducer extends Reducer<Text,IntWritable,Text,IntWritable>{
private IntWritable result = new IntWritable();
@Override
protected void reduce(Text key,Iterable<IntWritable> values,Context context)
throws IOException,InterruptedException{
int sum = 0;
for (IntWritable value : values){
sum += value.get();//彙總各個關鍵字數目,將每個key的values中所有值相加
}
result.set(sum);
context.write(key,result);
}
}
}

  

最終到hdfs的輸出目錄(本例是/mymapreduce1/out)中檢視輸出的檔案part-r-00000

可得到