apache pig如何替换chararray中的所有逗号

s5a0g9ez  于 2021-06-21  发布在  Pig
关注(0)|答案(1)|浏览(433)

我试图替换字符中的所有逗号,如下所示:
输入行示例: 1,compras com cartão, comprei (cp1,cp2,cp3), 206-01-01 00:00:00 输出示例: 1,compras com cartão, comprei (cp1 cp2 cp3), 206-01-01 00:00:00 使用此方法:
raw_data = LOAD 's3://datalake/example' USING org.apache.pig.piggybank.storage.CSVExcelStorage(',','NO_MULTILINE') AS (id:int, transaction:chararray, transaction_name:chararray, date:chararray); apply_cleanness = FOREACH raw_data GENERATE id:int, ransaction:chararray, REPLACE(transaction_name,',','') as transaction_name, date:chararray; 但是这个命令只是删除第一个逗号,结果是: 1,compras com cartão, comprei (cp1 cp2, cp3), 206-01-01 00:00:00 我做错了什么?
谢谢,

ttp71kqs

ttp71kqs1#

第3个字段没有明确的标记。您有2个选项。请用引号将第3个字段括起来,然后使用您的pigscript。

1,compras com cartão, "comprei (cp1,cp2,cp3)", 206-01-01 00:00:00

raw_data = LOAD 's3://datalake/example' USING org.apache.pig.piggybank.storage.CSVExcelStorage(',','NO_MULTILINE') AS (id:int, transaction:chararray, transaction_name:chararray, date:chararray);
apply_cleanness = FOREACH raw_data GENERATE id:int, ransaction:chararray, REPLACE(transaction_name,',','') as transaction_name, date:chararray;

或者,可以使用逗号作为分隔符加载字段,然后生成第3个字段作为加载中3、4、5个字段的组合。请参见下面的

A = LOAD 'test16.txt' USING org.apache.pig.piggybank.storage.CSVExcelStorage(',');
B = FOREACH A GENERATE $0 as id:int,$1 as transaction:chararray,CONCAT(CONCAT(CONCAT(CONCAT($2,' '),$3),' '),$4) as transaction_name:chararray,$5 as date:chararray; 
DUMP B;

相关问题