MapReduce - 来自输入和数组的多个输出

时间:2017-04-15 07:14:16

标签: java hadoop mapreduce

目标

我想从一个输入文件和一个数组中获得多个输出文件,如picture所述。

我的想法

我考虑过一个名为" check"的静态属性。对于父类Program

public class Program
{
    //Attribute check
    private static String check = null;

    public static class ProgramMapper extends Reducer<Object, Text, Text, Text>{ 
     // mapping 
    }
    public static class ProgramReducer extends Reducer<Object, Text, Text, TextArray>{ 
     // reducing
    }

    public static void main(String[] args){
     // main program
    }
// ...

main方法中,我会将check分配给&#34; a&#34;,&#34; b&#34;,&#34; c&#34;在循环中:

    public static void main(String[] args) throws Exception{

     // Array of checkpoints
        String[] arr = {"a", "c", "f"};

     // Loop for assigning check
        for(int j = 0; j<arr.length ; j++){
            check = arr[j];

        // job configuration
            Configuration conf = new Configuration();
            String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
            Job job = new Job(conf, "Program");
            //...
            for (int i = 0; i < otherArgs.length - 1; ++i){
                    FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
            } 

      /* here I define multiple outputs */
            FileOutputFormat.setOutputPath(job,new Path(otherArgs[otherArgs.length - 1]+j));
            job.waitForCompletion(true);
            if (j == arr.length -1){
            System.exit(job.waitForCompletion(true) ? 0 : 1);
      }

reducer将检查密钥是否等于检查

public static class ProgramReducer extends Reducer<Object, Text, Text, TextArray>{ 

  public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {

        ArrayList<Text> result = new ArrayList<Text>();
        String key1 = key.toString();

     // check if the key is equal to check
        if ( key1.equals(check) ){
           result.add(new Text("o"));
        }else{
           result.add(new Text("x"));
        }

     // other reducing code
   }
}

问题

check永远不会分配给&#34; a&#34;,&#34; b&#34;,&#34; c&#34;,所以我有3个输出文件都未经检查。

我怎么能解决这个问题?

1 个答案:

答案 0 :(得分:1)

您的main方法正在客户端上运行,但mappers和reducer正在Hadoop节点上运行。要将参数传递给mapreduce作业,您可以使用Configuration对象。

main方法设定值中:

conf.set("check", check);

在减少中得到它:

check = context.getConfiguration().get("check");

在处理数据之前,您只能使用方法Reducer.setup设置此值。

相关问题