java使用udf,java udf用于添加列

i am writing java udf function to add the pincode by comparing the locality column.here is my code.

import java.io.IOException;

import org.apache.pig.EvalFunc;

import org.apache.pig.data.Tuple;

import org.apache.commons.lang3.StringUtils;

public class MB_pincodechennai extends EvalFunc

{

private String pincode(String input)

{

String property_pincode = null;

String[] items = new String[]{"600088", "600016", "600053", "600070", "600040", "600106", "632301", "600109", "600083", "600054", "600023", "600095", "600077", "600073", "600003", "603001", "600064", "600094", "600044", "600008",

};

for (String itm : items)

{

if (StringUtils.containsIgnoreCase(input, itm))

{

property_pincode = itm;

break;

}

}

return property_pincode;

}

public String exec(Tuple input) throws IOException

{

if (input == null || input.size() == 0)

return null;

try

{

String str = (String) input.get(0);

return pincode(str);

}

catch (Exception e)

{

return null;

}

}

}

the locality looks like this adyar,tambaram,pallavaram,chromepet...

when i run the above it prints blank values only.i dont know where i am my mistake.any help will be appreciated.

解决方案

if you change the following to return "Invalid Input". then you will get Invalid Input in Pig Console.

catch (Exception e)

{

return null; // Change this to return "Invalid Input"

}

Reason :

Issue is you are trying to pass pincode=600073 (i.e.Integer) from Pig Script.And you are reading it as String in Java UDF. This casting wont work.

MB_pincodechennai(pincode) -- pincode is passed as integer.

For this Issue, you have 2 methods ;

1) Either you can have pincode field as String instead of int in pig script.

2) You can or else parse from Integer to String in Java end before doing the match.

String str = Integer.toString(input);

Please have a look at Mapping for more details on handshakes :

http://pig.apache.org/docs/r0.11.0/udf.html#udf-java

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值