【问题标题】:How to parse a JSON string from a column with Pig如何使用 Pig 从列中解析 JSON 字符串
【发布时间】:2014-07-11 19:29:19
【问题描述】:

我有 tsv 日志文件,其中一列由 json 字符串填充。

我想在 Pig 脚本中使用 JsonLoader 解析该列。 我看到了很多例子,其中 JsonLoader 用于每一行只是一个 json 字符串的情况。我还有其他专栏想跳过,但我不知道该怎么做。

文件如下所示:

foo    bar    {"version":1; "type":"an event"; "count": 1}
foo    bar    {"version":1; "type":"another event"; "count": 1}

我该怎么做?

【问题讨论】:

    标签: logging hadoop apache-pig bigdata


    【解决方案1】:

    您正在寻找象鸟中提供的 JsonStringToMap UDF:https://github.com/kevinweil/elephant-bird/search?q=JsonStringToMap&ref=cmdform

    示例文件:

    foo     bar     {"version":1, "type":"an event", "count": 1}
    foo     bar     {"version":1, "type":"another event", "count": 1}
    

    猪脚本:

    REGISTER /path/to/elephant-bird.jar;
    DEFINE JsonStringToMap com.twitter.elephantbird.pig.piggybank.JsonStringToMap();
    raw = LOAD '/tmp/file.tsv' USING PigStorage('\t') AS (col1:chararray,col2:chararray,json_string:chararray);
    parsed = FOREACH raw GENERATE col1,col2,JsonStringToMap(json_string);
    ILLUSTRATE parsed; -- Just to show the output
    

    预处理(JSON 为 chararray/string):

    -------------------------------------------------------------------------------------------------------
    | raw     | col1:chararray    | col2:chararray    | json_string:chararray                             | 
    -------------------------------------------------------------------------------------------------------
    |         | foo               | bar               | {"version":1, "type":"another event", "count": 1} | 
    

    后处理(JSON 作为地图):


    -------------------------------------------------------------------------------------------------
    | parsed     | col1:chararray    | col2:chararray    | json:map(:chararray)                     | 
    -------------------------------------------------------------------------------------------------
    |            | foo               | bar               | {count=1, type=another event, version=1} | 
    -------------------------------------------------------------------------------------------------
    

    这是两天前问的同一个问题:How do you decode JSON in Pig that comes from a column?

    【讨论】:

      【解决方案2】:

      查看 Elephantbird(Twitter 的所有 Hadoop 库)——他们有一个名为 JsonStringToMap 的 UDF,它完全可以满足您的需求(获取字符串并将其转换为地图)。

      【讨论】:

      • 不想为此使用 ElephantBird。还有其他方法吗?我试图重新编译 EB 但卡在某个地方。看来建造起来并不容易。 #菜鸟
      • 为什么不呢?您不必编译 ElephantBird。您可以在 search.maven.org/… 查找罐子
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-28
      相关资源
      最近更新 更多