介绍
这篇文章主要介绍了hadoop中如何实现GenericWritable,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获、下面让小编带着大家一起了解一下。
package com.test; import java.io.IOException; import java.util.Iterator; import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configured; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.GenericWritable; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.io.Writable; 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.KeyValueTextInputFormat; import org.apache.hadoop.mapreduce.lib.input.MultipleInputs; import org.apache.hadoop.mapreduce.lib.input.TextInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat; import org.apache.hadoop.util.Tool; import org.apache.hadoop.util.ToolRunner;/* * ,*业务场景: ,*含有两个文件,两个文件中单词之间的分隔方式不一样,但是统计出单词在两个文件中公共出现的次数 *大敌; 1,*文件来源,逗号分隔text1.txt ,*,你好,什么 ,*你,哈哈 2,*文件来源,制表符分隔text2.txt ,* girl 男孩 ,* father 母亲 ,*/public class  WordCountGenericWritable extends Configured  implements Tool { , ,public static  class Map1  extends Mapper, { public 才能;void 地图(LongWritable 关键,Text 价值,Context 上下文),throws IOException, InterruptedException { ,,String line =, value.toString (); ,, ,,StringTokenizer st =, new StringTokenizer (,,,,,); ,,,(st.hasMoreElements ()), { ,,,context.write (new 文本(st.nextElement () .toString ()),, new MyGenericWritable (new LongWritable (1))); ,,} ,,} ,} , ,public static  class Map2  extends Mapper<文本,文本,文本,MyGenericWritable>, { public 才能;void 地图(Text 关键,Text 价值,Context 上下文),throws IOException, InterruptedException { ,,context.write(关键,new MyGenericWritable (new 文本(“1“))); ,,context.write(价值,new MyGenericWritable (new 文本(“1“))); ,,} ,} , ,public static  class Reduce  extends Reducer<文本,MyGenericWritable,,文本,IntWritable>, { public 才能;void 减少(Text 关键,Iterable ,价值观,Context 上下文),throws IOException, InterruptedException { ,,int count =, 0; ,,Iterator hadoop中如何实现GenericWritable