【问题标题】:How to read a JSON column as a field in Apache PIG如何在 Apache PIG 中读取 JSON 列作为字段
【发布时间】:2018-11-25 11:08:35
【问题描述】:

我正在尝试使用 Apache Pig 读取从 Kaggle 获得的电影数据集。其中一个 .csv 文件名为“keywords.csv”,它具有如下元组:

862,[{'id': 931, 'name': 'jealousy'}, {'id': 4290, 'name': 'toy'}, {'id': 5202, 'name': 'boy'}, {'id': 6054, 'name': 'friendship'}, {'id': 9713, 'name': 'friends'}, {'id': 9823, 'name': 'rivalry'}, {'id': 165503, 'name': 'boy next door'}, {'id': 170722, 'name': 'new toy'}, {'id': 187065, 'name': 'toy comes to life'}]
8844,[{'id': 10090, 'name': 'board game'}, {'id': 10941, 'name': 'disappearance'}, {'id': 15101, 'name': "based on children's book"}, {'id': 33467, 'name': 'new home'}, {'id': 158086, 'name': 'recluse'}, {'id': 158091, 'name': 'giant insect'}]
.
.
.

第一个字段是电影的 id,第二个字段是一个类似 JSON 的字符串,其中包含与该电影相关的关键字及其 id。数据集的所有 .csv 文件中的文件分隔符都是逗号,但是在加载关键字.csv 时会出现问题。这是我尝试加载表格的方式:

keywords = load 'dataset/keywords.csv' USING PigStorage(',') as (id:int, keywords:chararray);
fltr = filter keywords by id == 862;

DUMP fltr;

当我期望它打印出这样的东西时,它只会打印(862,"[{'id': 931)

(862,[{'id': 931, 'name': 'jealousy'}, {'id': 4290, 'name': 'toy'}, {'id': 5202, 'name': 'boy'}, {'id': 6054, 'name': 'friendship'}, {'id': 9713, 'name': 'friends'}, {'id': 9823, 'name': 'rivalry'}, {'id': 165503, 'name': 'boy next door'}, {'id': 170722, 'name': 'new toy'}, {'id': 187065, 'name': 'toy comes to life'}])

这样我就可以将列关键字保存在扩展名为 .json 的新文件中,然后使用 JsonLoader() 提取关键字。

我该怎么做呢?或者甚至可以直接读取关键字而无需将其保存到外部 .json 文件?谢谢。


更新 1

刚刚发现 Apache pig 中的地图,这是我最近的尝试:

keywords = load 'dataset/keywords.csv' USING PigStorage(',') as (id:int, keywords:[{keyId:int,name:chararray}]);

这会引发错误:Syntax error, unexpected symbol at or near 'int'

【问题讨论】:

    标签: hadoop bigdata apache-pig


    【解决方案1】:

    我认为您需要使用 Twitter 的 Elephant Bird 来解析 Pig 中的单个 json 列。 (如果你想解析只有 json 的文件,你可以简单地使用 Pig 的JsonLoader API)。

    这里是a related question - 看起来你的 json 也是一个数组,所以那里写的内容也适用于你。

    如果这不起作用,这里是a blog post describing how to write a Python UDF for a more specific case of JSON parsing。你当然可以用 Java UDF 做同样的事情。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-25
      • 1970-01-01
      • 2015-07-25
      • 1970-01-01
      • 2019-05-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多