hive命令的3種調用方式
方式1:hive –f /root/shell/hive-script.sql(適合多語句)
hive-script.sql類似於script一樣,直接寫查詢命令就行
不進入交互模式,執行一個hive script
這里可以和靜音模式-S聯合使用,通過第三方程序調用,第三方程序通過hive的標准輸出獲取結果集。
$HIVE_HOME/bin/hive -S -f /home/my/hive-script.sql (不會顯示mapreduct的操作過程)
那么問題來了:如何傳遞參數呢?
demo如下:
start_hql.sh 內容:
#!/bin/bash
# -S 打印輸出mapreduce日志
hive \
-hivevar id=1 \
-hivevar col2=2 \
-S -f test.sql
test.sql 內容:
-- 數據庫
use tmp;
-- 表名
select *
from tmp_jzl_20140725_test11
where
id='${hivevar:id}' and col2='${hivevar:col2}';
方式2:hive -e 'sql語句'(適合短語句)
直接執行sql語句
例如:
[root@cloud4 shell]# hive -e 'select * from t1'
靜音模式:
[root@cloud4 shell]# hive -S -e 'select * from t1' (用法與第一種方式的靜音模式一樣,不會顯示mapreduce的操作過程)
此處還有一亮點,用於導出數據到linux本地目錄下
例如:
[root@cloud4 shell]# hive -e 'select * from t1' > test.txt
有點類似pig導出分析結果一樣,都挺方便的
方式3:hive (直接使用hive交互式模式)
都挺方便的
介紹一種有意思的用法:
1.sql的語法
#hive 啟動
hive>quit; 退出hive
hive> show databases; 查看數據庫
hive> create database test; 創建數據庫
hive> use default; 使用哪個數據庫
hive>create table t1 (key string); 創建表
對於創建表我們可以選擇讀取文件字段按照什么字符進行分割
例如:
hive>create table t1(id ) '/wlan'
partitioned by (log_date string) 表示通過log_date進行分區
row format delimited fields terminated by '\t' 表示代表用‘\t’進行分割來讀取字段
stored as textfile/sequencefile/rcfile/; 表示文件的存儲的格式
