Hive開發udf函數打包jar文件后,需將jar文件放入hive的運行環境,方法有三。
hive(default) > add jar /opt/module/hive/lib/udf-1.0-SNAPSHOT.jar;
1、進入hive,添加jar,
add jar /opt/module/hive/lib/hiveudf.jar
2、創建一個臨時函數
// create temporary function my_lower as 'com.example.hive.udf.LowerCase';
create temporary function myToLow as 'My_UDF';
3、調用
select myToLow(name) from teacher;
先將http://blog.csdn.net/fjssharpsword/article/details/70265554中重定義的兩個類打包成DefTextInputFormat.jar,並放到/home/hdfs目錄下。
1、方法一:使用add jar命令
1)在hive命令行下執行:add jar /home/hdfs/DefTextInputFormat.jar;
Added [/home/hdfs/DefTextInputFormat.jar] to class path
Added resources: [/home/hdfs/DefTextInputFormat.jar]
該方法每次啟動Hive的時候都要從新加入,退出hive就會失效。
2)驗證,建表語句如下:
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '#'
STORED AS INPUTFORMAT
'com.hive.DefTextInputFormat'
OUTPUTFORMAT
'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION
'hdfs://nameservice-ha/pgw/gz';
查詢后文件和表字段一致。
2、hive-site.xml文件配置hive.aux.jars.path:
配置參考如下:
<property>
<name>hive.aux.jars.path</name>
<value>/opt/module/hive/lib/test.jar,file:///jarpath/test.jar</value>
</property>
create function myToLow as 'My_UDF';
drop function myToLow;
該方法不需要每次啟動Hive執行命令加入,需要配置文件。
3、根目錄${HIVE_HOME}下創建文件夾auxlib,然后將自定義jar文件放入該文件夾中;
該方法方便快捷,不過對於客戶端操作環境就不能執行。
————————————————
版權聲明:本文為CSDN博主「fjssharpsword」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/fjssharpsword/article/details/70271671
1 <dependencies> 2 <dependency> 3 <groupId>org.apache.logging.log4j</groupId> 4 <artifactId>log4j-core</artifactId> 5 <version>2.8.2</version> 6 </dependency> 7 <!-- 輔助模塊包 --> 8 <dependency> 9 <groupId>org.apache.hadoop</groupId> 10 <artifactId>hadoop-common</artifactId> 11 <version>3.1.3</version> 12 </dependency> 13 <!-- 包含mr --> 14 <dependency> 15 <groupId>org.apache.hadoop</groupId> 16 <artifactId>hadoop-client</artifactId> 17 <version>3.1.3</version> 18 </dependency> 19 <!-- hdfs --> 20 <dependency> 21 <groupId>org.apache.hadoop</groupId> 22 <artifactId>hadoop-hdfs</artifactId> 23 <version>3.1.3</version> 24 </dependency> 25 26 <dependency> 27 <groupId>org.apache.hive</groupId> 28 <artifactId>hive-exec</artifactId> 29 <version>1.2.1</version> 30 </dependency> 31 32 <dependency> 33 <groupId>org.apache.hive</groupId> 34 <artifactId>hive-jdbc</artifactId> 35 <version>1.2.1</version> 36 </dependency> 37 38 <dependency> 39 <groupId>junit</groupId> 40 <artifactId>junit</artifactId> 41 <version>4.13.2</version> 42 <scope>test</scope> 43 </dependency> 44 </dependencies>
1 public class My_UDF extends UDF { 2 public String evaluate(String str){ 3 return str.toLowerCase(); 4 } 5 }