天天看點

Hive學習筆記(三)——shell指令行

1 hive與非互動式模式指令行

  1. hive -e:從指令行執行指定的HQL,不需要分号:
hive -e ‘select * from dumy limit 100’ >a.txt
           
  1. hive -f :執行HQL腳

3) hive -i :進入Hive互動Shell時候先執行腳本中的HQL語句

4)hive -v :備援verbose,額外列印出執行的HQL語句;

5) hive -S:靜默Slient模式,不顯示轉化MR-Job的資訊,隻顯示最終結果

hive -S -e ‘select * from student
           

6)hive --hiveconf <property=value>:使用給定屬性的值

HIVE_HOME/bin/hive --hiveconf mapred.reduce.tasks=2 //啟動時,配置reduce個數2(隻在此session中有效)
           

7)hive --service serviceName:啟動服務

hvie --service hiveserver2
           

8)hive [–database test]:進入CLI互動界面,預設進入default資料庫。加上[]内容直接進入test資料庫。

hive --database test
           

2.Hive的互動模式下指令:

quit / exit

:退出CLI

reset

:重置所有的配置參數,初始化為hive-site.xml中的配置。如之前使用set指令設定了reduce數量。

set <key>=<value>

:設定Hive運作時配置參數,優先級最高,相同key,後面的設定會覆寫前面的設定。

set –v

:列印出所有Hive的配置參數和Hadoop的配置參數。

//找出和"mapred.reduce.tasks"相關的設定
hive -e 'set -v;' | grep mapred.reduce.tasks
           

add

指令:包括add File[S]/Jar[S]/Archive[S] *,向 DistributeCache 中添加一個或過個檔案、jar包、或者歸檔,添加之後,可以在Map和Reduce task中使用。比如,自定義一個udf函數,打成jar包,在建立函數之前,必須使用add jar指令,将該jar包添加,否則會報錯找不到類。

list 指令

:包括list File[S]/Jar[S]/Archive[S]。列出目前DistributeCache中的檔案、jar包或者歸檔。

delete 指令

:包括 delete File[S]/Jar[S]/Archive[S] *。從DistributeCache中删除檔案.

//将file加入緩沖區
add file /root/test/sql;
//列出目前緩沖區内的檔案
list file;
//删除緩存區内的指定file
delete file /root/test/sql;
           

create指令

:建立自定義函數:hive> create temporary function udfTest as ‘com.cstore.udfExample’;

source <filepath>

:在CLI中執行腳本檔案。

//相當于[[email protected] test]# hive -S -f /root/test/sql
hive> source /root/test/sql; 
           

! :在CLI執行Linux指令。

dfs :在CLI執行hdfs指令

3.儲存查詢結果の三種方式:

hive -S -e 'select * from dummy' > a.txt //分隔符和hive資料檔案的分隔符相同
           
[[email protected] ~]# hive -S -e "insert overwrite local directory '/root/hive/a'\ 
>  row format delimited fields terminated by '\t' --分隔符\t
>  select * from logs sort by te" 
           
--使用hdfs指令導出整個表資料
hdfs dfs -get /hive/warehouse/hive01 /root/test/hive01 
           

4.Hive叢集間的導入和導出

使用Export指令會導出Hive表的資料表資料以及資料表對應的中繼資料

--導出指令
EXPORT TABLE test TO '/hive/test_export'

--dfs指令檢視
hdfs dfs -ls /hive/test_export

--結果顯示
/hive/test_export/_metadata
/hive/test_export/data
           

使用Import指令将導出的資料重新導入到hive中(必須将現導入的表重命名)

--導入到内部表的指令
IMPORT TABLE data_managed FROM '/hive/test_export'

--導入到外部表的指令
Import External Table data_external From '/hive/test_export' Location '/hive/external/data'

--驗證是否是外部表
desc formatted data_external
           

5.Hive - JDBC/ODBC

在Hive的jar包中,"org.apache.hadoop.hive.jdbc.HiveDriver"負責提供 JDBC 接口,用戶端程式有了這個包,就可以把 Hive 當成一個資料庫來使用,大部分的操作與對傳統資料庫的操作相同,Hive 允許支援 JDBC 協定的應用程式連接配接到 Hive。當 Hive 在指定端口啟動 hiveserver 服務後,用戶端通過 Java 的 Thrift 和 Hive 伺服器進行通信。過程如下:

1.開啟 hiveserver 服務:

$ hive –service hiveserver 50000(50000)  
           

2.建立與 Hive 的連接配接:

Class.forName(“org.apache.hadoop.hive.jdbc.HiveDriver”);  

Connectioncon=DriverManager.getConnection(“jdbc:hive://ip:50000/default,”hive”,”hadoop”) 
           

預設隻能連接配接到 default 資料庫,通過上面的兩行代碼建立連接配接後,其他的操作與傳統資料庫無太大差别。

3.Hive 的 JDBC 驅動目前還不太成熟,并不支援所有的 JDBC API。

6.Hive Web Interface

1.配置hive-site.xml

<property>
        <name>hive.hwi.war.file</name>
        <value>lib/hive-hwi-0.8.1.war</value>
        <description>This sets the path to the HWI war file, relative to ${HIVE_HOME}.</description>    
        </property>
        
        <property>
        <name>hive.hwi.listen.host</name>
        <value>0.0.0.0</value>
        <description>This is the host address the Hive Web Interface will listen on</description>
        </property>
        
        <property>
        <name>hive.hwi.listen.port</name>
        <value>9999</value>
        <description>This is the port the Hive Web Interface will listen on</description>
        </property>
           

2.啟動Hive的Web服務:

  

hive --service hwi

3.在浏覽器鍵入位址:

http://host_name:9999/hwi

通路

4.點選“Create Session”建立會話,在Query中鍵入查詢語句

7. Hive建立資料庫

hive啟動後預設有一個Default資料庫,也可以人為的建立資料庫,指令:

--手動指定存儲位置
create database hive02 location '/hive/hive02';

--添加其他資訊(建立時間及資料庫備注)
create database hive03 comment 'it is my first database' with dbproperties('creator'='kafka','date'='2015-08-08');

--檢視資料庫的詳細資訊
describe database hive03;
--更詳細的檢視
describe database extended hive03; 
--最優的檢視資料庫結構的指令
describe database formatted hive03;

--database隻能修改dbproperties裡面内容
alter database hive03 set dbproperties('edited-by'='hanmeimei');
           

出自:https://www.cnblogs.com/skyl/p/4736129.html

繼續閱讀