MR案例:WordCount改寫

來源:互聯網
上載者:User

標籤:

請參照wordcount實現一個自己的MapReduce,需求為:    a. 輸入檔案格式:       xxx,xxx,xxx,xxx,xxx,xxx,xxx    b. 輸出檔案格式:       xxx,20       xxx,30       xxx.40    c. 功能:根據命令列參數統計輸入檔案中指定關鍵字出現的次數,並展示出來       例如:hadoop jar xxxxx.jar keywordcount xxx,xxx,xxx,xxx(四個關鍵字)
package demo0830;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.input.TextInputFormat;import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;import java.io.IOException;import java.util.ArrayList;public class Demo0902 {    public static void main(String[] args) throws Exception {        Configuration conf = new Configuration();        if (args.length < 3) {            System.out.println("Usage: wordcount <input_path> <output_path> <keyword_list>");            return;        }        //Add to target(靜態方法)        String[] target_words = args[2].split(",");        for (String word : target_words) {            WCMap.addTargetWord(word.toLowerCase());        }        Job job = Job.getInstance(conf);        job.setJarByClass(Demo0902.class);        job.setOutputKeyClass(Text.class);        job.setOutputValueClass(IntWritable.class);        job.setMapperClass(WCMap.class);        job.setReducerClass(WCReduce.class);        job.setInputFormatClass(TextInputFormat.class);        job.setOutputFormatClass(TextOutputFormat.class);        FileInputFormat.addInputPath(job, new Path(args[0]));        FileOutputFormat.setOutputPath(job, new Path(args[1]));        job.waitForCompletion(true);    }    public static class WCMap extends Mapper<LongWritable, Text, Text, IntWritable> {        private final static IntWritable one = new IntWritable(1);        private Text word = new Text();        private final static ArrayList<String> target_words = new ArrayList<String>();        public static void addTargetWord(String word) {            target_words.add(word);        }        public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {            String[] items = value.toString().toLowerCase().split(" ");            for (String item : items) {                                //filter keyword                if (target_words.contains(item)) {                    word.set(item);                    context.write(word, one);                }            }        }    }    public static class WCReduce extends Reducer<Text, IntWritable, Text, IntWritable> {        public void reduce(Text key, Iterable<IntWritable> values, Context context)                throws IOException, InterruptedException {            int sum = 0;            for (IntWritable val : values) {                sum += val.get();            }            context.write(key, new IntWritable(sum));        }    }}

 

MR案例:WordCount改寫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.