且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将JSON数组加载到Pig中

更新时间:2022-10-17 23:19:49

我也遇到过类似的问题,后来我才知道Pig JSON不支持多行json格式。它总会期望json输入必须在单行中。

代替原生的Jsonloader,我建议你使用elephantbird json loader。这对Jsons格式非常有用。



您可以从以下链接下载jar包:

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

我将输入格式更改为单行,并通过elephantbird加载,如下所示:

  input.json 
{ test:[{id:2,createdBy:0,status:0,utcTime:2014年10月14日下午4:49:47,placeName:21 / F, Cunningham Main Rd,Sampangi Rama NagarBengaluruKarnatakaIndia,经度:77.5983817,纬度:12.9832418,createdDate:2014年9月16日下午2:59:03 PM,准确度:5,loginType:1, mobileNo:0000005567},{id:4,createdBy:0,status:0,utcTime:2014年10月14日下午4:52:48,placeName: Cunningham Main Rd,Sampangi Rama NagarBengaluruKarnatakaIndia 21 / F,经度:77.5983817,纬度:12.9832418,createdDate:2014年10月8日下午5点24分42秒,准确度:5,loginType :1,mobileNo:0000005566}]}

PigScript:
REGISTER'/ tmp / elephant-bi RD-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;

产量:
(Sampangi市坎宁安主路2,21 / F Rama Nagar班加罗尔卡纳塔克邦印度0)
(Sampangi坎宁安主路4,21楼Rama Nagar班加罗尔卡纳塔克邦印度, 0)


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;

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)