我有一个以下类型的CDR:
068373748102208100167682477351905149071PLAN1MOCCUST10612287077212:07:1201/01/2012
068373748102208100167682477351905149071PLAN1MTCCUST20600000001312:15:0901/01/2012
068373748102208100167682477351905149071PLAN1SMSCUST10613637193012:18:1801/01/2012
068373748102208100167682477351905149071PLAN1SMSCUST10612899062012:21:0701/01/2012
我必须使用PIG加载此文件,并使用以下架构:
MSIDN:IMSI:IMEI:PLAN:CALL_TYPE:CORRESP_TYPE:CORRESP_ISDN:DURATION:TIME:DATE
我知道每个架构的长度,但我无法找到如何以适当的格式加载数据。这是所需的长度格式,从第一列开始:
13
15
12
5
3
5
11
1
hh:mm:ss
dd/mm/yyyy
答案 0 :(得分:1)
你可以从piggybank看看FixedWidthLoader来加载位置分隔文件(我用它来加载类似你在这里提到的文件)。
例如,我们可以按如下方式指定列位置和列映射
A = LOAD' inputfile.txt'使用org.apache.pig.piggybank.storage.FixedWidthLoader( ' 1-6,7-5',' WRITE_HEADER',' col1:chararray,col2:chararray' );
http://pig.apache.org/docs/r0.12.0/api/org/apache/pig/piggybank/storage/FixedWidthLoader.html
答案 1 :(得分:0)
PigStorage(默认加载和存储功能)不处理此问题 案件。您需要编写自己的加载函数。运用 PigStorage作为一个模型,这应该不会太困难。代替 寻找字段分隔符,你只需解析基于的字段 长度,然后使用标准字符串函数来修剪空白区域。
阅读此http://pig.apache.org/docs/r0.7.0/udf.html#Store+Functions
答案 2 :(得分:0)
一种可能的解决方案是使用普通的猪装载机加载,然后将其传递给UDF以获取列。我会尽力提供代码并在今晚发布。正如所承诺的那样:
]$ more cdr.txt
068373748102208100167682477351905149071PLAN1MOCCUST10612287077212:07:1201/01/2012
068373748102208100167682477351905149071PLAN1MTCCUST20600000001312:15:0901/01/2012
068373748102208100167682477351905149071PLAN1SMSCUST10613637193012:18:1801/01/2012
068373748102208100167682477351905149071PLAN1SMSCUST10612899062012:21:0701/01/2012
]$ more cdr.py
import sys
def mysubstr(input,start,nc):
return input[start:nc]
]$ more cdr.pig
REGISTER 'cdr.py' using jython as mysubstr;
A = LOAD 'cdr.txt' AS (inp:chararray);
B = FOREACH A GENERATE
inp, mysubstr.mysubstr(inp,0,13),
mysubstr.mysubstr(inp,14,29),
mysubstr.mysubstr(inp,30,42);
DUMP B;
输出: (068373748102208100167682477351905149071PLAN1MOCCUST10612287077212:07 1201/01 / 2012,0683737481022,810016768247735,905149071PLA) (068373748102208100167682477351905149071PLAN1MTCCUST20600000001312:15:0901/01 / 2012,0683737481022,810016768247735,905149071PLA) (068373748102208100167682477351905149071PLAN1SMSCUST10613637193012:18:1801/01 / 2012,0683737481022,810016768247735,905149071PLA) (068373748102208100167682477351905149071PLAN1SMSCUST10612899062012:21:0701/01 / 2012,0683737481022,810016768247735,905149071PLA)