2015-11-11 73 views
0

如何計算每種類型事件的多少個1和0?我在豬身上做了所有這些,第二場只有1和0。 的數據是這樣的:按Pig中的組計數1和0

(pageLoad,1) 
(pageLoad,0) 
(pageLoad,1) 
(appLaunch,1) 
(appLaunch,0) 
(otherEvent,1) 
(otherEvent,0) 
(event,1) 
(event,1) 
(event,0) 
(somethingelse,0) 

的輸出將是這樣的

pageLoad 1:234 0:2359 
appLaunch 1:54 0:111 
event 1:345 0:0 

type 1 0 
pageLoad 21 345 
appLaunch 0 123 
event 234 12 

謝謝大家。

回答

1

輸入:

pageLoad,1 
pageLoad,0 
pageLoad,1 
appLaunch,1 
appLaunch,0 
otherEvent,1 
otherEvent,0 
event,1 
event,1 
event,0 
somethingelse,0 

豬腳本:

A = LOAD 'input.csv' USING PigStorage(',') AS (event_type:chararray,status:int); 
B = GROUP A BY event_type; 
req = FOREACH B { 
    event_type_1 = FILTER A BY status==1; 
    event_type_0 = FILTER A BY status==0; 
    GENERATE group AS event_type, COUNT(event_type_1) AS event_type_1_count, COUNT(event_type_0) AS event_type_0_count; 
}; 
DUMP req; 

輸出:

(event,2,1) 
(pageLoad,2,1) 
(appLaunch,1,1) 
(otherEvent,1,1) 
(somethingelse,0,1) 
+0

這完全成功了!謝謝! – 1EnemyLeft