如何使用pig将下面的元组集转换为格式化的json?

u5i3ibmn  于 2021-06-21  发布在  Pig
关注(0)|答案(2)|浏览(306)

我有下面的Pig袋子和两个元组。

{(1,'Kumar','23000','Kandy')}
{(2,'Mahela','34000','Colombo')}

如何使用pig将上述内容转换为以下json键值格式?

{
   1: 'kumar,23000,kandy',
   2: 'mahela',34000,'colombo'
}

任何帮助都将不胜感激。请注意,我需要将其存储在一个json文件中
在奥利弗的回答之后

{"fields":[{"name":"group","type":10,"description":"autogenerated from Pig Field Schema","schema":null},{"name":"test_table_columns_needed","type":120,"description":"autogenerated from Pig Field Schema","schema":{"fields":[{"name":null,"type":110,"description":"autogenerated from Pig Field Schema","schema":{"fields":[{"name":"id","type":10,"description":"autogenerated from Pig Field Schema","schema":null},{"name":"name","type":55,"description":"autogenerated from Pig Field Schema","schema":null},{"name":"salary","type":55,"description":"autogenerated from Pig Field Schema","schema":null},{"name":"destination","type":55,"description":"autogenerated from Pig Field Schema","schema":null}],"version":0,"sortKeys":[],"sortKeyOrders":[]}}],"version":0,"sortKeys":[],"sortKeyOrders":[]}}],"version":0,"sortKeys":[],"sortKeyOrders":[]}   {"group":1,"test_table_columns_needed":[{"id":1,"name":"'Kumar'","salary":"'23000'","destination":"'Kandy'"}]}
 {"group":2,"test_table_columns_needed":   [{"id":2,"name":"'Mahela'","salary":"'34000'","destination":"'Colombo'"}]}
rdrgkggo

rdrgkggo1#

这里有一种方法根据输入数据

{(1,'Kumar','23000','Kandy')}
{(2,'Mahela','34000','Colombo')}

A = LOAD ' /user/root/test1110_1.txt'  as (t1:(ID:chararray,     Name:chararray,Sal: chararray, Loc:chararray));
B = foreach A GENERATE t1.ID as ID, REPLACE(CONCAT(t1.Name ,',' ,     t1.Sal,',', t1.Loc ), '[\\\'\\(\\)]','') as Val:CHARARRAY ;
STORE B INTO 'tuple_table.json'  USING JsonStorage();

([1#Kumar,23000,Kandy])
([2#Mahela,34000,Colombo])
llew8vvj

llew8vvj2#

tuple_entry = LOAD 'tuple_array' 
    USING PigStorage() 
    AS ('ref: int, name:chararray, amount:int, surname:chararray');

STORE tuple_entry 
    INTO 'tuple_table.json' 
    USING JsonStorage();

修改file.pig\u模式以符合元组输入。

相关问题