在pig中存储解压后的数据

xoefb8l8  于 2021-06-25  发布在  Pig
关注(0)|答案(1)|浏览(324)

我的文件格式是-

({"food":"Tacos", "person":"Alice", "amount":3})
    ({"food":"Tomato Soup", "person":"Sarah", "amount":2})
    ({"food":"Grilled Cheese", "person":"Alex", "amount":5})

我试着用下面的代码来存储这个

STORE STOCK_A 
    INTO 'default.ash_json_pigtest' 
    USING HCatStorer();

存储的数据如下所示。

{"food":"Tacos", "person":"Alice", "amount":3}             None    None
    {"food":"Tomato Soup", "person":"Sarah", "amount":2}    None    None
    {"food":"Grilled Cheese", "person":"Alex", "amount":5}  None    None

预计产量为

Tacos           Alice   3
    Tomato Soup     Sarah   2
    Grilled Cheese  Alex    5

我怎样才能做到这一点?提前谢谢。

mhd8tkvw

mhd8tkvw1#

问题不在于如何存储数据,而在于如何加载数据。您有一个json文件,但是您正在将整个json读入一个字段,因此每行只能得到一个字段。当您将它保存到hcatalog表中时,一个字段中有一行json,两个空字段中有一行json。
而不是用 PigStorage 或者不管你在用什么,把它装上 JsonLoader :

STOCK_TABLE = LOAD 'your.data' USING JsonLoader('food:chararray, person:chararray, amount:int');

你可以 DUMP 检查数据是否正确:

DUMP STOCK_A;

(Tacos,Alice,3)
(Tomato Soup,Sarah,2)
(Grilled Cheese,Alex,5)

而不是:

DUMP STOCK_A;

({"food":"Tacos", "person":"Alice", "amount":3})
({"food":"Tomato Soup", "person":"Sarah", "amount":2})
({"food":"Grilled Cheese", "person":"Alex", "amount":5})

相关问题