使用管道连字符管道将数据加载到 Pig |-|分隔符

Posted

技术标签:

【中文标题】使用管道连字符管道将数据加载到 Pig |-|分隔符【英文标题】:Loading Data to Pig with a pipe hyphen pipe |-| delimiter 【发布时间】:2016-03-06 19:32:12 【问题描述】:

当我尝试将 file.txt 加载到 Pig 时,出现以下错误:

pig script failed to validate: java.lang.RuntimeException: could not instantiate 'PigStorage' with arguments '[\|-\|]'

文件中的示例行是:

文本|-|文本|-|文本

我正在使用以下命令:

bag = LOAD 'file.txt' USING PigStorage('\\|-\\|') AS (v1:chararray, v2:chararray, v3:chararray);

是分隔符吗?我的正则表达式?

【问题讨论】:

看起来 PigStorage 不允许使用正则表达式分隔符。试试PigStorage('|-|') 对不起,先试了,还是抛出: pig script failed to validate: java.lang.RuntimeException: could not instantiate 'PigStorage' with arguments '[|-|]' 是的,所以问题是分隔符只能是一个字节字符,而你有 3:delimiter - the single byte character that is used to separate fields。我发现了一个提示:one can always create a new loadfunc and implement his own parsing (perhaps by extending PigStorage and overriding the parsing bits). 【参考方案1】:

如果您不想编写自定义 LOAD 函数,您可以使用“-”作为分隔符加载记录,然后添加另一个步骤来替换所有“|”在你的领域。

bag = LOAD 'file.txt' USING PigStorage('-') AS (v1:chararray, v2:chararray, v3:chararray);
bag_new =  FOREACH bag GENERATE 
                    REPLACE(v1,'|','') as v1_new,
                    REPLACE(v2,'|','') as v2_new,
                    REPLACE(v3,'|','') as v3_new;

【讨论】:

以上是关于使用管道连字符管道将数据加载到 Pig |-|分隔符的主要内容,如果未能解决你的问题,请参考以下文章

我可以将管道分隔的文本文件直接加载到 Redshift

当行的管道数多于列数时,SSIS管道分隔文件不会失败?

在 Windows 上使用 bq load 和 python 子进程分隔的加载管道

带有管道分隔文件的 Netezza 合并表

Bigquery - 在 CSV(联合表)中处理双引号和管道字段分隔符

如何使用管道分隔符导出到 .csv