Hive 自定义 UDTF 函数

本是古典 何须时尚 2021-12-03 05:34 532阅读 0赞
  1. 继承org.apache.hadoop.hive.ql.udf.generic.GenericUDTF
  2. 重写 initialize,process 和 close方法

    1. Hive 调用 initialize 方法来确定传入参数的类型并确定 UDTF 生成表的每个字段的数据类型(即输入类型和输出类型。initialize 方法必须返回一个生成表的字段的相应的 StructObjectInspector。
    2. 初始化完成后,会调用process方法,真正的处理过程在process函数中,在process中,每一次forward()调用产生一行;如果产生多列可以将多个列的值放在一个数组中,然后将该数组传入到forward()函数。
    3. 调用close()方法,对需要清理的方法进行清理。

      1. package com.cloudera.udtf;
      2. import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
      3. import org.apache.hadoop.hive.ql.metadata.HiveException;
      4. import org.apache.hadoop.hive.ql.udf.generic.GenericUDTF;
      5. import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
      6. import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
      7. import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector;
      8. import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
      9. import java.util.ArrayList;
      10. public class UDTFExplode extends GenericUDTF {
      11. @Override
      12. public void close() throws HiveException {
      13. }
      14. @Override
      15. public StructObjectInspector initialize(StructObjectInspector argOIs) throws UDFArgumentException {
      16. ArrayList<String> fieldNames = new ArrayList<String>();
      17. ArrayList<ObjectInspector> fieldOIs = new ArrayList<ObjectInspector>();
      18. fieldNames.add("col1");
      19. fieldOIs.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
      20. fieldNames.add("col2");
      21. fieldOIs.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
      22. return ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames, fieldOIs);
      23. }
      24. @Override
      25. public void process(Object[] args) throws HiveException {
      26. String input = args[0].toString();
      27. String[] test = input.split(";");
      28. for (int i = 0; i < test.length; i++) {
      29. try {
      30. String[] result = test[i].split(":");
      31. forward(result);
      32. } catch (Exception e) {
      33. continue;
      34. }
      35. }
      36. }
      37. }
  3. 打成Jar包,并上传到服务器
  4. 注册UDF函数

    1. hive > add jar /root/hive-demo-0.0.1-SNAPSHOT.jar;
    2. hive > create temporary function split_test as 'com.cloudera.UDTFExplode';
  5. 使用自定义UDTF函数

    1. 直接select中使用

      1. select split_test('asd:123\;rtrt:3445\;vbvx:6787') as (col1,col2) ;
      2. [root@cdh02 ~]# beeline
      3. WARNING: Use "yarn jar" to launch YARN applications.
      4. ...
      5. Beeline version 2.1.1-cdh6.1.1 by Apache Hive
      6. beeline> !connect jdbc:hive2://192.168.1.101:10000
      7. Connecting to jdbc:hive2://192.168.1.101:10000
      8. Enter username for jdbc:hive2://192.168.1.101:10000: hive
      9. Enter password for jdbc:hive2://192.168.1.101:10000:
      10. Connected to: Apache Hive (version 2.1.1-cdh6.1.1)
      11. Driver: Hive JDBC (version 2.1.1-cdh6.1.1)
      12. Transaction isolation: TRANSACTION_REPEATABLE_READ
      13. 0: jdbc:hive2://192.168.1.101:10000> create temporary function split_test as 'com.cloudera.udtf.UDTFExplode';
      14. ...
      15. INFO : OK
      16. No rows affected (0.248 seconds)
      17. 0: jdbc:hive2://192.168.1.101:10000> select split_test('asd:123\;rtrt:3445\;vbvx:6787') as (col1,col2);
      18. ...
      19. INFO : OK
      20. +-------+-------+
      21. | col1 | col2 |
      22. +-------+-------+
      23. | asd | 123 |
      24. | rtrt | 3445 |
      25. | vbvx | 6787 |
      26. +-------+-------+
      27. 3 rows selected (18.492 seconds)

      注意:
      UDTF不可以添加其他字段使用,不可以嵌套调用,不可以和group by,cluster by,distribute by,sort by一起使用。

    2. 和lateral view一起使用

      1. select '1', mytable.col1, mytable.col2 from dual lateral view split_test('asd:123\;rtrt:3445\;vbvx:6787') mytable as col1, col2;
      2. 0: jdbc:hive2://192.168.1.101:10000> select '1',
      3. . . . . . . . . . . . . . . . . . .> mytable.col1,
      4. . . . . . . . . . . . . . . . . . .> mytable.col2
      5. . . . . . . . . . . . . . . . . . .> from test
      6. . . . . . . . . . . . . . . . . . .> lateral view split_test('asd:123\;rtrt:3445\;vbvx:6787')
      7. . . . . . . . . . . . . . . . . . .> mytable
      8. . . . . . . . . . . . . . . . . . .> as col1, col2;
      9. I...
      10. INFO : OK
      11. +------+---------------+---------------+
      12. | _c0 | mytable.col1 | mytable.col2 |
      13. +------+---------------+---------------+
      14. | 1 | asd | 123 |
      15. | 1 | rtrt | 3445 |
      16. | 1 | vbvx | 6787 |
      17. +------+---------------+---------------+
      18. 9 rows selected (21.693 seconds)

发表评论

表情:
评论列表 (有 0 条评论,532人围观)

还没有评论,来说两句吧...

相关阅读

    相关 hive: 定义UDTF

    在hive项目中, 有client和server通信的log日志体系如下,中间的网络传输使用的json格式,所以在server端接收时需要使用阿里的fastJSON来解析为日志

    相关 Hive UDTF

    UDTF:用户自定义表生成函数,表生成函数接受0个或多个输入然后产生多列或多行输出。 例如: select array(1,2,3); 结果为: ![在这里