在Pig中加入两个别名

时间:2016-10-24 16:28:26

标签: json apache-pig

这似乎是一个基本问题:

我正在使用大象鸟将一个json加载到猪身上,我想在json中只有几个字段。同样,我想使用这些字段并生成新字段并添加原始json。我有以下猪脚:

data_input = LOAD '$DATA_INPUT' USING com.twitter.elephantbird.pig.load.JsonLoader() AS (json:map []);

x = FOREACH data_input GENERATE json#'user__id_str' AS user_id, json#'user__created_at' AS user_created_at, json#'avl_user_days_active' AS user_days_active, json#'user__notifications' AS user_notifications, json#'user__follow_request_sent' AS user_follow_request_sent, json#'user__friends_count' AS user_following_count, json#'user__name' AS user_name, json#'user__time_zone' AS user_time_zone, json#'user__profile_background_color' AS user_profile_background_color, json#'user__is_translation_enabled' AS user_translation_enabled, json#'user__profile_link_color' AS user_profile_link_color, json#'user__utc_offset' AS user_UTC_offset, json#'user__profile_sidebar_border_color' AS user_profile_sidebar_border_color, json#'user__has_extended_profile' AS user_has_extended_profile, json#'user__profile_background_tile' AS user_profile_background_tile, json#'user__is_translator' AS user_is_tranlator, json#'user__profile_text_color' AS user_profile_text_color, json#'user__location' AS user_location, json#'user__profile_banner_url' AS user_profile_banner_url, json#'user__profile_use_background_image' AS user_profile_use_background_image, json#'user__default_profile_image' AS user_default_profile_image, json#'user__description' AS user_description, json#'user__profile_background_image_url_https' AS user_profile_background_image_url_https, json#'user__profile_sidebar_fill_color' AS user__profile_sidebar_fill_color, json#'user__followers_count' AS user_followers_count, json#'user__profile_image_url' AS user_profile_image_url, json#'user__geo_enabled' AS user_geo_enabled, json#'user__entities__description__urls' AS user_entities_description_urls, json#'user__screen_name' AS user_scren_name, json#'user__favourites_count' AS user_total_liked, json#'user__url' AS user_url, json#'user__statuses_count' AS user_total_posts, json#'user__default_profile' AS user_default_profile, json#'user__lang' AS user_language, json#'user__protected' AS user_protected, json#'user__listed_count' AS user_totalPublic_lists, json#'user__profile_image_url_https' AS user_profile_image_url_https, json#'user__contributors_enabled' AS user_contributors_enabled, json#'user__following' AS user_following, json#'user__verified' AS user_verified;

y1 = FOREACH x GENERATE user_total_posts/user_days_active as user_post_frequency;

y2 = GROUP x BY user_id;

z = FOREACH y2 GENERATE COUNT(x);

现在,我想将别名y1y2添加到x并将其写入输出文件。也就是说,我想将新字段user_post_frequency和user_total_replies添加到x并存储它。

我该怎么做?

编辑:

我尝试加入两个别名:

y1 = FOREACH x GENERATE user_id, user_total_posts/user_days_active as user_post_frequency;
y2 = JOIN x BY user_id, y1 BY user_id;
fs -rmr /tmp/user
STORE y2 INTO '/tmp/user' USING JsonStorage();

但我的输出看起来像:

{"x::user_id":"9642792","x::user_created_at":"Wed Oct 24 02:44:30 +0000 2007","x::user_days_active":"3272","x::user_notifications":"false","x::user_foll    ow_request_sent":"false","x::user_following_count":"500","x::user_name":"Everything Finance","x::user_time_zone":"Eastern Time (US & Canada)","x::user_p    rofile_background_color":"131516","x::user_translation_enabled":"false","x::user_profile_link_color":"992E01","   y1::user_id":"9642792","y1::user_post_frequency":10.910452322738386}

我不希望输出中有x::y::。只想要字段名称。我该怎么办?

2 个答案:

答案 0 :(得分:0)

您可以使用PIG project range功能生成所有列,并在现有关系中添加其他列。

x。$ 0 ..将投影关系x

中的所有字段
y1 = FOREACH x GENERATE x.$0 ..,user_total_posts/user_days_active as user_post_frequency;
y2 = GROUP y1 BY user_id;
z = FOREACH y2 GENERATE y1.$0 ..,COUNT(x) as user_total_replies ;

答案 1 :(得分:0)

如果您不希望x ::和y ::添加另一个FOREACH

y3 = FOREACH y2 GENERATE x::user_id AS user_id, ....