使用hive需要hive環境
啟動Hive
進入HIVE_HOME/bin,啟動hive
./hive
内部表
- 建表
hive> create table fz
> (id int,name string,age int,tel string)
> ROW FORMAT DELIMITED
> FIELDS TERMINATED BY ','
> STORED AS TEXTFILE;
建立完成後使用 show tables; 檢視是否建立成功.
hive> show tables;
OK
fz
Time taken: 0.043 seconds, Fetched: 9 row(s)
- 導入資料
提前準備好一個txt檔案,檔案内容如下
1,fz,25,131888888888
2,test,20,13222222222
3,dx,24,183938384983
接下來導入資料
hive> load data local inpath '/Users/FengZhen/Desktop/Hadoop/hive/testfile/fz.txt' into table fz;
Loading data to table default.fz
Table default.fz stats: [numFiles=1, totalSize=66]
OK
Time taken: 0.784 seconds
檢視表中的資料。
hive> select * from fz;
OK
1 fz 25 13188888888888
2 test 20 13222222222
3 dx 24 183938384983
Time taken: 0.445 seconds, Fetched: 3 row(s)
資料已經導入成功。
在導入資料的過程中,如果在建表的過程中沒有指定location,那麼就會在hive.metastore.warehouse.dir指定的路徑下,以表名建立一個檔案夾,之後所有有關該表的資料都會存儲到此檔案夾中。
hive.metastore.warehouse.dir是在HIVE_HOME/conf/hive-site.xml中配置的,如下,我配置的路徑為/user/hive/warehouse
<property>
<name>hive.metastore.warehouse.dir</name>
<value>/user/hive/warehouse</value>
</property>
現在去HDFS中就可以看到該路徑下有個檔案夾名為fz,fz下有個檔案就是我們在導入資料時,從本地檔案系統拷貝到HDFS中的fz.txt.
EFdeMacBook-Pro:conf FengZhen$ hadoop fs -ls /user/hive/warehouse/fz
Found 1 items
-rwxr-xr-x 1 FengZhen supergroup 66 2017-06-06 10:57 /user/hive/warehouse/fz/fz.txt
- 删除該表
hive> drop table fz;
OK
Time taken: 0.141 seconds
該表删除後,再去HDFS中看一下還有沒有之前建立的檔案夾。
hadoop fs -ls /user/hive/warehouse
結果顯示該檔案夾已經沒有了,也就是說當我們在删除内部表的時候,不僅删除了表中的資料,還删除了資料檔案。
外部表
- 建表
hive> create external table fz_external_table(id int,name string,age int,tel string)
> ROW FORMAT DELIMITED
> FIELDS TERMINATED BY ','
> STORED AS TEXTFILE
> location '/user/hive/external/fz_external_table';
建外部表時要加external關鍵字,我在建表時指定了location,當然也可以不指定,不指定就預設使用hive.metastore.warehouse.dir指定的路徑。
- 導入資料
提前準備好資料檔案fz_external.txt,資料同上,隻是換了個名。
1,fz,25,13188888888888
2,test,20,13222222222
3,dx,24,183938384983
4,test1,22,1111111111
接下來進行導入資料的操作
hive> load data local inpath '/Users/FengZhen/Desktop/Hadoop/hive/testfile/fz_external_table.txt' into table fz_external_table;
Loading data to table default.fz_external_table
Table default.fz_external_table stats: [numFiles=0, totalSize=0]
OK
Time taken: 0.224 seconds
檢視表内資料。
hive> select * from fz_external_table;
OK
1 fz 25 13188888888888
2 test 20 13222222222
3 dx 24 183938384983
4 test1 22 1111111111
Time taken: 0.06 seconds, Fetched: 4 row(s)
現在資料已經導入成功,接下來去HDFS中檢視以下,在location指定的路徑下
EFdeMacBook-Pro:testfile FengZhen$ hadoop fs -ls /user/hive/external/fz_external_table;
Found 1 items
-rwxr-xr-x 1 FengZhen supergroup 88 2017-06-06 13:44 /user/hive/external/fz_external_table/fz_external_table.txt
可以看到,往外部表中導入資料的時候,本地檔案系統中的資料檔案也被拷貝到了HDFS中。
現在來删除外部表,看一下HDFS中檔案有沒有被删除。
hive> drop table fz_external_table;
OK
Time taken: 0.103 seconds
EFdeMacBook-Pro:testfile FengZhen$ hadoop fs -ls /user/hive/external/fz_external_table;
Found 1 items
-rwxr-xr-x 1 FengZhen supergroup 88 2017-06-06 13:44 /user/hive/external/fz_external_table/fz_external_table.txt
内部表與外部表的不同
- 建立外部表需要添加 external 字段。而内部表不需要。
- 删除外部表時,HDFS中的資料檔案不會一起被删除。而删除内部表時,表資料及HDFS中的資料檔案都會被删除。