hadoop参数传递实例

要求:

根据输入文件中的信息,计算出某几个字符串出现的个数

输入文件格式:xxx,xxx,xxx,xx,x,x,xxx,x,x,xx,x,x,x,x,x,x,x,

输出文件:xx    10

     xx    4

               .....

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.output.FileOutputFormat;

import java.io.IOException;

/**
 * Created by hadoop on 17-3-28.
 */
public class main {

    public static class mapper extends Mapper<LongWritable,Text,Text,IntWritable>{
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String str = value.toString();
            String[] strs = str.split(",");

            String s1 = context.getConfiguration().get("flag1");
            String s2 = context.getConfiguration().get("flag2");
            String s3 = context.getConfiguration().get("flag3");
            String s4 = context.getConfiguration().get("flag4");

            for(String s:strs){
                if(s.equals(s1) || s.equals(s2) || s.equals(s3) || s.equals(s4)){
                    context.write(new Text(s),new IntWritable(1));
                }
            }

        }
    }

    public static class reducer extends Reducer<Text,IntWritable,Text,IntWritable>{
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            int sum = 0;
            for (IntWritable v:values){
                sum += v.get();
            }

            context.write(key,new IntWritable(sum));
        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

        Configuration conf = new Configuration();
        conf.set("flag1",args[2]);
        conf.set("flag2",args[3]);
        conf.set("flag3",args[4]);
        conf.set("flag4",args[5]);

        Job job = Job.getInstance(conf);

        job.setJarByClass(main.class);
        job.setMapperClass(mapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        job.setReducerClass(reducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        FileInputFormat.addInputPath(job,new Path(args[0]));
        FileOutputFormat.setOutputPath(job,new Path(args[1]));

        boolean b = job.waitForCompletion(true);
        System.exit(b?0:1);
    }
}

  

原文地址:https://www.cnblogs.com/zhangjxblog/p/6636520.html