--創建內表
create table if not exists employee(
id int comment 'empoyeeid',
dateincompany string comment 'data come in company',
money float comment 'work money',
mapdatamap array<string>,
arraydata array<int>,
structordata struct<col1:string,col2:string>)
partitioned by (century string comment 'centruycome in company',
year string comment 'come in comany year')
row format delimited fields terminated by ','
collection items terminated by '@'
map keys terminated by '$'
stored as textfile;
數據文件:
[hadoop@master hivetest]$ more employee.txt
1,huawei,1000.2,m1$a,1@2@3@4,c1@c2
裝載數據:
hive>LOAD DATA LOCAL INPATH '/home/hadoop/tmp/hivetest/employee.txt' INTO TABLE employee PARTITION(century='zhengzhou', year='20180910');
查詢數據:
hive> select * from employee;
OK
1 huawei 1000.2 ["m1$a"] [1,2,3,4] {"col1":"c1","col2":"c2"} zhengzhou 20180910
Time taken: 0.638 seconds, Fetched: 1 row(s)
給出的現象是:
在HDFS上,默認的路徑下/user/hive/warehouse/employee生成一個目錄,在前台界面,也是一個目錄。
刪除內表,目錄都不存在了。
還可以指定目錄(HDFS上)
create table if not exists test1(id int, name string)
row format delimited fields terminated by ',' stored as textfile location '/tmp/data';
> load data inpath '/tmp/test1.txt' into table test1 ;
> select * from test1;
OK
1 zhangwei
Time taken: 0.593 seconds, Fetched: 1 row(s)
/tmp/test1.txt在加載的時候,刪除掉了,數據加載到表里,其實就是落成文件到/tmp/data/test1.txt
刪除表后,數據都刪除了,data目錄都刪除了。
--創建外表
create external table if not exists test2 (id int,name string)
row format delimited fields terminated by ',' stored as textfile;
會在/user/hive/warehouse/新建一個表目錄test2
hive> load data inpath '/tmp/test1.txt' into table test2 ;
Loading data to table default.test2
OK
Time taken: 1.053 seconds
hive> select * from test2;
OK
1 zhangwei
Time taken: 0.281 seconds, Fetched: 1 row(s)
現象: 在load的那一步,會把/tmp/test1.txt文件移動到/user/hive/warehouse/test2這個目錄下。
數據的位置發生了變化!
本質是load一個hdfs上的數據時會轉移數據!刪除表后,數據文件在留在HDFS上。
2、在刪除內部表的時候,Hive將會把屬於表的元數據和數據全部刪掉;而刪除外部表的時候,Hive僅僅刪除外部表的元數據,數據是不會刪除的!
3. 在創建內部表或外部表時加上location 的效果是一樣的,只不過表目錄的位置不同而已,加上partition用法也一樣,只不過表目錄下會有分區目錄而已,load data local inpath直接把本地文件系統的數據上傳到hdfs上,有location上傳到location指定的位置上,沒有的話上傳到hive默認配置的數據倉庫中。
外部表相對來說更加安全些,數據組織也更加靈活,方便共享源數據。
那么,應該如何選擇使用哪種表呢?在大多數情況沒有太多的區別,因此選擇只是個人喜好的問題。但是作為一個經驗,如果所有處理都需要由Hive完成,那么你應該創建表,否則使用外部表!
有一個命令:
時光如水,悄然而逝。