天天看點

Hive指令行介紹

簡介

$HIVE_HONE/bin/hive指令工具是與Hive服務互動的最常用的方式,是學習和熟悉Hive重要的工具。本文總結了Hive指令行的常見用法,能夠幫助讀者快速了解和學習Hive。

hive指令選項

在配置Hive時,已經将$HIVE_HONE/bin加入PATH中,使用者隻要在shell提示符中輸入hive,就可以在shell環境中找到這個指令。

[email protected]:~$ hive --help
Usage ./hive <parameters> --service serviceName <service parameters>
Service List: beeline cleardanglingscratchdir cli hbaseimport hbaseschematool help hiveburninclient hiveserver2 hplsql hwi jar lineage llapdump llap 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參數允許使用者指定一個以冒号分割的"附屬的"JAR,用來支援使用者自定義的擴充。

--config檔案目錄,允許使用者用于覆寫$HIVE_HOME/conf中的預設的屬性配置,而指向一個新的配置檔案目錄

--service用于指定具體的服務,幫助資訊中給出了支援的服務清單,預設為cli。

指令行界面

CLI選項

下面的指令顯示了CLI所提供的選項清單:

[email protected]:~$ hive --service cli --help
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中屬性和變量命名空間:

命名空間 使用權限 描述
hivevar rw 使用者自定義變量
hiveconf rw Hive相關的配置屬性
system rw Java定義的配置屬性
env r shell環境定義的環境變量

了解命名空間,再來分析相關的CLI屬性選項。--define key=value和--hivevar key=value是等價的,都是向hivevar命名空間中添加屬性。--hiveconf key=value用于設定Hive相關的配置屬性。

在CLI中,可以使用SET指令顯示或者修改變量的值,在CLI查詢語句中引用變量需要使用${命名空間:屬性名}的方式,實際查詢中的變量引用會先被替換掉然後才會送出給查詢處理器。字首hivevar:是可選的。

[email protected]:~$ hive --define foo=bar
hive> set foo;
foo=bar
hive> set hivevar:foo;
hivevar:foo=bar
hive> create table toss1 (i int, ${hivevar:foo} string);
OK
Time taken: 2.535 seconds
hive> desc toss1;
OK
i                       int
bar                     string
Time taken: 0.392 seconds, Fetched: 2 row(s)
           

設定屬性時有一個需要注意的地方,就是屬性優先級層次的問題,按照優先級由高到低的順序排列: hive set指令、指令行--hiveconf選項、hive-site.xml、hive-default.xml、hadoop-site.xml(或等價的core-site.xml、hdfs-site.xml、mapred-site.xml、yarn-site.xml)。有時在排查問題時會用到。

”一次執行“指令

如果使用者有時期望執行一個或多個查詢(分号分割),執行結束後hive CLI立即退出,這時需要使用-e參數。這裡需要注意的是,Hive會将結果輸出的标準輸出中。

[email protected]:~$ hive -e "set" | grep warehouse
hive.metastore.warehouse.dir=hdfs://localhost:9000/user/hive/warehouse
hive.warehouse.subdir.inherit.perms=true
           

執行檔案中Hive查詢

-f選項允許hive執行指定檔案中的一個或者多個查詢語句。按照慣例,一般把這些查詢語句儲存在以.q或.hql為字尾的檔案中。該功能等同于在shell中使用source指令來執行一個腳本檔案。

$ hive -f /home/hadoop/withqueries.hql
$ hive
hive >  source /home/hadoop/withqueries.hql
           

hiverc檔案

-i選項允許使用者指定一個檔案,當CLI啟動時,在提示符出現前執行這個檔案。Hive會自動在HOME目錄下尋找名為.hiverc的檔案,且自動執行這個檔案中的指令。對于使用者需要頻繁使用的屬性,使用這個檔案非常友善。

自動不全功能

如果使用者在輸入過程中敲擊Tab制表鍵,那麼CLI會自動不全可能的關鍵字或者函數名。

執行shell指令

使用者不需要退出CLI就可以執行簡單的bash shell指令,隻要在指令前加上!并且以分号(;)結束即可。

參考資料

1. Hive程式設計指南

繼續閱讀