kenlz
kenlz

Reputation: 461

Getting Name Value JSON in PIG

Hi guys i just started doing pig, I was wondering if JsonLoader is capable of parsing all value inside json.

for example:

{"food":"Tacos", "person":"Alice", "amount":3}

and i need to get "food" stored as a relation in chararray and "Tacos" which is the value of "food" to another relation.

after reading many tutorial and documentation, i havent found a built in method to do so.

does it mean that the only solution to this is through UDF?

thanks a lot!

Upvotes: 1

Views: 249

Answers (2)

kenlz
kenlz

Reputation: 461

I found the answer that is to use external jar from twitter.

register 'hdfs:/udf/elephant-bird-pig-4.10.jar';
register 'hdfs:/udf/elephant-bird-core-4.10.jar';
register 'hdfs:/udf/elephant-bird-hadoop-compat-4.10.jar';
register 'hdfs:/udf/json-simple-1.1.1.jar';

test.json

{"food":"Tacos", "person":"Alice", "amount":3}

script:

A = LOAD 'hdfs:/test.json' USING com.twitter.elephantbird.pig.load.JsonLoader('-nestedLoad') AS (json:map[]); 
DUMP A;

and the output that i wanted is:

([amount#3,food#Tacos,person#Alice])

Thanks!

Upvotes: 1

Kishore
Kishore

Reputation: 5891

input- pig.json

{"food":"Tacos", "person":"Alice", "amount":3}

script

A = LOAD '/home/kishore/Data/Pig/pig.json' USING JsonLoader('food:chararray,person:chararray,amount:int');
B = foreach A generate food,person,amount;
Dump B;

output

(Tacos,Alice,3)

Upvotes: 0

Related Questions