您需要为您的 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)));
}