在Pig中解析复杂的嵌套JSON
我想将亿万富翁JSON数据集解析为Pig。可以找到JSON文件 以下是每个条目的内容:在Pig中解析复杂的嵌套JSON,json,hadoop,apache-pig,elephantbird,Json,Hadoop,Apache Pig,Elephantbird,我想将亿万富翁JSON数据集解析为Pig。可以找到JSON文件 以下是每个条目的内容: { "wealth": { "worth in billions": 1.2, "how": { "category": "Resource Related", "from emerging": true, "industry": "Mining and metals",
{
"wealth": {
"worth in billions": 1.2,
"how": {
"category": "Resource Related",
"from emerging": true,
"industry": "Mining and metals",
"was political": false,
"inherited": true,
"was founder": true
},
"type": "privatized and resources"
},
"company": {
"sector": "aluminum",
"founded": 1993,
"type": "privatization",
"name": "Guangdong Dongyangguang Aluminum",
"relationship": "owner"
},
"rank": 1372,
"location": {
"gdp": 0.0,
"region": "East Asia",
"citizenship": "China",
"country code": "CHN"
},
"year": 2014,
"demographics": {
"gender": "male",
"age": 50
},
"name": "Zhang Zhongneng"
}
尝试1
我尝试在grunt中使用以下命令加载此数据:
亿万富翁=使用JsonLoader('财富:
(价值数十亿美元:两倍,如何:(类别:chararray,来自
新兴:查拉雷,工业:查拉雷,是政治:查拉雷,
继承人:chararray,创始人:chararray),类型:chararray),公司:
(部门:chararray,成立时间:int,类型:chararray,名称:chararray,关系:chararray),等级:int,地点:(gdp:双,地区:chararray,国籍:chararray,国家
代码:chararray),年份:int,人口统计:(性别:chararray,年龄:int),
名称:chararray);
然而,这给了我一个错误:
错误org.apache.pig.tools.grunt.grunt-错误1200:输入“in”不匹配,应为右参数
尝试2
接下来,我尝试使用Twitter的elephantbird项目的加载程序com.Twitter.elephantbird.pig.load.JsonLoader
。是此UDF的代码。这就是我所做的:
billionaires = LOAD 'billionaires.json' USING com.twitter.elephantbird.pig.load.JsonLoader('-nestedLoad') AS (json:map[]);
names = foreach billionaires generate json#'name' AS name;
dump names;
现在这运行,我没有得到错误!但是什么也没有显示出来。我得到如下输出:
输入:从以下位置成功读取0条记录(1445335字节):
"hdfs://localhost:9000/user/purak/billionaires.json"
输出:已成功将0条记录存储在:
"hdfs://localhost:9000/tmp/temp-1399280624/tmp-477607570“
计数器:写入的记录总数:0写入的字节总数:0可溢出
内存管理器溢出计数:0主动溢出的行李总数:0
主动溢出的记录:0
Job DAG:Job_1478889184960_0005
我做错了什么?这可能不是最好的方法,但这就是我最终要做的:
[{{u comment:“first entry”…},{u comment:“second entry”…}
。但Pig中的JsonLoader将每一行换行作为一个新条目。为了使json文件以换行符分隔,而不是使用命令行json处理器的逗号。使用sudo apt get Install js
安装它,然后运行cat亿万富翁.json | jq-c.[]>newBillionaires.json
copyFromLocal/home/purak/Desktop/newBillionaires.json/user/purak
description.airesfinal检查结构一次代码>:
这就是我在Pig中想要的数据结构!现在我可以继续分析数据集了:)这可能不是最好的方法,但这就是我最终要做的:
[{{u comment:“first entry”…},{u comment:“second entry”…}
。但Pig中的JsonLoader将每一行换行作为一个新条目。为了使json文件以换行符分隔,而不是使用命令行json处理器的逗号。使用sudo apt get Install js
安装它,然后运行cat亿万富翁.json | jq-c.[]>newBillionaires.json
copyFromLocal/home/purak/Desktop/newBillionaires.json/user/purak