你如何解码来自列的猪的JSON?

时间:2014-07-21 21:19:44

标签: apache-pig

有许多示例说明如何使用JsonLoader()从文件加载JSON数据,但不能从任何其他输出加载。

1 个答案:

答案 0 :(得分:2)

您正在寻找Elephant Bird中提供的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}

Pig Script:

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 as map):


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

此问题与How to parse a JSON string from a column with Pig

重复