Hive目錄及配置文件
# 配置文件
/etc/hive/conf
# 根目錄
/opt/cloudera/parcels/CDH/lib/hive
當(dāng)cm界面里執(zhí)行部署客戶端配置時(shí),如上兩個(gè)conf目錄都會(huì)覆蓋。
JDBC方式訪問Hive
基于HiveServer2服務(wù)。
SecureRT,sz設(shè)置下載目錄,Options-Xmodem/Zmodem-Download。
HiveServer2端口,10000。
# user為提交作業(yè)的賬戶
Connection con = DriverManager.getConnection("jdbc:hive2://cdhslave1:10000/default", "root", "123456");
idea設(shè)置,Settings-Build, Execution, Deployment-Compiler-Java Compiler,Project bytecode version: 1.7
Default Settings-Editor-File Encodings, Project Encoding: UTF-8。
Project Structure, Project, 設(shè)置jdk版本和編譯版本,編譯版本和Settings一致。Libraries, 把lib目錄加進(jìn)工程。Modules, 設(shè)置輸出目錄,Use module compile output path。
調(diào)整配置后,等待索引創(chuàng)建完。
UDF開發(fā)
UDF,一進(jìn)一出。
UDAF,聚集函數(shù),多進(jìn)一出。
UDTF,一進(jìn)多出。
UDF中的evaluate函數(shù)名不可變,可以任意重載。
使用方式:在Hive會(huì)話中add自定義函數(shù)的jar文件,然后創(chuàng)建function,繼而使用函數(shù)。
需求:統(tǒng)計(jì)每個(gè)活動(dòng)頁的流量,獲取活動(dòng)ID。
- Java開發(fā)
- 導(dǎo)出jar
- 使用
add jar /root/project/lib/hive_udf.jar;
create temporary function GetActID as 'com.cloudy.hive.udf.GetActID';
sql中通過GetActID直接使用,用起來和內(nèi)置函數(shù)無區(qū)別。
IDEA中打包,
Project Structure-Artifacts, 有兩類,分別是空的和有依賴包的,Build on make選上。然后Build-Make Project。
hive_udf.jar上傳至/root/project/lib/
# /root/project/rpt_act_visit_daily
touch rpt_act_visit_daily.hql
touch rpt_act_visit_daily.sh
hiveF命令封裝
Q: hive -f不能傳參,hql只能寫在shell腳本里,導(dǎo)致shell腳本內(nèi)容龐大和凌亂。
A: 開發(fā)一個(gè)hiveF的功能,用法同hive -f,但支持傳參,使用格式:
- hiveF aa.sql
- hiveF aa.sql -date 2015-01-02
- hiveF aa.sql -date 2015-01-02 -date1 2015-01-03
其中,aa.sql里通過${date}的方式對接。
hiveF開發(fā)思路,
使用方式,hiveF aa.sql -date 2015-01-02
- 開發(fā)一個(gè)Java應(yīng)用程序讀出aa.sql內(nèi)容存入一個(gè)String變量里。
- 把里面的${date}替換為2015-01-02,并System.out.println出來。
- 在shell腳本里,用str接收2的輸出,執(zhí)行
hive -e $str。
# /root/project
mkdir bin
cd bin
touch hiveF
難點(diǎn),把Java程序的輸出結(jié)果直接放到shell腳本里運(yùn)行。
Java運(yùn)行,參數(shù)傳進(jìn)來,shell腳本里$*接收參數(shù)。參數(shù)是哪里傳的呢?哪里傳給hiveF的呢?是shell腳本rpt_act_visit_daily.sh傳過來的,交給java來處理,打印的str由cmd來接收,再用hive -e執(zhí)行。
#!/bin/sh
. /etc/profile
cmd=`java -jar /root/project/lib/HiveF.jar $*`
echo $cmd
hive -e "$cmd" -i /root/project/bin/init.hql
chmod +x hiveF
vi /etc/profile
export PATH=$PATH:/root/project/bin
source /etc/profile
打包hiveF.jar, Main Class: com.cloudy.hive.hiveF.Main
執(zhí)行,
sh ./rpt_act_visit_daily.sh 2015-08-28