Apache pig 在pig中读取json文件

Apache pig 在pig中读取json文件,apache-pig,Apache Pig,我有三种数据类型 1) 基础数据 2) 数据目录1 3) 数据目录2 -1 foo -2 bar -3 foobar ... and so on 基本数据是格式非常好的json。。 例如: {"id1":"foo", "id2":"bar" ,type:"type1"} {"id1":"foo", "id2":"bar" ,type:"type2"} {"id1":1, "id2":2 ,type:"type1"} {"id1":-1, "id2":-2 ,type:"type2"} 数据

我有三种数据类型

1) 基础数据 2) 数据目录1 3) 数据目录2

-1 foo
-2 bar
-3 foobar
... and so on
基本数据是格式非常好的json。。 例如:

{"id1":"foo", "id2":"bar" ,type:"type1"}
{"id1":"foo", "id2":"bar" ,type:"type2"}
{"id1":1, "id2":2 ,type:"type1"}
{"id1":-1, "id2":-2 ,type:"type2"}
数据目录1

1 foo
2 bar
3 foobar
....
数据目录2

-1 foo
-2 bar
-3 foobar
... and so on
现在,我想要的是。。如果数据类型为1

然后从数据目录1读取id1,从数据目录2读取id2,并分配该整数id。。 如果数据类型为2。。然后从数据目录2中读取id1。。来自数据的id2\u dict1。。并分配相应的ID。。 例如:

{"id1":"foo", "id2":"bar" ,type:"type1"}
{"id1":"foo", "id2":"bar" ,type:"type2"}
{"id1":1, "id2":2 ,type:"type1"}
{"id1":-1, "id2":-2 ,type:"type2"}
等等。。
如何在pig中执行此操作?

注意:上面的示例中的不是有效的json,没有引用
type

假设Pig为0.10及更高版本,则有内置的模式,您可以将模式传递给它并使用它进行加载

data = LOAD 'loljson' USING JsonLoader('id1:chararray,id2:chararray,type:chararray');
并载入口述

dict_1 = LOAD 'data_dict_1' USING PigStorage(' ') AS (id:int, key:chararray);
dict_2 = LOAD 'data_dict_2' USING PigStorage(' ') AS (id:int, key:chararray);
然后根据
类型
值将其拆分

SPLIT data INTO type1 IF type == 'type1', type2 IF type == 'type2';
适当地加入它们

type1_joined = JOIN type1 BY id1, dict_1 BY key;
type1_joined = FOREACH type1_joined GENERATE type1::id1 AS id1, type1::id2 AS id2, type1::type AS type, dict_1::id AS id;

type2_joined = JOIN type2 BY id2, dict_2 BY key;
type2_joined = FOREACH type2_joined GENERATE type2::id1 AS id1, type2::id2 AS id2, type2::type AS type, dict_2::id AS id;
由于模式是相等的,
UNION

final_data = UNION type1_joined, type2_joined;
这就产生了

DUMP final_data;

(foo,bar,type2,-2)
(foo,bar,type1,1)