CASE statement in PIG

孤街醉人 提交于 2020-01-06 04:46:06

问题


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

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'

eg: geocode = 44556677

if SUBSTRING(geocode,0,2) is between 00-51, then code=01

hence vertex_code = 014455667700

Below is my script:

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;

I get the below error:

2018-01-29 09:00:40,645 [main] ERROR org.apache.pig.tools.grunt.Grunt - ERROR 1039: (Name: Equal Type: null Uid: null)incompatible types in Equal Operator left hand side:chararray right hand side:int

Help is greatly appreciated.


回答1:


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;


来源:https://stackoverflow.com/questions/48503572/case-statement-in-pig

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!