我在Apache Pig中有一个简单的关系v
:
dump v;
(151364,[ 'ref'#'R813','highway'#'secondary', 'name:ga'#'Lána Chairdif', 'name'#'Cardiff Lane'],(31015271, 31053762))
(151368,[ 'ref'#'N1', 'oneway'#'yes','designation'#'Buses Only', 'highway'#'trunk', 'motor_vehicle'#'designated', 'name:ga'#'Cearnóg Pharnell Thoir', 'maxspeed'#'30', 'name'#'Parnell Square East'],(389365, 540403072))
(151596,[ 'name:en'#'Liffey', 'boundary'#'administrative', 'name:ga'#'An Life','admin_level'#'8', 'name'#'Liffey', 'waterway'#'river'],(1347749, 1426049020, 1347745, 1426049019, 1347742, 900075612))
(367947,[ 'maxspeed'#'80', 'ref'#'L2223','highway'#'tertiary'],(13259933, 2384217, 335978958))
(367952,['created_by'#'YahooApplet 1.0', 'name'#'Charnwood Avenue', 'highway'#'residential'],(2384386, 25963471, 14949594, 2384385, 6146344, 2384254))
(508603,[ 'ref'#'L3018','highway'#'tertiary', 'maxspeed'#'50', 'name'#'Shelerin Road'],(2854184, 2854168, 335978984, 2853307, 2384254, 335978978, 335978975, 2655735, 2655703, 392675957, 11676198, 920037194, 244531387, 2655952, 11675077))
(727153,[ 'ref'#'N8','highway'#'trunk', 'name'#'Merchants' Quay'],(354153, 453344873))
(727157,['highway'#'unclassified', 'oneway'#'yes', 'maxspeed'#'30', 'name'#'Kyle Street'],(354168, 354167))
(727159,['highway'#'unclassified', 'oneway'#'yes', 'maxspeed'#'30', 'name'#'North Main Street'],(354178, 465226768, 354167, 413995429, 72219131, 685537307, 1232381779, 354164))
(727161,[ 'maxspeed'#'30','highway'#'pedestrian', 'name'#'Maylor Street'],(1486492976, 1515360721, 1515360722, 1515345383, 1515344226, 1515344227, 1515344228, 1515344231))
在@ orangeoctopus的建议中,我尝试使用键名中的任何'
重新生成数据,并且我有这些数据:
(151364,[ ref#'R813', name:ga#'Lána Chairdif', name#'Cardiff Lane',highway#'secondary'],(31015271, 31053762))
(151368,[ motor_vehicle#'designated', name#'Parnell Square East', highway#'trunk', oneway#'yes',designation#'Buses Only', maxspeed#'30', name:ga#'Cearnóg Pharnell Thoir', ref#'N1'],(389365, 540403072))
(151596,[ name:en#'Liffey', boundary#'administrative', waterway#'river', name:ga#'An Life',admin_level#'8', name#'Liffey'],(1347749, 1426049020, 1347745, 1426049019, 1347742, 900075612))
(367947,[highway#'tertiary', maxspeed#'80', ref#'L2223'],(13259933, 2384217, 335978958))
(367952,[ name#'Charnwood Avenue',created_by#'YahooApplet 1.0', highway#'residential'],(2384386, 25963471, 14949594, 2384385, 6146344, 2384254))
(508603,[ maxspeed#'50', ref#'L3018', name#'Shelerin Road',highway#'tertiary'],(2854184, 2854168, 335978984, 2853307, 2384254, 335978978, 335978975, 2655735, 2655703, 392675957, 11676198, 920037194, 244531387, 2655952, 11675077))
(727153,[highway#'trunk', name#'Merchants' Quay', ref#'N8'],(354153, 453344873))
(727157,[ oneway#'yes', maxspeed#'30', name#'Kyle Street',highway#'unclassified'],(354168, 354167))
(727159,[ oneway#'yes', maxspeed#'30', name#'North Main Street',highway#'unclassified' (354178, 465226768, 354167, 413995429, 72219131, 685537307, 1232381779, 354164))
(727161,[highway#'pedestrian', name#'Maylor Street', maxspeed#'30'],(1486492976, 1515360721, 1515360722, 1515345383, 1515344226, 1515344227, 1515344228, 1515344231))
在这两种情况下,v都具有相同的架构/结构:
grunt> describe v;
2012-01-09 22:55:34,271 [main] WARN org.apache.pig.PigServer - Encountered Warning IMPLICIT_CAST_TO_CHARARRAY 1 time(s).
v: {id: int,tags: map[ ],nodes: (null)}
然后我尝试从tags
地图中提取出一个值:
grunt> w = foreach v generate tags#'ref';
dump w;
但它只给我空数据,即使某些元素在这里有数据。
()
()
()
()
()
()
()
()
()
()
我尝试使用旧的“引用”键(根据@orangeoctopus'解决方案)
w = foreach v generate tags#'\'ref\'';
这给了我相同的“空”数据,但没有奏效。 (我还尝试过'
和"
的其他组合,例如"'ref'"
/ '"ref"'
/等。但'\'ref\''
以外的所有组合都是无效的猪拉丁语法} p>
发生了什么事?如果我尝试根据标签值进行过滤(例如filter v by tags#'highway' != ''
),我什么也得不到,这与以上无法从地图中提取数据的问题相吻合,我做错了什么?
答案 0 :(得分:4)
非常棘手!
您的问题是您的文字数据包含单引号。您的字符串不是ref
(长度为3个字符),而是'ref'
(长度为5个字符)。我意识到这一点,因为包含字符串的地图的转储通常没有引号。
因此,您需要键入包含这些引号(您必须使用\
转义它们):
grunt> w = foreach v generate tags#'\'ref\'';
您的另一个选择是更改数据的加载方式,因此不会在字符串本身中包含单引号,并将其删除。 PigStorage
不是免费的,但您可以使用REPLACE
或您自己的UDF来执行此操作。
答案 1 :(得分:3)
您是否也正确加载数据?转储地图后[和之前]之后有一个空格是很奇怪的。
还可以更简单地删除input data中键和值中的所有引号。例如:
输入文件
151364 [ref#R813,highway#secondary]
猪
a = LOAD 'data.txt' AS (id:INT, m:MAP[]);
DUMP a;
b = FOREACH a GENERATE m#'ref';
DUMP b;
输出
(151364,[highway#secondary,ref#R813])
(R813)