假设我有一组餐厅评论数据:
User,City,Restaurant,Rating
Jim,New York,Mecurials,3
Jim,New York,Whapme,4.5
Jim,London,Pint Size,2
Lisa,London,Pint Size,4
Lisa,London,Rabbit Whole,3.5
我想按用户和城市的平均评论生成一个列表。即输出:
User,City,AverageRating
Jim,New York,3.75
Jim,London,2
Lisa,London,3.75
我可以编写一个 Pig 脚本如下:
Data = LOAD 'data.txt' USING PigStorage(',') AS (
user:chararray, city:chararray, restaurant:charray, rating:float
);
PerUserCity = GROUP Data BY (user, city);
ResultSet = FOREACH PerUserCity {
GENERATE group.user, group.city, AVG(Data.rating);
}
但是我很好奇我是否可以先将更高级别的组(用户)分组,然后再将下一个级别(城市)分组:即
PerUser = GROUP Data BY user;
Intermediate = FOREACH PerUser {
B = GROUP Data BY city;
GENERATE group AS user, B;
}
我得到:
Error during parsing.
Invalid alias: GROUP in {
group: chararray,
Data: {
user: chararray,
city: chararray,
restaurant: chararray,
rating: float
}
}
有没有人成功地尝试过这个?是否根本不可能在 FOREACH 中进行 GROUP?
我的目标是做类似的事情:
ResultSet = FOREACH PerUser {
FOREACH City {
GENERATE user, city, AVG(City.rating)
}
}