hive (1)Cli命令


查看命令選項

# hive --help
Usage ./hive <parameters> --service serviceName <service parameters>
Service List: beeline cleardanglingscratchdir cli hbaseimport hbaseschematool help hiveburninclient hiveserver2 hplsql hwi jar lineage llap llapdump llapstatus metastore metatool orcfiledump rcfilecat schemaTool version 
Parameters parsed:
  --auxpath : Auxillary jars 
  --config : Hive configuration directory
  --service : Starts specific service/component. cli is default
Parameters used:
  HADOOP_HOME or HADOOP_PREFIX : Hadoop install directory
  HIVE_OPT : Hive options
For help on a particular service:
  ./hive --service serviceName --help
Debug help:  ./hive --debug --help

選項解釋:

--auxpath  該選項允許用戶指定一個以冒號分割的Java包(jar)。很少有到。
--config 指定配置文件目錄,覆蓋系統默認的配置(${HIVE_HOME}/conf)
--service 啟動服務,cli是默認的服務。
比如,想要啟動一個cli:
# hive --service cli 或者hive
# hive --help --service cli 查看一個服務的參數(這里的服務是cli)

usage: hive
-d,--define <key=value> Variable subsitution to apply to hive
commands. e.g. -d A=B or --define A=B
--database <databasename> Specify the database to use
-e <quoted-query-string> SQL from command line
-f <filename> SQL from files
-H,--help Print help information
--hiveconf <property=value> Use value for given property
--hivevar <key=value> Variable subsitution to apply to hive
commands. e.g. --hivevar A=B
-i <filename> Initialization SQL file
-S,--silent Silent mode in interactive shell
-v,--verbose Verbose mode (echo executed SQL to the
console)

 

hive服務

以上服務名字里面的hiveserver已經被改名成hiveserver2了。使用的時候換成hiveserver2.

hive中的變量和屬性

使用 --define key=value 和 --hivevar key=value 都可以在命令行定義變量,這兩者是等價的。
例如:
# hive --define name=yjt
查看定義的變量:以下兩種方式都可以查看變量值。

hive> set hivevar:name;
hivevar:name=yjt


hive> set name;
name=yjt
那么上述的hivevar是什么呢?這是hive的命名空間,hive會將這項鍵值對放到hivevar這個命名空間內,當然還有其他三種內置命名空間。hiveconf、system、env。

 

hive命名空間

Hive變量內部是以Java字符串的方式存儲的。在使用變量的時候,Hive會先使用變量值替換掉變量引用,然后才將語句提交給處理器。變量替換跟Linux變量替換是一樣的。

在CLI中,可以使用set命令顯示或者修改變量值。

例如:

hive> set;   顯示着四種內置變量的所有值
hive> set env:HOME; 顯示env內置變量的HOME值
hive> set -v; 更詳細的輸出,輸出內容包括了Hadoop中定義的一些屬性。
hive> set hive.execute.engine=tez; 或者 set hiveconf:hive.execute.engine=tez; 使用set關鍵字為變量重新賦值或者定義變量。
hive> set hiveconf:hive.cli.print.current.db=true; 顯示當前數據庫。

注意:在使用或者定義system或者env命名空間的屬性值時,需要指定前綴systemc:或者env:

hive 客戶端命令選項

usage: hive
 -d,--define <key=value>          Variable subsitution to apply to hive
                                  commands. e.g. -d A=B or --define A=B
    --database <databasename>     Specify the database to use
 -e <quoted-query-string>         SQL from command line
 -f <filename>                    SQL from files
 -H,--help                        Print help information
    --hiveconf <property=value>   Use value for given property
    --hivevar <key=value>         Variable subsitution to apply to hive
                                  commands. e.g. --hivevar A=B
 -i <filename>                    Initialization SQL file    
 -S,--silent                      Silent mode in interactive shell
 -v,--verbose                     Verbose mode (echo executed SQL to the
                                  console)

解釋:
-e: 在命令行執行sql,跟mysql一樣。執行完就會退出客戶端。
例如:
# hive -e "select * from user_info";

OK
1 dennis hu CN
2 Json Lv Jpn
3 Mike Lu USA
1 dennis hu CN
2 Json Lv Jpn
3 Mike Lu USA
Time taken: 2.775 seconds, Fetched: 6 row(s)

