5.map_reduce简单例子与任务运行命令


  1. wordcount例子
  2. public class WordCount {  
  3.   
  4.    public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {  
  5.      private final static IntWritable one = new IntWritable(1);  
  6.      private Text word = new Text();  
  7.   
  8.      public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {  
  9.        String line = valuetoString();  
  10.        StringTokenizer tokenizer = new StringTokenizer(line);  
  11.        while (tokenizerhasMoreTokens()) {  
  12.          wordset(tokenizernextToken());  
  13.          outputcollect(word, one);  
  14.        }  
  15.      }  
  16.    }  
  17.   
  18.    public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> {  
  19.      public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {  
  20.        int sum = 0;  
  21.        while (valueshasNext()) {  
  22.          sum += valuesnext()get();  
  23.        }  
  24.        outputcollect(key, new IntWritable(sum));  
  25.      }  
  26.    }  
  27.   
  28.    public static void main(String[] args) throws Exception {  
  29.      JobConf conf = new JobConf(WordCountclass);  
  30.      confsetJobName("wordcount");  
  31.   
  32.      confsetOutputKeyClass(Textclass);  
  33.      confsetOutputValueClass(IntWritableclass);  
  34.   
  35.      confsetMapperClass(Mapclass);  
  36.      confsetCombinerClass(Reduceclass);  
  37.      confsetReducerClass(Reduceclass);  
  38.   
  39.      confsetInputFormat(TextInputFormatclass);  
  40.      confsetOutputFormat(TextOutputFormatclass);  
  41.   
  42.      FileInputFormatsetInputPaths(conf, new Path(args[0]));  
  43.      FileOutputFormatsetOutputPath(conf, new Path(args[1]));  
  44.   
  45.      JobClientrunJob(conf);  
  46.    }  
  47. }  


运行命令

:hadoop jar /usr/local/hadoop/hadoop-examples-1.1.2.jar wordcount dedup_in dedup_out

/usr/local/hadoop/hadoop-examples-1.1.2.jar 是jar包在linux系统的绝对路径


wordcount是类名,一般要写全路径

dedup_in   是输入文件的路径,是hdfs上的绝对路径。

dedup_out  是输出结果文件的路径,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值