如何获得工资高于所在部门平均水平的员工的姓名和部门?

wi3ka0sx  于 2021-06-24  发布在  Pig
关注(0)|答案(2)|浏览(460)

我是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

帮帮我,谢谢

nukf8bse

nukf8bse1#

按部门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;
v8wbuo2f

v8wbuo2f2#

JOIN a和c由deptid、grpid和 FILTER 其中薪资>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;

相关问题