在猪中减压后存储数据

时间:2015-05-27 08:15:31

标签: tuples apache-pig

我的档案格式为 -

 ({"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

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

1 个答案:

答案 0 :(得分:0)

您的问题不是存储数据的方式,而是您如何加载数据。您有一个JSON文件但是您正在将整个JSON读入一个字段,因此每行只能获得一个字段。将它保存到HCatalog表中时,在一个字段和两个空字段中获得一行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})