问题描述
我正在尝试根据几个条件从地理编码"中提取顶点代码":
I am trying to extract 'vertex_code' from 'geocode' based on few conditions:
SUBSTRING(geocode,0,2) ----> Code
00-51 ----> 01
70 ----> 03
61-78 ----> 04
Else ----> 00
现在必须将获得的code"值与geocode"值(前缀)连接,并再次与末尾的 00(后缀)连接以形成vertex_code"
Now the obtained 'code' value has to be concatenated with 'geocode' value (prefix) and again concatenated with 00 at the end (suffix) to form the 'vertex_code'
例如:geocode = 44556677
如果 SUBSTRING(geocode,0,2)
在 00-51
之间,则 code=01
if SUBSTRING(geocode,0,2)
is between 00-51
, then code=01
因此顶点代码 = 014455667700
hence vertex_code = 014455667700
下面是我的脚本:
item = load '/user/item.txt' USING PigStorage('|') AS (load_id:chararray, record_type:chararray, geocode:chararray);
newitem = FOREACH item GENERATE load_id, record_type,
(CASE (SUBSTRING(geocode,0,2))
WHEN 00-51 THEN 'CONCAT(01,CONCAT(geocode,00))'
WHEN 70 THEN 'CONCAT(03,CONCAT(geocode,00))'
WHEN 61-78 THEN 'CONCAT(04,CONCAT(geocode,00))'
ELSE 'CONCAT(00,CONCAT(geocode,00))'
END) AS vertex_code;
DUMP newitem;
我收到以下错误:
2018-01-29 09:00:40,645 [main] 错误 org.apache.pig.tools.grunt.Grunt -ERROR 1039: (Name: Equal Type: null Uid: null) Equal 中的类型不兼容运算符左侧:chararray 右侧:int
非常感谢您的帮助.
推荐答案
你必须把它转换成 int 然后比较
You have to cast it to int and then compare
newitem = FOREACH item GENERATE load_id, record_type,
(CASE
WHEN ((int)SUBSTRING(geocode,0,2) <= 51 THEN CONCAT('01',CONCAT(geocode,'00'))
WHEN ((int)SUBSTRING(geocode,0,2) = 70 THEN CONCAT('03',CONCAT(geocode,'00'))
WHEN ((int)SUBSTRING(geocode,0,2) >= 61 and ((int)SUBSTRING(geocode,0,2) <=78 THEN CONCAT('04',CONCAT(geocode,'00'))
ELSE CONCAT('00',CONCAT(geocode,'00'))
END) AS vertex_code;
DUMP newitem;
这篇关于PIG 中的 CASE 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!