gpt4 book ai didi

apache - 有没有办法在配置单元 UDF 中获取数据库名称

转载 作者:可可西里 更新时间:2023-11-01 14:51:42 26 4
gpt4 key购买 nike

我正在编写 Hive UDF。

我必须获取数据库的名称(函数部署在其中)。然后,我需要根据数据库环境从 hdfs 访问一些文件。你能帮我看看哪个函数可以帮助从 Hive UDF 运行 HQL 查询吗?

最佳答案

  1. 编写UDF类并准备jar文件
public class MyHiveUdf extends UDF {
public Text evaluate(String text,String dbName) {
if(text == null) {
return null;
} else {
return new Text(dbName+"."+text);
}
}
}
  1. 像下面提到的那样在 hive 查询中使用这个 UDF

hive > 使用 mydb; 好的 耗时:0.454秒

hive> ADD jar /root/MyUdf.jar;
Added [/root/MyUdf.jar] to class path
Added resources: [/root/MyUdf.jar]

hive> create temporary function myUdfFunction as 'com.hiveudf.strmnp.MyHiveUdf';
OK
Time taken: 0.018 seconds

hive> select myUdfFunction(username,current_database()) from users;
Query ID = root_20170407151010_2ae29523-cd9f-4585-b334-e0b61db2c57b
Total jobs = 1
Launching Job 1 out of 1
Number of reduce tasks is set to 0 since there's no reduce operator
Starting Job = job_1491484583384_0004, Tracking URL = http://mac127:8088/proxy/application_1491484583384_0004/
Kill Command = /opt/cloudera/parcels/CDH-5.9.0-1.cdh5.9.0.p0.23/lib/hadoop/bin/hadoop job -kill job_1491484583384_0004
Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 0
2017-04-07 15:11:11,376 Stage-1 map = 0%, reduce = 0%
2017-04-07 15:11:19,766 Stage-1 map = 100%, reduce = 0%, Cumulative CPU 3.12 sec
MapReduce Total cumulative CPU time: 3 seconds 120 msec
Ended Job = job_1491484583384_0004
MapReduce Jobs Launched:
Stage-Stage-1: Map: 1 Cumulative CPU: 3.12 sec HDFS Read: 21659 HDFS Write: 381120 SUCCESS
Total MapReduce CPU Time Spent: 3 seconds 120 msec
OK

mydb.user1
mydb.user2
mydb.user3

Time taken: 2.137 seconds, Fetched: 3 row(s)
hive>

关于apache - 有没有办法在配置单元 UDF 中获取数据库名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43222510/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com