简体   繁体   English

如何读取Pig中的json数据?

[英]How to read json data in pig?

I have the following type of json file: 我有以下类型的json文件:

{"employees":[
    {"firstName":"John", "lastName":"Doe"},
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"Peter", "lastName":"Jones"}
]}

I am trying to execute the following pig script to load json data 我正在尝试执行以下猪脚本来加载json数据

A = load 'pigdemo/employeejson.json' using JsonLoader ('employees:{(firstName:chararray)},{(lastName:chararray)}');

getting error!! 出错!

Unable to recreate exception from backed error: Error: org.codehaus.jackson.JsonParseException: Unexpected end-of-input: expected close marker for ARRAY (from [Source: java.io.ByteArrayInputStream@1553f9b2; line: 1, column: 1]) at [Source: java.io.ByteArrayInputStream@1553f9b2; 无法从支持的错误重新创建异常:错误:org.codehaus.jackson.JsonParseException:意外的输入结束:预期的ARRAY关闭标记(来自[来源:java.io.ByteArrayInputStream@1553f9b2;行:1,列:1 ]),网址为[Source:java.io.ByteArrayInputStream@1553f9b2; line: 1, column: 29] 行:1,列:29]

First the reason that you see Unexpected end-of-input is because each recode should be in 1 line - like this : 首先,您看到Unexpected end-of-input的原因是因为每次重新编码应在1行中-像这样:

{"employees":[{"firstName":"John", "lastName":"Doe"}, {"firstName":"Anna", "lastName":"Smith"}, {"firstName":"Peter", "lastName":"Jones"}]}

Now - since each row is employees list run the next command 现在-由于每一行都是雇员列表,请运行下一个命令

A = load '$flurryData' using JsonLoader ('employees:bag {t:tuple(firstName:chararray, lastName:chararray)}');
describe A;
dump A;

Give the next output 给出下一个输出

A: {employees: {t: (firstName: chararray,lastName: chararray)}}

({(John,Doe),(Anna,Smith),(Peter,Jones)})

Hope this help ! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM