hive 連接及使用(三)


1. 連接

有三種方式連接 hive

  • cli:直接輸入 bin/hive 就可以進入 cli
  • hiveserver2、beeline
  • webui

1.1 hiveserver2/beeline

1、開啟 hiveserver2 服務

// 前台運行,當 beeline 輸入命令時,服務端會返回 OK
[root@hadoop1 bin]# ./hiveserver2
OK

// 后台運行,1:表示標准日志輸出、2:表示錯誤日志輸出 如果我沒有配置日志的輸出路徑,日志會生成在當前工作目錄,默認的日志名稱叫做: nohup.xxx
nohup hiveserver2 1>/home/hadoop/hiveserver.log 2>/home/hadoop/hiveserver.err &
或者:nohup hiveserver2 1>/dev/null 2>/dev/null &
或者:nohup hiveserver2 >/dev/null 2>&1 &

2、啟動 beeline 客戶端連接

[hadoop@hadoop1 bin]$ ./beeline

// 這里為 hadoop 的用戶名
beeline> !connect jdbc:hive2://hadoop1:10000
Connecting to jdbc:hive2://hadoop1:10000
Enter username for jdbc:hive2://hadoop1:10000: hadoop
Enter password for jdbc:hive2://hadoop1:10000: ******
Connected to: Apache Hive (version 1.2.1)
Driver: Hive JDBC (version 1.2.1)
Transaction isolation: TRANSACTION_REPEATABLE_READ
0: jdbc:hive2://hadoop1:10000> show databases;
+----------------+--+
| database_name  |
+----------------+--+
| default        |
| hive_1         |
+----------------+--+
2 rows selected (4.183 seconds)
0: jdbc:hive2://hadoop1:10000>

// 指定用戶名連接
beeline -u jdbc:hive2://hadoop1:10000 -n hadoop

參考文章:Hive學習之路 (四)Hive的連接3種連接方式

2. 交互式命令

// -e 不進入hive的交互窗口執行sql語句
bin/hive -e "select id from student;"

// -f 執行腳本中的 sql 語句,hivef.sql 語句:select *from student;
bin/hive -f /opt/module/datas/hivef.sql
bin/hive -f /opt/module/datas/hivef.sql  > /opt/module/datas/hive_result.txt

// 退出
exit、quit

// 查看 hdfs 文件系統
dfs -ls /;

// 查看本地文件系統
! ls /opt/module/datas;

// 查看在hive中輸入的所有歷史命令,一般為當前用戶的根目錄 /root 或 /home 目錄 
cat /home/hadoop/.hivehistory

// 其他常用命令
show databases;
show tables;
drop table tableName;
desc tableName;		// 查看表結構
use default;		// 使用數據庫

3. 常見屬性配置

3.1 數據倉庫位置

Default 數據倉庫的最原始位置是在 hdfs 上的:/user/hive/warehouse 路徑下,修改位置:

<!--hive-default.xml.template 拷貝到 hive-site.xml文件中-->

<property>
<name>hive.metastore.warehouse.dir</name>
<value>/user/hive/warehouse</value>
<description>location of default database for the warehouse</description>
</property>

配置完后,需要修改同組用戶權限:

bin/hdfs dfs -chmod g+w /user/hive/warehouse

注意:重啟 hive cli 才會生效

3.2 修改查詢結果顯示信息

1、新建一張表 student,並插入數據:

// 以 \t 作為分隔符
[hadoop@hadoop1 apps]$ vim my_code/student.txt

// 檢查分隔符
[hadoop@hadoop1 apps]$ cat -T my_code/student.txt	
1001^Izhangshan
1002^Ilishi
1003^Izhaoliu

// 創建一張表 student,數據以 \t 作為分隔符
hive> create table student(id int, name string) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';
OK
Time taken: 0.463 seconds

// 從本地導入數據
hive> load data local inpath '/home/hadoop/apps/my_code/student.txt' into table student;
Loading data to table hive_1.student
Table hive_1.student stats: [numFiles=1, totalSize=39]
OK
Time taken: 0.846 seconds

// 查詢(發現沒有顯示具體列名等信息)
hive> select * from student;
OK
1001    zhangshan
1002    lishi
1003    zhaoliu
Time taken: 0.229 seconds, Fetched: 3 row(s)

2、修改 hive-site.xml文件中添加如下配置信息:

<property>
	<name>hive.cli.print.header</name>
	<value>true</value>
</property>

<property>
	<name>hive.cli.print.current.db</name>
	<value>true</value>
</property>

3、重啟 hive

// 顯示列名
hive (hive_1)> select * from student;
OK
student.id      student.name
1001    zhangshan
1002    lishi
1003    zhaoliu
Time taken: 1.636 seconds, Fetched: 3 row(s)

3.3 Hive 運行日志信息配置

1、默認日志路徑:/tmp/hadoop/hive.log

2、修改 hive-log4j.properties

[hadoop@hadoop1 apps]$ cd hive/conf/
[hadoop@hadoop1 conf]$ ls
beeline-log4j.properties.template  hive-env.sh           hive-exec-log4j.properties.template  hive-site.xml
hive-default.xml.template          hive-env.sh.template  hive-log4j.properties.template       ivysettings.xml
[hadoop@hadoop1 conf]$ cp hive-log4j.properties.template hive-log4j.properties
[hadoop@hadoop1 conf]$ vim hive-log4j.properties

// 修改日志路徑
hive.log.dir=/home/hadoop/apps/hive/logs

3、重啟 hive

3.4 參數配置方式

參數配置有三種方式:

  • 修改配置文件:對所有會話有效
  • 命令行參數:僅對本次會話有效,即退出 cli 就失效
  • 參數聲明:上同

優先級:配置文件 < 命令行參數 < 參數聲明

系統級的參數,log4j,必須用前兩種方式設定,因為參數的讀取在會話建立之前就完成了,推薦使用第一種方式

配置文件

  • 默認配置文件:hive-default.xml

  • 用戶自定義配置文件:hive-site.xml

注意:用戶自定義配置會覆蓋默認配置,另外 hive 配置會覆蓋 hadoop 配置,因為它會讀取 hadoop 配置

命令行參數

即在啟動 hive時通過命令行來添加一些參數,如:

// 格式:-hiveconf param=value
bin/hive -hiveconf mapred.reduce.tasks=10;

// 查看配置

hive (default)> set mapred.reduce.tasks;
mapred.reduce.tasks=-1

參數聲明方式

可以在 HQL 中使用 SET 關鍵字設定參數

hive (default)> set mapred.reduce.tasks=100;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM