java udf用于添加列 [英] java udf for adding columns

查看:83
本文介绍了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<String>
  {
    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;
      }
    }
  }

这个地方看起来像是阿达亚尔,坦巴拉姆,帕拉瓦拉姆,chromepet ...

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.

推荐答案

如果更改以下内容以返回无效输入".那么您将在Pig控制台中看到无效输入.

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"
}

原因:

问题是您正在尝试从Pig脚本传递pincode = 600073(即整数),并且正在Java UDF中将其读取为String.这种转换无法正常工作.

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.

对于此问题,您有2种方法; 1)可以在pin脚本中将pincode字段设置为String而不是int.

For this Issue, you have 2 methods ; 1) Either you can have pincode field as String instead of int in pig script.

2)在进行匹配之前,您可以将Java末尾从Integer解析为String.

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

String str = Integer.toString(input);

有关握手的更多详细信息,请参阅映射": http://pig.apache.org/docs/r0.11.0/udf.html#udf-java

Please have a look at Mapping for more details on handshakes : http://pig.apache.org/docs/r0.11.0/udf.html#udf-java

这篇关于java udf用于添加列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