Store data after decompression in pig

痴心易碎 提交于 2019-12-12 07:01:28

问题


Format of my file is -

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

I tried to store this using the following code

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

Stored data as shown below.

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

Expected out put is

    Tacos           Alice   3
    Tomato Soup     Sarah   2
    Grilled Cheese  Alex    5

How can I achieve this? Thanks in advance.


回答1:


Your problem is not how you store the data, but how you are loading it. You have a JSON file but you are reading the whole JSON into one field, so you get only one field per row. When you save it into your HCatalog table, you get 1 row with the JSON in one field and two null fields.

Instead of loading the data with PigStorage or whatever you are using, load it with JsonLoader:

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

You can DUMP the data to check that now it's correct:

DUMP STOCK_A;

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

Instead of:

DUMP STOCK_A;

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


来源:https://stackoverflow.com/questions/30476798/store-data-after-decompression-in-pig

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!