java版hive的UDF

版权声明:本文为博主九师兄(QQ群:spark源代码 198279782 欢迎来探讨技术)原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_21383435/article/details/81773335

1.生产测试数据

# vim /Users/lcc/IdeaProjects/AllTest/data/hive/udf/littlebigdata.txt
lcc,lcc@123.com,2-12-1981,209.191.139.200,M,10
xjj,xjj@rsa.com,10-10-2004,10.10.10.1,M,50
ser,ser@dfg.com,4-5-1974,64.64.5.1,F,2

2.开启hive

lcc@lcc ~$ hive --service metastore
lcc@lcc ~$ hive --service hiveserver2

3.远程连接hive(可以直接hive)

lcc@lcc ~$ beeline -u "jdbc:hive2://lcc:10013"
0: jdbc:hive2://lcc:10013> use lccdb;
No rows affected (0.179 seconds)
0: jdbc:hive2://lcc:10013> create table if not exists litt(name string,email string,body string,ip string,gendr string ,anum int) row format delimited fields terminated by ',';
0: jdbc:hive2://lcc:10013> load data local inpath '/Users/lcc/IdeaProjects/AllTest/data/hive/udf/littlebigdata.txt';
: jdbc:hive2://lcc:10013> select * from litt;
+------------+--------------+-------------+------------------+-------------+------------+--+
| litt.name  |  litt.email  |  litt.body  |     litt.ip      | litt.gendr  | litt.anum  |
+------------+--------------+-------------+------------------+-------------+------------+--+
| lcc        | [email protected]  | 2-12-1981   | 209.191.139.200  | M           | 10         |
| xjj        | [email protected]  | 10-10-2004  | 10.10.10.1       | M           | 50         |
| ser        | [email protected]  | 4-5-1974    | 64.64.5.1        | F           | 2          |
+------------+--------------+-------------+------------------+-------------+------------+--+
3 rows selected (0.618 seconds)
0: jdbc:hive2://lcc:10013>

4.编写代码

package com.hive.udf.example;

import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;

/**
 * Created by lcc on 2018/8/17.
 */
public class HelloUDF extends UDF {

    public Text evaluate(Text body){

        return new Text("hello"+body);
    }

    public static void main(String[] args){
        HelloUDF udf = new HelloUDF();
        Text result = udf.evaluate(new Text("张三"));
        System.out.println(result.toString());
    }

}

5.编译

lcc@lcc hive_test$ mvn clean package

6.注册临时函数UDF

0: jdbc:hive2://lcc:10013> add jar /Users/lcc/IdeaProjects/AllTest/hive_test/target/hive_test-1.0-SNAPSHOT.jar;
0: jdbc:hive2://lcc:10013> list jars;
+------------------------------------------------------------------------------+--+
|                                   resource                                   |
+------------------------------------------------------------------------------+--+
| /Users/lcc/IdeaProjects/AllTest/hive_test/target/hive_test-1.0-SNAPSHOT.jar  |
+------------------------------------------------------------------------------+--+
3 rows selected (0.017 seconds)
0: jdbc:hive2://lcc:10013>
0: jdbc:hive2://lcc:10013> create temporary function say_hello as 'com.hive.udf.example.HelloUDF';
0: jdbc:hive2://lcc:10013> show functions like 'say*';
+------------+--+
|  tab_name  |
+------------+--+
| say_hello  |
+------------+--+
1 row selected (0.084 seconds)
0: jdbc:hive2://lcc:10013>
0: jdbc:hive2://lcc:10013> select name,body,say_hello(body) from litt;
+-------+-------------+------------------+--+
| name  |    body     |       _c2        |
+-------+-------------+------------------+--+
| lcc   | 2-12-1981   | hello2-12-1981   |
| xjj   | 10-10-2004  | hello10-10-2004  |
| ser   | 4-5-1974    | hello4-5-1974    |
+-------+-------------+------------------+--+
3 rows selected (0.51 seconds)
0: jdbc:hive2://lcc:10013>

猜你喜欢

转载自blog.csdn.net/qq_21383435/article/details/81773335