`
nepshi
  • 浏览: 49370 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Hadoop基本的Map/Reduce

阅读更多

HelloHadoop.class

 

Mapper

static public class HelloMapper extends Mapper<LongWritable, Text, LongWritable, Text> {
    public void map(LongWritable key, Text value, Context context) {
        context.write((LongWritable)key, (Text)value);
    }
}

<LongWritable, Text, LongWritable, Text>中的第一个LongWritable和第一个Text为Mapper的输入数据类型,第二个LongWritable和Text为Mapper的输出数据类型。

 

Reducer

static public class HelloReducer extends Reducer<LongWritable, Text, L ongWritable, Text> {
    public void reduce(LongWritable key, Iterable<Text> values, Context context) {
        Text val = new Text();
        for (Text str : values) {
            val.set(str.toString());
        }
        context.write(key, val);
    }
}

<LongWritable, Text, LongWritable, Text>中的第一个LongWritable和第一个Text为Reducer的输入数据类型,第二个LongWritable和Text为Reducer的输出数据类型。

 

Main

public static void main(String[] args) {
    Configuration conf = new Configuration();
    Job job = new Job(conf, "Hadoop Hello World");
    job.setJarByClass(HelloHadoop.class);
    FileInputFormat.setInputPaths(job, "input");
    FileOutputFormat.setOutputPath(job, new Path("output"));
    job.setMapperClass(HelloMapper.class);
    job.setReducerClass(HelloReducer.class);
    job.waitForCompletion(true);
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics