按照Hadoop中的值从文件中进行排序 [英] Sorting by value in Hadoop from a file
问题描述
我有一个文件,其中包含一个字符串,然后是一个空格,然后是每行上的一个数字。
示例:
第一行:第二行
第二行:Word1 8
Line3:Word2 1
我需要按降序对数字进行排序,然后将结果放入一个文件中,为文件分配一个等级。所以我的输出应该是一个包含以下格式的文件:
Line1:Word1 8 1
Line2:Word 2 2
Line3:Word2 1 3
有没有人有想法,我该怎么做Hadoop的?
我在Hadoop中使用java。
我设计了解决这个问题的方案。实际上很简单。
为了按值排序,您需要使用
setOutputValueGroupingComparator(Class)
要按降序排序,您需要使用您需要使用
计数器类
, getCounter
和增量
函数。
I have a file containing a String, then a space and then a number on every line.
Example:
Line1: Word 2
Line2 : Word1 8
Line3: Word2 1
I need to sort the number in descending order and then put the result in a file assigning a rank to the numbers. So my output should be a file containing the following format:
Line1: Word1 8 1
Line2: Word 2 2
Line3: Word2 1 3
Does anyone has an idea, how can I do it in Hadoop? I am using java with Hadoop.
I devised the solution to this problem. It was simple actually.
For sorting by value you need to use
setOutputValueGroupingComparator(Class)
For sorting in decreasing order you need to use setSortComparatorClass(LongWritable.DecreasingComparator.class);
For ranking you need to use
Counter class
, getCounter
and increment
function.
这篇关于按照Hadoop中的值从文件中进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!