2015-06-07 36 views
0

我寫了UDF(extends EvalFunc<Tuple>),其具有與內元組(嵌套的)輸出元組。取消嵌套元組嵌套到單個術語

例如轉儲的樣子:

(((photo,photos,photo))) 
(((wedg,wedge),(audusd,audusd))) 
(((quantum,quantum),(mind,mind))) 
(((cassi,cassie),(cancion,canciones))) 
(((calda,caldas),(nova,novas),(rodada,rodada))) 
(((fingerprint,fingerprint),(craft,craft),(easter,easter))) 

現在我想處理每個這種條款,明顯的它,並給它一個id(RANK)。要做到這一點,我需要擺脫括號。在這種情況下,簡單的FLATTEN不起作用。

最終的輸出應該是這樣的:

1 photo 
2 photos 
3 wedg 
4 wedge 
5 audusd 
6 quantum 
7 mind 
.... 

我的代碼(不是UDF一部分,而不是原始解析):

tags = FOREACH raw GENERATE FLATTEN(tags) AS tag; 
tags_distinct = DISTINCT tags; 
tags_sorted = RANK tags_distinct BY tag; 
DUMP tags_sorted; 

回答

1

我覺得你的UDF是回報是不是最佳的你流程。而不是返回與字段(這是元組)的可變數量的元組的,那就方便多了返回元組的袋子。

而不是

(((wedg,wedge),(audusd,audusd))) 

,你將有

({(wedg,wedge),(audusd,audusd)}) 

,你就能那個包壓扁到: 1.使DISTINCT 2. RANK標籤

要做到這一點,更新您的UDF這樣的:

class MyUDF extends EvalFunc <DataBag> { 

    @Override 
    public DataBag exec(Tuple input) throws IOException { 
     // create DataBag 
    } 
} 
+0

謝謝你的建議。我如何指定數據類型? 「xyz AS tag:bag {t:tuple()};」 - 喜歡這個? – Stefanos13

+0

看到我的更新來改變你的UDF – glefait

+0

好的,謝謝,但我已經做了我的UDF這種變化。我的意思是豬的數據類型。告訴豬它的一個袋子與元組,並且我可以迭代它。我的問題是我不能弄清楚如何遍歷它,直到但 – Stefanos13