hive自定义函数的使用
# 创建自定义函数(注意,此处的hdfs一定要写成别名OpsCluster1)
## create function 函数名 as 'udf类的全路径(包名+类名)' using jar "自己的jar包存放在hdfs的路径";
hive> create function default.testmd5 as 'com.jd.hive.Md5UDF' using jar 'hdfs://hdfs-name/user/hive/udf/getmd5.jar';
converting to local hdfs://hdfs-name/user/hive/udf/getmd5.jar
Added [/tmp/c7f9b31c-7612-4f88-b199-f267a7062176_resources/getmd5.jar] to class path
Added resources: [hdfs://hdfs-name/user/hive/udf/getmd5.jar]
OK
Time taken: 0.607 seconds
hive>
# 查看自定义函数
hive> show functions like '*testmd5*';
OK
default.testmd5
Time taken: 0.013 seconds, Fetched: 1 row(s)
hive>
# 查看自定义函数的使用文档
hive> describe function default.testmd5;
OK
There is no documentation for function 'default.testmd5'
Time taken: 0.011 seconds, Fetched: 1 row(s)
# 查看自定义函数的描述
hive> desc function default.testmd5;
OK
There is no documentation for function 'default.testmd5'
Time taken: 0.011 seconds, Fetched: 1 row(s)
# 查看自定义函数的详细描述以及文档(示例)
hive> desc function extended default.testmd5;
OK
There is no documentation for function 'default.testmd5'
Time taken: 0.012 seconds, Fetched: 1 row(s)
# 自定义函数的使用
hive> select default.testmd5('12345');
OK
827ccb0eea8a706c4c34a16891f84e7b
Time taken: 2.709 seconds, Fetched: 1 row(s)
# 删除自定义函数
drop function getmd5;
## 相关异常信息(由于之前指定的ip进行上传的包,导致自动切换异常)
hive> drop function default.getmd5;
converting to local hdfs://172.25.179.106:8020/user/hive/udf/getmd5.jar
Failed to read external resource hdfs://172.25.179.106:8020/user/hive/udf/getmd5.jar
OK
Time taken: 0.008 seconds
hive> show functions like '*tmd5*';
OK
default.getmd5
Time taken: 0.022 seconds, Fetched: 1 row(s)
使用beeline客户端连接hiveserver2
beeline
$ beeline -u jdbc:hive2://localhost:10000
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/export/server/hbase-1.2.6/lib/phoenix-4.14.1-HBase-1.2-client.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/export/server/hadoop-2.7.3/share/hadoop/common/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
Connecting to jdbc:hive2://localhost:10000
Connected to: Apache Hive (version 1.2.2)
Driver: Hive JDBC (version 1.2.2)
Transaction isolation: TRANSACTION_REPEATABLE_READ
Beeline version 1.2.2 by Apache Hive
0: jdbc:hive2://localhost:10000> select default.getmd5('12345');
+-----------------------------------+--+
| _c0 |
+-----------------------------------+--+
| 827ccb0eea8a706c4c34a16891f84e7b |
+-----------------------------------+--+
1 row selected (0.831 seconds)
0: jdbc:hive2://localhost:10000>