Pig:使用实际的 key_name 和值创建 json 文件

Posted

技术标签:

【中文标题】Pig:使用实际的 key_name 和值创建 json 文件【英文标题】:Pig: Create json file with actual key_name and values 【发布时间】:2016-10-19 19:27:30 【问题描述】:

我有一个使用大象鸟 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', json#'user__created_at', json#'user__notifications', json#'user__follow_request_sent', json#'user__friends_count', json#'user__name', json#'user__time_zone', json#'user__profile_background_color', json#'user__is_translation_enabled', json#'user__profile_link_color', json#'user__utc_offset', json#'user__profile_sidebar_border_color', json#'user__has_extended_profile', json#'user__profile_background_tile', json#'user__is_translator', json#'user__profile_text_color', json#'user__location', json#'user__profile_banner_url', json#'user__profile_use_background_image', json#'user__default_profile_image', json#'user__description', json#'user__profile_background_image_url_https', json#'user__profile_sidebar_fill_color', json#'user__followers_count', json#'user__profile_image_url', json#'user__geo_enabled', json#'user__entities__description__urls', json#'user__screen_name', json#'user__favourites_count', json#'user__url', json#'user__statuses_count', json#'user__default_profile', json#'user__lang', json#'user__protected', json#'user__listed_count', json#'user__profile_image_url_https', json#'user__contributors_enabled', json#'user__following', json#'user__verified';

STORE x INTO '$DATA_OUTPUT' USING JsonStorage();

我的输出正确,但字段名称错误。

我的输出有 val_n 代替字段名称本身:

"val_0":"40510796","val_1":"Sat May 16 18:03:53 +0000 2009","val_2":"false"......

我想要类似的东西:

"user__id_str":"40510796","user__created_at":"Sat May 16 18:03:53 +0000 2009","user__notifications":"false"...........

如何获得列名?

【问题讨论】:

【参考方案1】:

您是否尝试过在生成语句中提供别名:

x = FOREACH data_input GENERATE json#'user__id_str' AS user__id_str, json#'user__created_at' AS user__created_at;

【讨论】:

谢谢,它只是挂在地图阶段。 在本地模式下工作。不知道为什么它在 Map reduce 模式下挂起。我有 4 个数据节点。会调查的。谢谢指点。

以上是关于Pig:使用实际的 key_name 和值创建 json 文件的主要内容,如果未能解决你的问题,请参考以下文章

C#引用类型(class)和值类型(struct)

Python字典和值

如何在 PIG 脚本中从数据包中读取数据

数据存储实体 key_name 可以有多长(最大字符数)?拥有很长的 key_names 是否很糟糕?

从 Pig 保存到 Hive 表的问题

无法删除 Pig 中的特殊字符