Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
566 views
in Technique[技术] by (71.8m points)

hadoop - CASE statement in PIG

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...