在hadoop中,如何获取值中的最后一个元素

In hadoop,how to get last element in values

例如,这里有一些 .csv 格式的输入数据:

而我想要的是:

输出, 但我得到的是:

我不知道我的代码有什么问题,下面是我的程序的一些代码:

功能图:

 public class MergeUrlMapper extends MapReduceBase implements Mapper<LongWritable, Text, Text, Text> {
public void map(LongWritable key, Text value, OutputCollector<Text, Text> output, Reporter reporter) throws IOException {
    String valueString = value.toString();
    String[] UrlHtmlData = valueString.split(",");
    output.collect(new Text(UrlHtmlData[0]), new Text(UrlHtmlData[1]));
}
}

和函数减少:

public class MergeUrlReducer extends MapReduceBase implements Reducer<Text, Text, Text, Text> {
public void reduce(Text t_key, Iterator<Text> values, OutputCollector<Text,Text> output, Reporter reporter) throws IOException {
    Text key = t_key;
    // if values is empty,then output will be (t_key,t_key)
    Text latestHtml = t_key;
    while (values.hasNext()) {
        Text temp = values.next();
        latestHtml = temp;
    }
    output.collect(key, latestHtml);
}
}

我的代码有什么问题,输出应该是最后一个值,但实际上它是第一个值。提前致谢!

不保证值的顺序。

如果您想根据某种顺序对它们进行排序,您需要将所有迭代器值添加到一个 Arraylist 中,然后根据需要使用自定义比较器对其调用 Collections.sort

然后在list.size() - 1

获取元素

此外,根据您的问题,您的输入不包含逗号,因此请确保您拆分的是正确的字符。