2016-12-10 2 views
0
나는 형식

GROUP BY, 필터 및 주문 데이터

(id_type:chararray,id:long,date_cnt_bag:{(date:chararray,count:long)}) 

난 단지 내가 형식

다음
(id, date, A_count, B_count, C_count, D_count) 
에이 데이터를 변환 할 4 개 가지 ID 유형
id_type: A/B/C/D 

이 다음에 데이터가

예 :

A 1 {(20161209, 100),(20161208, 90),(20161207, 80)} 
B 1 {(20161209, 1000),(20161208, 900),(20161207, 800)} 
C 1 {(20161209, 100),(20161208, 90)} 
D 1 {(20161209, 10),(20161208, 9),(20161207, 8)} 
A 2 {(20161209, 100),(20161208, 90),(20161207, 80)} 
B 2 {(20161209, 1000),(20161207, 800)} 
C 2 {(20161209, 100),(20161208, 90),(20161207, 80)} 
D 2 {(20161209, 10),(20161208, 9),(20161207, 8)} 

출력은 다음과 같아야합니다. 또한 해당 날짜에 카운트가 누락 된 경우 0을 입력합니다.

1 20161209 (100 1000 100 10) 
1 20161208 (90 900 90 9) 
1 20161207 (80 800 0 8) 
2 20161209 (100 1000 100 10) 
2 20161208 (90 0 90 9) 
2 20161207 (80 800 80 8) 

가능한 해결책과 힌트를 찾았습니다. 하지만 나는 아무데도 가지 않을거야. 미리 감사드립니다. 데이터가 될 것입니다 빈 행에 대해 검증되지 않은 경우

답변

1
raw = LOAD '...' using PigStorage(...) as (id_type:chararray,id:long,date_cnt_bag:{(date:chararray,count:long)}); 

flattened = foreach raw generate id_type, id, flatten(date_cnt_bag); 

final = foreach (group flattened by (id, date)) { 
    A_count_g = filter flattened by id_type == 'A'; 
    A_count = foreach A_count_g generate count as a_cnt; 
    ... same for all 4 ... 
    generate 
    group.id, 
    group.date, 
    flatten(A_count) 
    ; 
}; 
1

@Vinay 다음은 0이 아닌가 null의 정보 행에 대한 데이터를 그리워 얻을 것이다 스크립트 (2,2016120890090,9)입니다 잃어버린.

raw = LOAD '/user/data/grp_ABCD.txt' using PigStorage('\t') as (id_type:chararray,id:long,date_cnt_bag:{(date:chararray,count:long)}); 

flattened = foreach raw generate id_type, id, flatten(date_cnt_bag); 

final = foreach (group flattened by (id, date)) { 
    A_count_g = filter flattened by id_type == 'A'; 
    A_count = foreach A_count_g generate count as a_cnt; 
    B_count_g = filter flattened by id_type == 'B'; 
    B_count = foreach B_count_g generate count as b_cnt;  
    C_count_g = filter flattened by id_type == 'C'; 
    C_count = foreach C_count_g generate count as c_cnt; 
    D_count_g = filter flattened by id_type == 'D'; 
    D_count = foreach D_count_g generate count as d_cnt; 

    generate 
    group.id, 
    group.date, 
    flatten((IsEmpty(A_count) ? {((long)0)} : A_count)),  
    flatten((IsEmpty(B_count) ? {((long)0)} : B_count)), 
    flatten((IsEmpty(C_count) ? {((long)0)} : C_count)), 
    flatten((IsEmpty(D_count) ? {((long)0)} : D_count)) 
    ; 
}; 
+0

로드 명령문 자체가 잘못되었습니다. 데이터는 쉼표로 구분되어 있으며 '\ t'탭을 구분 기호로 사용하고 있습니다. –

+0

일반 필드와 백 필드의 구분 기호가 같은 경우 작동하지 않습니다. '/ t'을 일반 및 ','백에 사용했습니다. 도움이 되었기를 바랍니다. –