天天看點

HIVE 基本指令hive筆記:

#hive     啟動 hive>quit;     --退出hive hive> exit;    --exit會影響之前的使用,是以需要下一句kill掉hadoop的程序 >hadoop job -kill jobid

hive>create database database_name; 建立資料庫 如果資料庫已經存在就會抛出一個錯誤資訊,使用如下語句可以避免抛出錯誤資訊: hive>creat database if not exists database_name

hive> show databases;   檢視資料庫

如果資料庫比較多的話,也可以用正規表達式來檢視:

hive> show databases like 'h.*';

hive> use default;    --使用哪個資料庫

hive>show tables;  --檢視該資料庫中的所有表

hive>show tables  ‘*t*’;    --支援模糊查詢

hive> describe tab_name;    --檢視表的結構及表的路徑

hive> describe database database_name;  --檢視資料庫的描述及路徑

可以用下面的指令來修改資料庫的路徑:

hive> creat database database_name location '路徑';   

hive> drop database if exists database_name; --删除空的資料庫

hive> drop database if exists database_name cascade; --先删除資料庫中的表再删除資料庫

hive>show partitions t1;   --檢視表有哪些分區 

修改表:

hive>alter table table_name rename to another_name;   --修改表名

hive>drop table t1 ;      --删除表t1

或者: hive> drop table if exists t1;
hive不支援修改表中資料,但是可以修改表結構,而不影響資料

有local的速度明顯比沒有local慢:

hive>load data inpath '/root/inner_table.dat' into table t1;   移動hdfs中資料到t1表中

hive>load  data local inpath '/root/inner_table.dat' into table t1;  上傳本地資料到hdfs中

hive> !ls;  查詢目前linux檔案夾下的檔案

hive> dfs -ls /; 查詢目前hdfs檔案系統下  '/'目錄下的檔案

hive筆記:

1、從檔案中執行hive查詢:$ hive -f .sql檔案的路徑;      e.g  $hive -f /path/to/file/xxxx.hql;      在hive shell中可以用source指令來執行一個腳本檔案: hive>source .sql檔案的路徑       e.g. hive> source /path/to/file/test.sql;       hive中一次使用指令: $ hive -e "SQL語句";      e.g.  $ hive -e "select * from mytable limit 3";

2、沒有一個指令可以讓使用者檢視目前所在的是哪個資料庫庫

3、在hive内執行一些bash shell指令(在指令前加!并且以;結尾即可)      

HIVE 基本指令hive筆記:

4、在hive内執行Hadoop的dfs指令:(去掉hadoop,以;結尾)

5、 Hive腳本如何注釋:       使用--開頭的字元串來表示注釋

6、Hive與MySQL相比,它不支援行級插入操作、更新操作和删除操作。Hive也不支援事務。      Hive增加了在Hadoop背景下的可以提高更高性能的擴充。

7、 向管理表中加載資料:       Hive沒有行級别的插入、删除、更新的操作,那麼往表裡面裝資料的唯一的途徑就是使用一種“大量”的資料裝載操作,或者僅僅将檔案寫入到正确的目錄下面。        overwrite關鍵字:               load data local inpath '${env:HOME}/目錄'               overwrite into table table_name               partition (分區);

8、從表中導出資料:     hadoop fs -cp source_path target_path 或者:使用者可以使用 insert……directory……      insert overwrite local directory '/tmp/目錄'     這裡指定的路徑也可以是全URL路徑

9、hive中使用正規表達式   (1)   hive> select 'price.*' from table_name;                選出所有列名以price作為字首的列   (2)  用Like或者RLike

10、聚合函數      可以通過設定屬性hive.map.aggr值為true來提高聚合的性能:      hive>hive.map.aggr=true;

11、什麼情況下hive可以避免進行mapreduce?       在本地模式的時候可以避免觸發一個mr的job,此外,如果屬性hive.execmode.local.auto的值為true的話,hive還戶嘗試本地模式進行其他的操作。        set hive.execmode.local.auto=true;        說明:最好将 set hive.execmode.local.auto=true;這個設定增加到你的$HOME/.hiverc配置檔案中去。

12、JOIN語句        hive支援通常的SQL JOIN語句,但是隻支援等值連接配接。hive也不支援在on子句中用謂詞OR

13、union all        将兩個表或者多個表進行合并,每一個union all子查詢都必須具有相同的列,而且對應每個字段的每個類型都必須一緻。

繼續閱讀