json - 无法在 Pig 中使用 Elephant Bird 访问带有包和元组的嵌套 JSON

标签 json hadoop nested apache-pig elephantbird

我正在尝试使用 Pig 中的 Elephant Bird 解析一个嵌套的 JSON 对象,该对象的级别可以包含包和/或元组。在第四层引用列会导致一些奇怪的行为。

Pig 在引用第四个或以下的列时出现问题。似乎是因为数据在包、元组和映射之间做了一些交替。需要明确的是,看起来 JsonLoader 将一些转换为 map ,但其他则没有。例如,参见下面对“五”的引用。

HDP版本:2.1.2, pig 版本:0.12.1,象鸟版本:4.13

这是结构的示例数据,其中键和值替换为占位符。

   {
        "one" : {
            "output_info" : {
                "sample_key": "sample value"
            },
            "two" : {
                "three" : [{
                    "three_id" : "three_id_value",
                    "four" : {
                        "five" : [{
                                "level_five_info" : {
                                    "five_info_key" : "five_info_value"
                                },
                                "six" : {
                                    "seven" : [{
                                            "eight_id" : "123545",
                                            "eight_score" : "77"
                                        }, {
                                            "eight_id" : "98765",
                                            "eight_score" : "88"
                                        }
                                    ]
                                }
                            } 
                        ]
                    }
                }]
            }
        }
    }

pig 声明:
a = LOAD 'nest_test.dat' USING com.twitter.elephantbird.pig.load.JsonLoader('-nestedLoad') AS (json:map[]);

b = foreach a generate json#'one'#'two'#'three' as (three: {(three_id: chararray,four: map[])});

运行 dump b;结果是:
({([three_id#three_id_value,four#{five={([six#{seven={([eight_score#77,eight_id#123545]),([eight_score#88,eight_id#98765])}},level_five_info#{five_info_key=five_info_value}])}}])})

这一切看起来都符合预期,但是:
c = foreach b generate three.four as ({(four:map[])});

但现在,运行 dump c;导致以上数据均未返回。
({()})

省略模式描述也是如此
c = foreach b generate three.four

引用更深的级别会产生错误:
d = foreach b generate three.four#'five';

2016-03-15 11:56:01,655 [main] ERROR org.apache.pig.tools.grunt.Grunt - ERROR 1052: Cannot cast bag with schema :bag{:tuple(four:map)} to map with schema :map

我应该如何引用五级和六级?我的最终目标是能够引用 eight_ideight_score并将 seven 的元素展平阵列/袋

最佳答案

尝试使用

c = FOREACH b GENERATE FLATTEN(three);
d = FOREACH c GENERATE three::four#five AS five;

关于json - 无法在 Pig 中使用 Elephant Bird 访问带有包和元组的嵌套 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36016687/

相关文章:

java - 从AJAX接收HTTP POST,并在后端java中读取它

mysql - 使用 MySQL 作为 Metastore 安装 hive-0.7.1 后无法创建表

hadoop - 为什么在HiveServer2中保留Tez队列?

hadoop - 如何从客户端Windows计算机访问hadoop群集(unix)

json - 查询Mongodb子文档错误将循环结构转换为JSON

javascript - 无法使用javascript访问json对象

swift - 访问子类时不能在类型错误的实例上使用静态成员

java - 如何判断某个键是否包含在 Hashmap 中的非命名 Hashmap 中? Java、JavaFX

python - 对由列表乘法创建的嵌套引用列表进行深度复制不起作用

php - 如何在没有js代码的情况下显示json结果数组