hive命令列操作

2022-05-12 15:43:06 字數 2032 閱讀 9867

$hive> create databses mydb2;    //注意在hive中資料庫就是目錄

$hive>show databases ; //顯示資料庫

$hive>use mydb2;

$hive>create table t(id int,name string,age int);//建立乙個表

$hive>create table mydb2.t(id int,name string ,age int); //在指定資料庫下面建立乙個表

$hive>drop table mydb2.t; //刪除指定庫下面的表

$hive>insert into t3(id,name,age) values(1,'jack',34);//這個插入過程是通過mr的過程

$hive>hdfs dfs -cat /user/hive/warehouse/mydb1.db/t3/000000_0; //通過這樣的乙個過程來進行插入資料的查詢

$hive>select *from t3 ; //這個地方實現在表t3中來查詢資料

或者:$hive>dfs -lsr /; //通過這個語句來進行查詢

1.managed table:託管表,

2.external table :外部表

這兩個表的區別是:託管表在刪除表的時候資料也被刪除了,而外部表在刪除的時候,資料不會被刪除

內部表

create table test (name string , age string) location '/input/table_data';

注:hive預設建立的是內部表

此時,會在hdfs上新建乙個test表的資料存放地

load data inpath '/input/data' into table test ;

會將hdfs上的/input/data目錄下的資料轉移到/input/table_data目錄下。刪除test表後,會將test表的資料和元資料資訊全部刪除,即最後/input/table_data下無資料,當然/input/data下再上一步已經沒有了資料!

如果建立內部表時沒有指定location,就會在/user/hive/warehouse/下新建乙個表目錄,其餘情況同上。

注:load data會轉移資料

外部表

create external table etest (name string , age string);

會在/user/hive/warehouse/新建乙個表目錄et

load data inpath '/input/edata' into table etest;

把hdfs上/input/edata/下的資料轉到/user/hive/warehouse/et下,刪除這個外部表後,/user/hive/warehouse/et下的資料不會刪除,但是/input/edata/下的資料在上一步load後已經沒有了!資料的位置發生了變化!

本質是load乙個hdfs上的資料時會轉移資料!

小結最後歸納一下hive中表與外部表的區別:

1、在匯入資料到外部表,資料並沒有移動到自己的資料倉儲目錄下(如果指定了location的話),也就是說外部表中的資料並不是由它自己來管理的!而內部表則不一樣;

2、在刪除內部表的時候,hive將會把屬於表的元資料和資料全部刪掉;而刪除外部表的時候,hive僅僅刪除外部表的元資料,資料是不會刪除的!

在建立內部表或外部表時加上location 的效果是一樣的,只不過表目錄的位置不同而已,加上partition用法也一樣,只不過表目錄下會有分割槽目錄而已,load data local inpath直接把本地檔案系統的資料上傳到hdfs上,有location上傳到location指定的位置上,沒有的話上傳到hive預設配置的資料倉儲中。

外部表相對來說更加安全些,資料組織也更加靈活,方便共享源資料。

那麼,應該如何選擇使用哪種表呢?在大多數情況沒有太多的區別,因此選擇只是個人喜好的問題。但是作為乙個經驗,如果所有處理都需要由hive完成,那麼你應該建立表,否則使用外部表!

Hive命令列引數

hive home bin hive是乙個shell工具,它可以用來執行於互動或批處理方式配置單元查詢。語法 usage hive hiveconf x y i filename f filename e query string s i initialization sql from file e...

Hive命令列工具

hive支援sequencefile,說明可以對檔案進行壓縮,rcfile支援列式儲存,做到稀疏儲存,減小空間,讀取速度快。圖 使用命令hive hiveconf hive.cli.print.current.db true 來設定hive 後面跟著資料庫的名字 圖 使用命令來設定查詢時出現列名 圖...

大資料 hive 命令列

hive h 幫助文件 usage hive commands.e.g.d a b or define a b database specify the database to use e sql from command line f sql from files h,help print hel...