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

查看:24
本文介绍了用于添加列的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;
      }
    }
  }

当地看起来像这样 adyar,tambaram,pallavaram,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 Console 中得到 Invalid Input.

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 Script 传递 pincode=600073(即整数).您在 Java UDF 中将其作为字符串读取.此演员表不起作用.

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.

对于这个问题,你有两种方法;1) 您可以将 pincode 字段设为 String 而不是 Pig 脚本中的 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 端从整数解析为字符串.

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天全站免登陆