mapreduce, sort values



我的映射器有一个输出:

Mapper: KEY, VALUE(Timestamp, someOtherAttrbibutes)

My Reducer does receive:

Reducer: KEY, Iterable<VALUE(Timestamp, someOtherAttrbibutes)>

我希望Iterable<VALUE(Timestamp, someOtherAttrbibutes)>时间戳属性排序。有可能实施吗?

我想避免在Reducer代码中手动排序。http://cornercases.wordpress.com/2011/08/18/hadoop-object-reuse-pitfall-all-my-reducer-values-are-the-same/

我必须从Iterable中"深度复制"所有对象,这可能会导致巨大的内存开销。(((

这相对容易,您需要为您的VALUE类编写比较器类。

在这里仔细看看:http://vangjee.wordpress.com/2012/03/20/secondary-sorting-aka-sorting-values-in-hadoops-mapreduce-programming-paradigm/特别是在一个二级排序部分的解决方案。

您需要为您的VALUE类编写比较器类。

@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
    final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    List<String> list = new ArrayList<String>();
    for (Text val : values) {
        list.add(val.toString());
    }
    Collections.sort(list, new Comparator<String>() {
       public int compare(String s1, String s2) {
           String str1[] = s1.split(",");
           String str2[] = s2.split(",");
          int time1 = 0;
           int time2 = 0;
           try {
               time1 = (int)(sdf.parse(str1[0]).getTime());
               time2 = (int) (sdf.parse(str2[0]).getTime());
           } catch (ParseException e) {
               e.printStackTrace();
           } finally {
               return time1 - time2;
           }
       }
    });
    for(int i = 0; i < list.size(); ++i)
    context.write(key, new Text(list.get(i)));
}

相关内容

  • 没有找到相关文章

最新更新