0

级联新手,试图找到一种基于排序/顺序获取前 N 个元组的方法。例如,我想知道人们使用的前 100 个名字。

这是我可以在 teradata sql 中做的类似操作:

select top 100 first_name, num_records   
from
    (select first_name, count(1) as num_records   
     from table_1  
     group by first_name) a  
order by num_records DESC

hadoop pig 也有类似的情况

a = load 'table_1' as (first_name:chararray, last_name:chararray);
b = foreach (group a by first_name) generate group as first_name, COUNT(a) as num_records;
c = order b by num_records DESC;
d = limit c 100;

在 SQL 或 Pig 中似乎很容易做到,但很难尝试在级联中找到一种方法。请指教!

4

2 回答 2

1

方法 1 使用 GroupBy 并根据所需的列对它们进行分组,您可以利用级联提供的二级排序,默认情况下它按升序提供它们,如果我们希望它们按降序排列,我们可以通过反向排序()

获取前 n 个元组或行

它非常简单,只需在 FILTER 中使用 静态变量count 并将每个元组计数值增加 1 并将其增加 1 并检查天气它是否大于 N

当计数值大于 N 时返回 true,否则返回 false

这将为输出提供前 N 个元组

方法二

级联提供了一个独特的 inbuit 函数,它返回 firstNbuffer

请参阅以下链接 http://docs.cascading.org/cascading/2.2/javadoc/cascading/pipe/assembly/Unique.html

于 2014-03-19T07:27:44.383 回答
1

假设您只需要设置 Pipe 如何执行此操作:

在级联 2.1.6 中,

Pipe firstNamePipe = new GroupBy("topFirstNames", InPipe,  
                                 new Fields("first_name"),
                                 );

firstNamePipe = new Every(firstNamePipe, new Fields("first_name"), 
                          new Count("num_records"), Fields.All);

firstNamePipe = new GroupBy(firstNamePipe,  
                                 new Fields("first_name"),
                                 new Fields("num_records"),
                                 true); //where true is descending order

firstNamePipe = new Every(firstNamePipe, new Fields("first_name", "num_records")
                          new First(Fields.Args, 100), Fields.All)

InPipe 由您传入的点击形成,其中包含您在上面引用的元组数据。即,“名字”。“num_records”在new Count()被调用时创建。

如果您在单独的水龙头(表或文件)中有“num_records”和“first_name”数据,那么您可以设置两个指向这两个Tap源的管道并使用CoGroup.

我使用的定义来自 Cascading 2.1.6:

通过...分组(String groupName, Pipe pipe, Fields groupFields, Fields sortFields, boolean reverseOrder)

数数(Fields fieldDeclaration)

第一的(Fields fieldDeclaration, int firstN)

于 2013-04-30T19:03:33.417 回答