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


1 hive與非交互式模式命令行

  1. hive -e:從命令行執行指定的HQL,不需要分號:
hive -e ‘select * from dumy limit 100>a.txt
  1. hive -f :執行HQL腳
hive -f /home/my/hive-script.sql

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

hive -i /home/my/hive-init.sql 

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中執行腳本文件。

//相當於[root@ncst 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數據文件的分隔符相同
[root@hadoop01 ~]# 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


免責聲明!

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



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