Loading Raw JSON into Pig

最后都变了- 提交于 2019-12-09 13:21:08

问题


I have a file where each line is a JSON object (actually, it's a dump of stackoverflow). I would like to load this into Apache Pig as easily as possible, but I am having trouble figuring out how I can tell Pig what the input format is. Here's an example of an entry,

{ 
"_id" : { "$oid" : "506492073401d91fa7fdffbe" }, 
"Body" : "....", 
"ViewCount" : 7351, 
"LastEditorDisplayName" : "Rich B", 
"Title" : ".....", 
"LastEditorUserId" : 140328, 
"LastActivityDate" : { "$date" : 1314819738077 }, 
"LastEditDate" : { "$date" : 1313882544213 }, 
"AnswerCount" : 12, "CommentCount" : 19, 
"AcceptedAnswerId" : 7, 
"Score" : 83, 
"PostTypeId" : "question", 
"OwnerUserId" : 8, 
"Tags" : [ "c#", "winforms" ], 
"CreationDate" : { "$date" : 1217540572667 }, 
"FavoriteCount" : 13, "Id" : 4, 
"ForumName" : "stackoverflow.com" 
}

Is there a way I can load a file where each line is one of the above into Pig without having to specify the schema by hand? Or perhaps a way to automatically generate a schema based on the (possibly nested) keys observed in all objects? If I do need to specify the schema by hand, what would the schema string look like?

Thanks!


回答1:


The quick and easy way: use Twitter's elephantbird project. Inside is a loader called com.twitter.elephantbird.pig.load.JsonLoader. When used directly like so,

A = LOAD '/path/to/data.json' USING com.twitter.elephantbird.pig.load.JsonLoader() as (json:map[]);
B = FOREACH A GENERATE json#'fieldName' AS field_name;

nested elements won't be loaded. However, you can easily fix that (if desired) by changing it to,

A = LOAD '/path/to/data.json' USING com.twitter.elephantbird.pig.load.JsonLoader('-nestedLoad')

Including elephantbird is easy -- simply pull the the project "elephant-bird" with organization "com.twitter.elephantbird" using Maven (or equivalent's) dependency manager, then issuing the usual register command in pig

register 'lib/elephantbird.jar';


来源:https://stackoverflow.com/questions/12644392/loading-raw-json-into-pig

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