2016-10-24 35 views
0

這看起來像是一個基本問題:在Pig中加入兩個別名

我正在使用大象鳥加載一個json到Pig,我只想在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} 

我DONOT想在輸出x::y::。只需要字段名稱。我應該做什麼?

回答

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 ; 
+0

謝謝!給出語法錯誤:'錯誤1200:<文件user.pig,第11行,第35列>不匹配的輸入'..'期待SEMI_COLON' – kskp

+0

確保$ 0之後有一個空格或這可能與您正在使用的豬版本有關如果'..'不被識別,那麼顯式指定columns.y1 = FOREACH x GENERATE x。$ 0,x。$ 1,x。$ 2,user_total_posts/user_days_active as user_post_frequency; –

0

如果你不想X ::和y ::再添FOREACH

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