0

在 map-reduce 程序中,我希望每个映射器生成一个唯一的数字(与其他映射器中的数字不同)。我认为这可以使用用户定义的计数器来完成。但是,正如我从计数器中了解到的那样,当映射器完成时,计数器的值会发送到任务跟踪器。如果这是真的,我有点困惑如何在映射器中生成唯一编号。

4

1 回答 1

0

为什么不在每个映射器中使用通用唯一标识符 (UUID)?

请检查这个链接,Java 有这个内置的。

评论后编辑:

如果您希望在拆分中的所有记录中只生成一次 UUID,您可以覆盖 Mapper 类的 setup 方法,该方法在 map 任务开始时仅调用一次。然后可以将生成的 UUID 存储在一个变量中,以用于 map() 函数中的每个记录。

如果您使用的是mapreduce API,您可以这样做——

public static class SampleMapper extends
            Mapper<LongWritable, Text, Text, Text> {

   String uuid;

   /**
    * This method will be called once at the beginning
    * of each map task
    */
    @Override
    protected void setup(Context context) throws IOException,
            InterruptedException {
        //generate your uuid here
        uuid = generateUUID();
    }


    @Override
    protected void map(LongWritable key, Text value, Context context)
            throws IOException, InterruptedException {

        //use uuid here
    }

}

在使用mapred API 的情况下,您可以这样做——

public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> { 

     String uuid;

     @override
     public void configure(JobConf job) {
         uuid = gernerateUUID();
     }

     public void map(LongWritable key, Text value, 
        OutputCollector<Text, IntWritable> output, Reporter reporter)
              throws IOException { 

          //use uuid here
     }

}

链接在这里。

于 2013-09-24T05:47:18.707 回答