hive UDF函數(shù)中讀取mysql配置文件最佳實(shí)踐

上代碼:

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<hive.version>1.1.0</hive.version>

<hadoop.version>2.6.0</hadoop.version>

<mysql.version>5.1.34</mysql.version>

</properties>

<dependencies>

<dependency>

<groupId>org.apache.hive</groupId>

<artifactId>hive-exec</artifactId>

<version>${hive.version}</version>

</dependency>

<dependency>

<groupId>org.apache.hadoop</groupId>

<artifactId>hadoop-common</artifactId>

<version>${hadoop.version}</version>

<exclusions>

<exclusion>

<groupId>jdk.tools</groupId>

<artifactId>jdk.tools</artifactId>

</exclusion>

</exclusions>

</dependency>

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>${mysql.version}</version>

</dependency>

</dependencies>

java代碼:

import java.io.IOException;

import java.net.URI;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.Properties;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.FSDataInputStream;

import org.apache.hadoop.fs.FileSystem;

import org.apache.hadoop.fs.Path;

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


public class GetNextTradeDate extends UDF{


public String evaluate (){

Properties p=null;

try {

p = getPro();

} catch (Exception e1) {

e1.printStackTrace();

}

String DB_URL=p.getProperty("DB_URL");

String JDBC_DRIVER=p.getProperty("JDBC_DRIVER");

String USER=p.getProperty("USER");

String PASS=p.getProperty("PASS");

Connection conn = null;

? ? ? ? Statement stmt = null;

? ? ? ? String init_date = "";

? ? ? ? try{

? ? ? ? ? ? Class.forName(JDBC_DRIVER);

? ? ? ? ? ? conn = DriverManager.getConnection(DB_URL,USER,PASS);

? ? ? ? ? ? stmt = conn.createStatement();

? ? ? ? ? ? String sql = "SELECT date FROM xxx.xxx limit 1";

? ? ? ? ? ? ResultSet rs = stmt.executeQuery(sql);

? ? ? ? ? ? while(rs.next()){

? ? ? ? ? ? init_date= rs.getString("date");

? ? ? ? ? ? }

? ? ? ? ? ? rs.close();

? ? ? ? ? ? stmt.close();

? ? ? ? ? ? conn.close();

? ? ? ? }catch(SQLException se){

? ? ? ? ? ? se.printStackTrace();

? ? ? ? }catch(Exception e){

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }finally{

? ? ? ? ? ? try{

? ? ? ? ? ? ? ? if(stmt!=null) stmt.close();

? ? ? ? ? ? }catch(SQLException se2){

? ? ? ? ? ? }

? ? ? ? ? ? try{

? ? ? ? ? ? ? ? if(conn!=null) conn.close();

? ? ? ? ? ? }catch(SQLException se){

? ? ? ? ? ? ? ? se.printStackTrace();

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? return init_date;

? ? }

private Properties getPro() throws Exception{

????????String dsf= "/user/xxx/mysql-for-hive.properties";

? ? ? ? Configuration conf = new Configuration();

? ? ? ? FileSystem fs = FileSystem.get(URI.create(dsf),conf);

? ? ? ? FSDataInputStream inputStream = fs.open(new Path(dsf));

? ? ? ? Properties p = new Properties();

? ????try {

????? p.load(inputStream);

????? } catch (IOException e) {

????? e.printStackTrace();

????? }

return p;

}

}

將上述代碼導(dǎo)出為jar包。

配置文件mysql-for-hive.properties內(nèi)容如下:

JDBC_DRIVER=com.mysql.jdbc.Driver

DB_URL=jdbc:mysql://192.168.xxx.xxx:3306/xxx

USER=xxxx

PASS=xxxx


這種是創(chuàng)建永久UDF函數(shù),需要把jar包和配置文件上傳hdfs;

hdfs dfs -put -p getNextTradeDate.jar /user/xxx

hdfs dfs -put -p?mysql-for-hive.properties /user/xxx

創(chuàng)建函數(shù)

CREATE FUNCTION getNextTradeDate AS 'com.xxx.xxx.xxx.GetNextTradeDate' USING JAR 'hdfs://xxxx:8020/user/xxxx/getNextTradeDate.jar';

刪除函數(shù)

drop function getNextTradeDate;

測(cè)試函數(shù):

select getNextTradeDate() tradeDate from xxxx.xxxx;

創(chuàng)建臨時(shí)函數(shù)略有不同,臨時(shí)函數(shù)當(dāng)前session有效:

如果你的臨時(shí)函數(shù)沒有輸入,只有輸出,會(huì)被hive優(yōu)化為本地執(zhí)行,不需要集群,這時(shí)候讀取配置文件就可以讀本地,add file后,直接用getResourceAsStream讀本地文件即可,不再采用從集群中讀取配置文件的方式。

讀入配置文件的語句修改如下:

InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("./mysql-for-hive.properties");

然后再Properties p = new Properties();? p.load(inputStream); .........

打jar包上傳,然后打開hive,輸入:

add file /home/mysql-for-hive.properties;

add jar /home/getNextTradeDate.jar;

create temporary function getNextTradeDate as "com.xxx.xxx.xxx.GetNextTradeDate";

select getNextTradeDate() tradeDate from xxxx.xxxx;

有結(jié)果的話就測(cè)試成功。

刪除臨時(shí)函數(shù)

drop temporary function getNextTradeDate;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容