您正在尋找象鳥提供的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 /串):
-------------------------------------------------------------------------------------------------------
| 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?
本來不想使用ElephantBird了點。有沒有其他方法?我試圖重新編譯EB,但卡住了某處。看起來這不容易建立。 #noob –
爲什麼不呢?你不必編譯ElephantBird。你可以在http://search.maven.org/#artifactdetails%7Ccom.twitter.elephantbird%7Celephant-bird-pig%7C4.5%7Cjar查找罐子 – SNeumann