Load JSON array into Pig

白昼怎懂夜的黑 提交于 2020-01-11 12:53:52

问题


I have a json file with the following format

[
  {
    "id": 2,
    "createdBy": 0,
    "status": 0,
    "utcTime": "Oct 14, 2014 4:49:47 PM",
    "placeName": "21/F, Cunningham Main Rd, Sampangi Rama NagarBengaluruKarnatakaIndia",
    "longitude": 77.5983817,
    "latitude": 12.9832418,
    "createdDate": "Sep 16, 2014 2:59:03 PM",
    "accuracy": 5,
    "loginType": 1,
    "mobileNo": "0000005567"
  },
  {
    "id": 4,
    "createdBy": 0,
    "status": 0,
    "utcTime": "Oct 14, 2014 4:52:48 PM",
    "placeName": "21/F, Cunningham Main Rd, Sampangi Rama NagarBengaluruKarnatakaIndia",
    "longitude": 77.5983817,
    "latitude": 12.9832418,
    "createdDate": "Oct 8, 2014 5:24:42 PM",
    "accuracy": 5,
    "loginType": 1,
    "mobileNo": "0000005566"
  }
]

when i try to load the data to pig using JsonLoader class I am getting a error like Unexpected end-of-input: expected close marker for OBJECT

a = LOAD '/user/root/jsoneg/exp.json' USING JsonLoader('id:int,createdBy:int,status:int,utcTime:chararray,placeName:chararray,longitude:double,latitude:double,createdDate:chararray,accuracy:double,loginType:double,mobileNo:chararray');
b = foreach a generate $0,$1,$2;
dump b;

回答1:


I am also faced similar kind of problem sometime back, later i came to know that Pig JSON will not support multiline json format. It will always expect the json input must be in single line.

Instead of native Jsonloader, i suggest you to use elephantbird json loader. It is pretty good for Jsons formats.

You can download the jars from the below link

http://www.java2s.com/Code/Jar/e/elephant.htm

I changed your input format to single line and loaded through elephantbird as below

input.json
{"test":[{"id": 2,"createdBy": 0,"status": 0,"utcTime": "Oct 14, 2014 4:49:47 PM","placeName": "21/F, Cunningham Main Rd, Sampangi Rama NagarBengaluruKarnatakaIndia","longitude": 77.5983817,"latitude": 12.9832418,"createdDate": "Sep 16, 2014 2:59:03 PM","accuracy": 5,"loginType": 1,"mobileNo": "0000005567"},{"id": 4,"createdBy": 0,"status": 0,"utcTime": "Oct 14, 2014 4:52:48 PM","placeName": "21/F, Cunningham Main Rd, Sampangi Rama NagarBengaluruKarnatakaIndia","longitude": 77.5983817,"latitude": 12.9832418,"createdDate": "Oct 8, 2014 5:24:42 PM","accuracy": 5,"loginType": 1,"mobileNo": "0000005566"}]}

PigScript:
REGISTER '/tmp/elephant-bird-hadoop-compat-4.1.jar';
REGISTER '/tmp/elephant-bird-pig-4.1.jar';

A = LOAD 'input.json ' USING com.twitter.elephantbird.pig.load.JsonLoader('-nestedLoad');
B = FOREACH A GENERATE FLATTEN($0#'test');
C = FOREACH B GENERATE FLATTEN($0) AS mymap;
D = FOREACH C GENERATE mymap#'id',mymap#'placeName',mymap#'status';
DUMP D;

Output:
(2,21/F, Cunningham Main Rd, Sampangi Rama NagarBengaluruKarnatakaIndia,0)
(4,21/F, Cunningham Main Rd, Sampangi Rama NagarBengaluruKarnatakaIndia,0)


来源:https://stackoverflow.com/questions/26710438/load-json-array-into-pig

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