2013-04-30 57 views
0

新來級聯,試圖找到一種方法來獲得基於排序/順序的前N個元組。例如,我想知道人們使用的前100名。hadoop層疊如何獲取頂部N元組

這裏就是我可以在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的豬相似

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或豬的事,但有一個困難時期嘗試找到一種方式來實現級聯。請指教!

回答

1

假設你只需要在管道上如何做到這一點成立:

在級聯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你在上面引用。即,「first_name」。當調用new Count()時會創建「num_records」。

如果你有「num_records」和「FIRST_NAME」數據保存在單獨的水龍頭(表或文件),那麼你可以設置兩個管道指向這兩個Tap來源和使用CoGroup加入他們的行列。

我使用的定義來自級聯2.1。需要

GroupBy(String groupName, Pipe pipe, Fields groupFields, Fields sortFields, boolean reverseOrder)

Count(Fields fieldDeclaration)

First(Fields fieldDeclaration, int firstN)

+0

Engineiro嗨,我覺得你是在「FIRST_NAME」字段分組的,和排序在同一組內的num_records上,即僅在具有相同名字的組內進行排序。但是我想在這裏做的是獲得頂級名字。排序一組,然後得到頂部行。 – Kartrace 2013-04-30 21:36:40

+0

到目前爲止,我能想到的是在{first_name,num_records}方案中添加一個常量字段,並在該常量字段上對其進行分組以獲得單個組。然後在num_records上排序,並得到最高的N. – Kartrace 2013-04-30 21:39:36

+0

你是對的。我做了一些編輯。請記住,這是所有本地排序。一般而言,Hadoop和級聯並不十分熱衷於總體排序。對於總體排序,您需要一個級聯減速器。 – Engineiro 2013-05-01 13:40:46

1

方法1 使用的GroupBy並將它們組在列基和u可以利用二次分選的是:6由級聯提供,默認情況下它會按升序提供,如果我們希望他們按照順序排序,我們可以通過reverseo來完成刻申()

爲了得到TOP N元組或行

它只是使用靜態變量計數濾波器和1以1爲每個元組計數值加增量,並檢查天氣很簡單它是多於N

還真當計數值大於N或否則返回false

這將提供輸出中與第一N元組

方法2

級聯提供了獨特的inbuit函數返回firstNbuffer

看到下面的鏈接 http://docs.cascading.org/cascading/2.2/javadoc/cascading/pipe/assembly/Unique.html

相關問題