-S:靜默模式,在輸出的時候可以去掉OK,Time taken等行。
例如:# hive -S -e "select * from user_info";

1 dennis hu CN
2 Json Lv Jpn
3 Mike Lu USA
1 dennis hu CN
2 Json Lv Jpn
3 Mike Lu USA

 

-f:指定sql文件來自於文件而不是命令行。不能與-e同時使用。

在hive shell也可以使用source 文件名 來執行sql腳本。
-i:指定一個文件,當CLI啟動的時候,在提示符出現之前會執行這個文件,默認的文件名是當前用戶家目錄下的.hiverc文件。可以與-e同時使用。對於需要
頻繁執行的命令,比如設置屬性,執行sql、增加對於Hadoop分布式內存進行自定義的hive擴展Java包等
一個.hiverc的例子:

set env:HOME;
set hiveconf:hive.cli.print.current.db=true;

注意:在hiverc文件里面,每一行后面不要忘記分號。

說明:如果想要查看hive的歷史記錄,在當前用戶的家目錄下有一個.hivehistroy文件,例如:

ls ~/.hivehistory   默認只記錄100條。

hive客戶端執行shell命令

想要在hive里面執行shell命令,需要在命令的前面加上!,並且以分號(;)結尾,例如:

hive (default)> !echo "I'm learning hive.";
"I'm learning hive."

注意:Hive CLI中不能使用交互式、管道等命令功能。

hive客戶端使用Hadoop的dfs命令

使用Hadoop的命令時,只需要把前面的hadoop去掉即可

hive (default)> dfs -ls /;
Query returned non-zero code: 1, cause: Permission denied: Principal [name=root, type=USER] does not have following privileges for operation DFS [[ADMIN PRIVILEGE] on Object [type=COMMAND_PARAMS, name=[-ls, /]]]
恩恩。。。。這個報錯,沒權限。如果在hive里面開啟了權限控制,很有可能會導致這個情況。把權限控制去掉,執行:

hive (default)> dfs -ls /;  這個命令等價於在shell 終端執行   hadoop dfs -ls /
Found 10 items
-rw-r--r-- 2 root supergroup 12 2019-06-18 07:17 /aa
drwxr-xr-x - root supergroup 0 2019-06-18 07:17 /aa.out
drwxr----- - root supergroup 0 2019-06-18 08:42 /data1
drwxr-xr-x - root supergroup 0 2019-06-14 08:50 /hbase
drwxr-xr-x - root supergroup 0 2019-06-17 05:45 /hive
-rw-r--r-- 2 root supergroup 39904 2019-06-18 03:07 /test
drwxr-xr-x - root supergroup 0 2019-06-19 01:51 /test.out
drwxr-xr-x - root supergroup 0 2019-06-18 01:49 /tez-0.9.0
drwx-wx-wx - root supergroup 0 2019-06-18 11:10 /tmp
drwx------ - root supergroup 0 2019-05-06 07:43 /user

使用 def --help查看支持的選項。

說明:在hive里面執行dfs命令比在shell命令行速度快,這是因為在shell命令行每次執行都需要開啟一個新jvm實例,而在hive里面,會在同一個進程里面執行命令。

hive使用注釋

方法一:直接在命令行使用 -- 進行注釋,hive是不會解析的。

方法二:在腳本中使用 --,通過 -f執行sql的時候,也不會解析。

列如:

hive (default)> --file
hive (default)> -- 123

 顯示字段名稱

在使用sql查詢的時候,如果想要看到字段名稱,如何設置?

可以通過設置hiveconf配置選項hive.cli.print.header為true,默認是關閉的

hive (default)> set hive.cli.print.header;
hive.cli.print.header=false
hive (default)> set hive.cli.print.header=true;

hive (default)> select * from user_info limit 3;
OK
user_info.user_id user_info.firstname user_info.lastname user_info.count
1 dennis hu CN
2 Json Lv Jpn
3 Mike Lu USA
Time taken: 0.114 seconds, Fetched: 3 row(s)

如果想要每次都查看字段名稱,想要永久設置這個值,有兩種方法:
1、在用戶家目錄的.hiverc文件寫入上訴的開啟命令。
2、在hive-site.xml配置

 


免責聲明!

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



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