【问题标题】:Ingesting an array of JSON objects and transforming to tabular data摄取 JSON 对象数组并转换为表格数据
【发布时间】:2015-08-14 22:17:13
【问题描述】:

我有一个这样的 JSON 对象数组。 [ 和 ] 封装的每个数组都在一行上。

[{"event":0,"properties":{"color":"red","connectionType":2}}{"event":30,"properties":{"color":"blue","connectionType":4}},{"event":45,"properties":{"color":"green","connectionType":3}}] [{"event":0,"properties":{"color":"red","connectionType":5}}, {"event":1,"properties":{"color",:"blue","connectionType":6}}]

这里的格式更容易阅读。

[
    {"event":0, "properties":{"color":"red","connectionType":2}},
    {"event":3, "properties":{"color":"blue",'connectionType":4}},
    {"event":45, "properties":{"color":"green","connectionType":3}}
]
[
    {"event":0, "properties":{"color":"red","connectionType":5}},
    {"event":1, "properties":{"color":"blue","connectionType":6}}
]

有些事情需要注意,所以 [ ] 中的每个 JSON 对象都在一行中。每行中的对象数量各不相同。属性内的字段数量也各不相同。

我想要这些数据是获取每个 JSON 对象并将其以逗号分隔或制表符分隔值的形式转换为表格格式

| event    | color    | connectionType
  0          red        2
  3          blue       4

我查看了 PIG 用于读取 JSON 结构的一些工具 - 即大象鸟,但不能完全让它在我的数据上工作。

我希望获得有关替代解决方案的指针,或使用 elephant-bird / 其他 pig json 解析器 的示例代码。我的最终目标实际上只是捕获事件和属性的子集并将它们加载到 Hive 中。

【问题讨论】:

    标签: hadoop hive apache-pig hdfs


    【解决方案1】:

    在您的 json 文件中。你没有开始对象。所以它不区分行。我找到了解决方案,但我已将 start 对象放入您的 json 对象中。

    {"startObject":[{"event":0, "properties":{"color":"red","connectionType":2}},{"event":3, "properties":{"color":"blue","connectionType":4}},{"event":45, "properties":{"color":"green","connectionType":3}}]}
    
    A = LOAD '/home/kishore/Data/Pig/pig.json' USING JsonLoader('{(event:chararray,properties: (color:chararray,connectionType:chararray))}');
    B = foreach A generate Flatten($0);
    C = foreach B generate $0,Flatten($1);
    Dump C;
    
    Result
    
    (0,red,2)
    (3,blue,4)
    (45,green,3)
    

    如果你想解析你的 json 对象而不放置 start 对象,在这种情况下你应该编写你自己的自定义 UDF。 https://gist.github.com/kimsterv/601331

    或者去象鸟 https://github.com/twitter/elephant-bird

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-07
      • 1970-01-01
      • 1970-01-01
      • 2021-05-26
      • 1970-01-01
      • 1970-01-01
      • 2020-03-29
      相关资源
      最近更新 更多