2012-08-30 53 views
1

我们旨在利用PIG进行服务器日志的大规模日志分析。我需要从一个文件加载一个PIG映射数据类型。PIG加载CSV - 地图类型错误

我试着用下面的数据运行一个示例PIG脚本。

在我的CSV文件中的行,名为 '测试'(由PIG处理)的样子,

151364,[ref#R813,highway#secondary] 

我的PIG脚本

a = LOAD 'test' using PigStorage(',') AS (id:INT, m:MAP[]); 
DUMP a; 

的想法是加载一个int和第二个元素作为散列表。 但是,当我转储时,int字段得到正确解析(并在转储中打印),但未解析映射字段导致解析错误。

有人能解释一下,如果我错过了什么吗?

回答

1

我认为有一个分隔符相关的问题(例如字段分隔符在某种程度上影响了地图字段的解析,或者它与地图分隔符相混淆)。

当使用该输入数据(通知我用分号作为字段分隔符):

151364;[ref#R813,highway#secondary] 
下面

是从我咕噜壳输出:

grunt> a = LOAD '/tmp/temp2.txt' using PigStorage(';') AS (id:int, m:[]); 
grunt> dump a; 
... 
(151364,[highway#secondary,ref#R813]) 

grunt> b = foreach a generate m#'ref'; 
grunt> dump b; 
(R813) 
1

Atlast,我想通解决问题。只要将解除限制器从','换成另一个角色,比如管道。字段分隔符被混淆与用于地图:)

The string 151364,[ref#R813,highway#secondary] was getting parsed into, 
field1: 151364 field2: [ref#R813 field3: highway#secondary] 
Since '[ref#$813' is not a valid map field, there is a parse error. 

我还调查了PigStorage函数的源代码和确认解析逻辑分隔符“” - Source code

@Override 
public Tuple getNext() throws IOException { 
     for (int i = 0; i < len; i++) { 
      //skipping some stuff 
      if (buf[i] == fieldDel) { // if we find delim 
       readField(buf, start, i); //extract field from prev delim to current 
       start = i + 1; 
       fieldID++; 
      } 
     } 
} 

因此,由于PIG按分隔符分割字段,因此会导致分析字段与用于映射的分隔符混淆。