[关闭]
@awsekfozc 2015-12-03T23:04:38.000000Z 字数 1545 阅读 1928

Hive UDF

Hive

创建maven工程

QQ截图20151203215052.png-32.3kB

pom.xml添加依赖包

  1. <!-- Hive Client -->
  2. <dependency>
  3. <groupId>org.apache.hive</groupId>
  4. <artifactId>hive-jdbc</artifactId>
  5. <version>${hive.version}</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.apache.hive</groupId>
  9. <artifactId>hive-exec</artifactId>
  10. <version>${hive.version}</version>
  11. </dependency>

继承UDF类

QQ截图20151203215309.png-16kB

实现evaluate方法

  1. package bigdata.hadoop.hive.bigdata.hadoop.hive.udf;
  2. import org.apache.hadoop.hive.ql.exec.UDF;
  3. import org.apache.hadoop.io.Text;
  4. public class RemoveQuotes extends UDF{
  5. public Text evaluate(Text inputValue){
  6. return new Text();
  7. }
  8. }

导出jar

QQ截图20151203220857.png-55.8kB

jar包加载到hive

  1. hive (testdb)> add jar /opt/datas/udf_removequotes.jar;
  2. Added /opt/datas/udf_removequotes.jar to class path
  3. Added resource: /opt/datas/udf_removequotes.jar

注册udf

  1. hive (testdb)> create temporary function zc_removequotes as 'bigdata.hadoop.hive.bigdata.hadoop.hive.udf.RemoveQuotes';
  2. OK
  3. Time taken: 0.011 seconds

测试

QQ截图20151203222125.png-47.7kB

code

  1. package bigdata.hadoop.hive.bigdata.hadoop.hive.udf;
  2. import org.apache.commons.lang.StringUtils;
  3. import org.apache.hadoop.hive.ql.exec.UDF;
  4. import org.apache.hadoop.io.Text;
  5. public class RemoveQuotes extends UDF{
  6. public Text evaluate(Text inputValue){
  7. if(null == inputValue){
  8. return null;
  9. }
  10. if(StringUtils.isBlank(inputValue.toString())){
  11. return null;
  12. }
  13. String str = inputValue.toString();
  14. String reStr = str;
  15. if(str.length()>1){
  16. if ("\"".equals(str.substring(0,1))) {
  17. reStr = reStr.substring(1);
  18. }
  19. if ("\"".equals(str.substring(str.length()-1))) {
  20. reStr = reStr.substring(0,reStr.length()-1);
  21. }
  22. }
  23. return new Text(reStr);
  24. }
  25. public static void main(String[] args) {
  26. // TODO Auto-generated method stub
  27. System.out.println(new RemoveQuotes().evaluate(new Text("\"1\"")));
  28. }
  29. }

在此输入正文

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注