2017-11-04 213 views
0

我是Hadoop和pig的新手。根據問題我能夠深入研究,直到下面的腳本,但我怎麼能比較個人的薪水與他的部門的平均工資。以下是寫入得到各部門的平均工資腳本如何獲得薪水高於其部門平均水平的員工的姓名和部門?

A = LOAD 'Assignment_1_Input.log' USING PigStorage('\t') as (id:int,name:chararray,age:int,salary:int,deptid:int); 
B = GROUP A by deptid; 
STORE B INTO 'Assign1GrpByNew'; 
C = FOREACH B GENERATE group as grpId,AVG(A.salary) as grpAvgSal; 
DUMP C; 

輸入文件:

15878 mohan 24  8000 1 
19173 ramya 27  10000 1 
9527 krishna 35  40000 2 
9528 raj  36  60000 2 
16884 ravi 50  70000 2 

期望輸出

ramya 1 
raj 2 
ravi 2 

幫我個忙,謝謝

回答

0

JOIN A和C by deptid,grpId和FILTER where salary> grpAvgSal

A = LOAD 'Assignment_1_Input.log' USING PigStorage('\t') as (id:int,name:chararray,age:int,salary:int,deptid:int); 
B = GROUP A by deptid; 
STORE B INTO 'Assign1GrpByNew'; 
C = FOREACH B GENERATE group as grpId,AVG(A.salary) as grpAvgSal; 

D = JOIN A BY deptid,C BY grpId; 
E = FILTER D BY (A::salary > C::grpAvgSal); 
DUMP E; 
0

GROUP BY dept_id並計算每個員工記錄的平均工資並選擇那些工資大於平均值的員工。

段:

inp_data = LOAD 'Assignment_1_Input.log' USING PigStorage('\t') as (id:int,name:chararray,age:int,salary:int,deptid:int); 
inp_data_fmt = FOREACH(GROUP inp_data BY deptid) GENERATE FLATTEN(inp_data), AVG(inp_data.salary) AS avg_salary; 
req_data = FILTER inp_data_fmt BY salary > avg_salary; 
DUMP req_data; 
相關問題