Hadoop 之 Hive創建內外部表


Hive創建內外部表

Hive 數據庫類似傳統數據庫,也是有數據庫與表的概念,hive和關系數據庫存儲文件的系統不同,hive使用的是hadoop的HDFS(hadoop的分布式文件系統),關系數據庫則是服務器本地的文件系統。Hive作用比如是為海量數據做數據倉庫分析與挖掘之用等以及原理,可查看相關資料,這里通過操作層面的示例,認識下hive下如何創建與查詢表,如何導入數據

Hive表分內部表與外部表,其區別體現在存儲上,內部表的創建過程和數據加載過程可以分別獨立完成,也可以在同一語句中完成,加載數據的過程中,實際數據會被移動到數據倉庫目錄中;之后對數據訪問將會直接在數據倉庫中完成。刪除表時,表中的數據和元數據將會同時刪除,而外外部表只有一個過程,加載數據和創建表同時完成,實際數據存儲在 location指定的HDFS的相應目錄,並不會移到數據倉庫中,當刪除外部表時,只是刪除了該鏈接。

概括來講,直觀表現:

1.語句上看,創建外部表需要添加 external 字段

2.刪除內部表時,表數據及HDFS中的數據文件都會被刪除,刪除外部表時,HDFS中的數據文件不會一起被刪除,實際數據是存儲在location指定的hdfs中目錄。

一、創建內部表

首先是進入hive環境 >hive

hive> create table person (id int,name string,sex string,tel string)
    > row format delimited fields terminated by ','
    > stored as textfile;
OK
Time taken: 0.187 seconds

查看創建是否成功 

hive> show tables;
OK
person
Time taken: 0.11 seconds, Fetched: 1 row(s)
hive>

准備好待導入表的數據,如

加載數據,我們可以提前准備好文件,如/usr/mydata/persion.txt 內容為

1 zhangshan man 13000000001
2 lishi woman 15000000001
3 wangwu man 17000000001
4 sytemadm man 10000000001

將上面文本文件中的內容導入剛才建的person表中

hive> load data local inpath '/usr/mydata/person.txt' into table person;
Loading data to table default.person
OK
Time taken: 9.869 seconds

查看數據

hive> select * from person;
OK
1    zhangshan    man    13000000001
2    lishi    woman    15000000001
3    wangwu    man    17000000001
4    sytemadm    man    10000000001
Time taken: 2.266 seconds, Fetched: 4 row(s)
hive>

查看表存儲目錄,hive.metastore.warehouse.dir是在HIVE_HOME/conf/hive-site.xml中配置的,路徑默認為/user/hive/warehouse,查看此目錄可看到一個目錄名為person

[hadoop@kencentos01 bin]$ hdfs dfs -ls /user/hive/warehouse
Found 1 items
drwxrwxrwx   - hadoop supergroup          0 2017-12-26 14:53 /user/hive/warehouse/person

person下有個文件就是我們在導入數據時,從本地文件導入HDFS中的person.txt文件

[hadoop@kencentos01 bin]$ hdfs dfs -ls /user/hive/warehouse/person
Found 1 items
-rwxrwxrwx   3 hadoop supergroup        106 2017-12-26 14:53 /user/hive/warehouse/person/person.txt

二、創建外部表

創建外部表,注意關鍵字”external”,location是指定表的文件位置,若不指定,則為默認與上面內部表一樣配置的路徑

hive> create external table person_external (id int,name string,sex string,tel string)
    > row format delimited fields terminated by ','
    > stored as textfile
> location '/user/hive/warehouse/external';
OK
Time taken: 1.31 seconds

查看建表結果

hive> show tables;
OK
person
person_external
Time taken: 0.124 seconds, Fetched: 2 row(s)
hive>

同樣為了導入數據,需提前准備相應文件如,person_external.txt ,具體內容按字段類型列出就行,可直接拿上面內部表數據

hive> load data local inpath '/usr/mydata/person_external.txt' into table person_external;
Loading data to table default.person_external
OK
Time taken: 0.673 seconds

查看數據,與查看內部表一樣:

查看表中的數據
hive> select * from person_external
    > ;
OK
11    zhangshan    man    13000000002
12    lishai    woman    15000000002
13    wangwu    man    17000000002
14    systemadm    man    10000000002
Time taken: 1.004 seconds, Fetched: 4 row(s)

查看文件所在位置,對應的HDFS目錄

[hadoop@kencentos01 bin]$ hdfs dfs -ls /user/hive/warehouse/external
Found 1 items
-rwxrwxrwx   3 hadoop supergroup        112 2017-12-26 15:22 /user/hive/warehouse/external/person_external.txt

 


免責聲明!

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



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