hive使用方式
1.1. 最基本使用方式
啓動一個hive交互shell
bin/hive
hive>
設置一些基本參數,讓hive使用起來更便捷,比如:
1、讓提示符顯示當前庫:
hive>set hive.cli.print.current.db=true;
2、顯示查詢結果時顯示字段名稱:
hive>set hive.cli.print.header=true;
但是這樣設置只對當前會話有效,重啓hive會話後就失效,解決辦法:
在linux的當前用戶目錄中,編輯一個.hiverc文件,將參數寫入其中:
vi .hiverc
set hive.cli.print.header=true; set hive.cli.print.current.db=true; |
1.2. 啓動hive服務使用
啓動hive的服務:
[[email protected]]# bin/hiveserver2 -hiveconf hive.root.logger=DEBUG,console
上述啓動,會將這個服務啓動在前臺,如果要啓動在後臺,則命令如下:
nohup bin/hiveserver2 1>/dev/null2>&1 &
啓動成功後,可以在別的節點上用beeline去連接
v 方式(1)
[root@hdp20-04 hive-1.2.1]# bin/beeline 回車,進入beeline的命令界面
輸入命令連接hiveserver2
beeline> !connectjdbc:hive2//mini1:10000
(hadoop01是hiveserver2所啓動的那臺主機名,端口默認是10000)
v 方式(2)
啓動時直接連接:
bin/beeline -u jdbc:hive2://mini1:10000 -n root
接下來就可以做正常sql查詢了
1.3. 腳本化運行
大量的hive查詢任務,如果用交互式shell來進行輸入的話,顯然效率及其低下,因此,生產中更多的是使用腳本化運行機制:
該機制的核心點是:hive可以用一次性命令的方式來執行給定的hql語句
[root@hdp20-04 ~]# hive -e "insert into table t_dest select* from t_src;"
然後,進一步,可以將上述命令寫入shell腳本中,以便於腳本化運行hive任務,並控制、調度衆多hive任務,示例如下:
vi t_order_etl.sh
#!/bin/bash hive -e "select * from db_order.t_order" hive -e "select * from default.t_user" hql="create table default.t_bash as select * from db_order.t_order" hive -e "$hql" |
如果要執行的hql語句特別複雜,那麼,可以把hql語句寫入一個文件:
vi x.hql
select * from db_order.t_order; select count(1) from db_order.t_user; |
然後,用hive -f /root/x.hql 來執行