2017-05-23 5 views
0

아래 텍스트 파일을 입력 했으므로 논리를 기반으로 다른 파일에서 출력을 생성해야합니다.PIG의 조건부

customerid|Dateofsubscription|Customercode|CustomerType|CustomerText 
 
1001|2017-05-23|455|CODE|SPRINT56 
 
1001|2017-05-23|455|DESC|Unlimited Plan 
 
1001|2017-05-23|455|DATE|2017-05-05 
 
1002|2017-05-24|455|CODE|SPRINT56 
 
1002|2017-05-24|455|DESC|Unlimited Plan 
 
1002|2017-05-24|455|DATE|2017-05-06

논리 :

If Customercode = 455 
 
if(CustomerType = "CODE") 
 
    Val= CustomerText 
 
if(CustomerType = "DESC") 
 
    Description = CustomerText 
 
if(CustomerType = "DATE") 
 
    Date = CustomerText

출력 :

여기 내 입력 파일입니다

당신이 좀 도와 주 시겠어요.

답변

0
rawData = LOAD data; 
filteredData = FILTER rawData BY (Customercode == 455); 

--Extract and set Val/Description/Date based on CustomerText and 'null' otherwise 
ExtractedData = FOREACH filteredData GENERATE 
      customerId, 
      (CustomerType == "CODE" ? CustomerText : null) AS Val, 
      (CustomerType == "DESC" ? CustomerText : null) AS Description, 
      (CustomerType == "DATE" ? CustomerText : null) AS Date; 

groupedData = GROUP ExtractedData BY customerId; 

--While taking MAX, all 'nulls' will be ignored 
finalData = FOREACH groupedData GENERATE 
      group as CustomerId, 
      MAX($1.Val) AS Val, 
      MAX($1.Description) AS Description, 
      MAX($1.Date) AS Date; 

DUMP finalData; 

내가 코어 로직을 지정했습니다. 적재, 형식 지정 및 보관은 직설적이어야합니다.

+0

고맙습니다. – satya

0

customercode = 455 인 입력을 필터링하고 필요한 2 열을 생성 한 다음 고객 ID별로 그룹화 한 다음 BagToString 을 사용하십시오.

B = FILTER A BY Customercode == 455; 
C = FOREACH B GENERATE $0 as CustomerId,$4 as CustomerText; 
D = GROUP C BY CustomerId; 
E = FOREACH D GENERATE group AS CustomerId, BagToString(C.CustomerText, '|'); -- Note:This will generate 1001,SPRINT56|Unlimited Plan|2017-05-05 so,you will have to concat the first field with '|' and then concat the resulting field with the second field which is already delimited by '|'. 
F = FOREACH E GENERATE CONCAT(CONCAT($0,'|'),$1); 
DUMP F; 
+0

고맙습니다. – satya