如何计算Apache Pig中字符串的长度?

时间:2015-02-25 11:44:29

标签: apache-pig

在pig中,substring函数有三个参数,我需要从第4个位置到字符串长度的子字符串。

所以我指定了:

substring(division,4,string.length(division))

显示错误Could not resolve string.LENGTH using imports:。 如何找到猪的弦长?

4 个答案:

答案 0 :(得分:5)

您可以使用SIZE

SUBSTRING(分裂,4,SIZE(除法))

答案 1 :(得分:0)

SIZE()是内部调用String.length()

的函数

所以在你的情况下(如弗雷德里克所说):

SUBSTRING(division,4,SIZE(division))

答案 2 :(得分:0)

之前提供的回复并没有为我提供帮助。有用的是:

SUBSTRING(division,4,(int)SIZE(division))

注意int演员。

答案 3 :(得分:0)

我也得到同样的错误。在尝试了将近1天后,我找到了解决方案,解决方案是,你应该做以下代码,

REGISTER '/usr/local/piggybank.jar';
maindata = LOAD '/user/pig/storm/StormEvents_details-ftp_v1.0_d1950_c20170120.csv' using org.apache.pig.piggybank.storage.CSVExcelStorage(',','NO_MULTILINE','UNIX','SKIP_INPUT_HEADER');

B = foreach maindata generate (int)$20 as INJURIES_DIRECT,(int)$21 as INJURIES_INDIRECT,(int)$22 as DEATHS_DIRECT,(int)$23 as DEATHS_INDIRECT,(chararray)$24 as DAMAGE_PROPERTY,(int)$10 as YEAR,(chararray)$11 as MONTH_NAME,(int)$0 as BEGIN_YEARMONTH;
k = foreach B generate org.apache.pig.piggybank.evaluation.string.LENGTH(DAMAGE_PROPERTY) as len; 

所以在这里,如果你能看到这些代码,你必须包括你的字段代替DAMAGE_PROPERTY。所以解决我们的问题是,

org.apache.pig.piggybank.evaluation.string.LENGTH(DAMAGE_PROPERTY) as len; 

现在对于子字符串,您可以执行这些类型的代码,您可以相应地更改它的字符串名称,

substringdata = foreach B generate SUBSTRING(DAMAGE_PROPERTY,0,org.apache.pig.piggybank.evaluation.string.LENGTH(DAMAGE_PROPERTY)-1) as substr; 

我认为应该解决问题。