• 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);
        }
    }
    

      

  • 相关阅读:
    android ListView加载不同布局
    实例演示如何在spring4.2.2中集成hibernate5.0.2并创建sessionFactory
    【翻译】Ext JS最新技巧——2015-10-21
    Android Studio下使用NDK的流程
    Android Studio JNI javah遇到的问题
    题解报告:hdu 1062 Text Reverse
    题解报告:hdu 1039 Easier Done Than Said?
    ACM_逆序数(归并排序)
    hdu 1556 Color the ball(区间修改,单点查询)
    hdu 1754 I Hate It(线段树)
  • 原文地址:https://www.cnblogs.com/zhangjxblog/p/6636520.html
Copyright © 2020-2023  润新知