您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)Hive基礎(chǔ)操作的示例代碼,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
1.在hive的服務(wù)端新建源數(shù)據(jù)
[root@hadoop5 ~]# cat hivedata 1,xiaoming,read-tv-code,beijing:chaoyang-shanghai:pudong 2,lisi,cook-game,chongqing:yongchun-sichuan:yibing 3,zhangsan,shop-eat,shanghai:xujiahui
2.創(chuàng)建內(nèi)部表
create table test1 (id int,name string,likes array<string>,address map<string,string>) row format delimited fields terminated by ',' collection items terminated by '-' map keys terminated by ':';
3.將數(shù)據(jù)導(dǎo)入hive
load data local inpath '/root/hivedata' into table test1;
0: jdbc:hive2://hadoop5:10000> select * from test1; +-----------+-------------+-----------------------+----------------------------------------------+ | test1.id | test1.name | test1.likes | test1.address | +-----------+-------------+-----------------------+----------------------------------------------+ | 1 | xiaoming | ["read","tv","code"] | {"beijing":"chaoyang","shanghai":"pudong"} | | 2 | lisi | ["cook","game"] | {"chongqing":"yongchun","sichuan":"yibing"} | | 3 | zhangsan | ["shop","eat"] | {"shanghai":"xujiahui"} | +-----------+-------------+-----------------------+----------------------------------------------+ 3 rows selected (0.207 seconds) 0: jdbc:hive2://hadoop5:10000>
4.創(chuàng)建外部表(在hive中刪除后,hdfs上數(shù)據(jù)不會刪除)
create external table test2 (id int,name string,likes array<string>,address map<string,string>) row format delimited fields terminated by ',' collection items terminated by '-' map keys terminated by ':' location '/user/test2';
5.以test1的部分列為模板創(chuàng)建test3
create table test3 as select id, name from test1;
0: jdbc:hive2://hadoop5:10000> desc test3; +-----------+------------+----------+ | col_name | data_type | comment | +-----------+------------+----------+ | id | int | | | name | string | | +-----------+------------+----------+ 2 rows selected (0.406 seconds) 0: jdbc:hive2://hadoop5:10000>
6.參照test1創(chuàng)建test4
create table test4 like test1;
7.創(chuàng)建分區(qū)表
create table test5 (id int,name string,likes array<string>,address map<string,string>) partitioned by (sex string) row format delimited fields terminated by ',' collection items terminated by '-' map keys terminated by ':';
0: jdbc:hive2://hadoop5:10000> desc test5; +--------------------------+-----------------------+-----------------------+ | col_name | data_type | comment | +--------------------------+-----------------------+-----------------------+ | id | int | | | name | string | | | likes | array<string> | | | address | map<string,string> | | | sex | string | | | | NULL | NULL | | # Partition Information | NULL | NULL | | # col_name | data_type | comment | | | NULL | NULL | | sex | string | | +--------------------------+-----------------------+-----------------------+ 10 rows selected (0.382 seconds) 0: jdbc:hive2://hadoop5:10000>
8.為該分區(qū)添加加載數(shù)據(jù)
load data local inpath '/root/hivedata' into table test5 partition (sex='boy');
0: jdbc:hive2://hadoop5:10000> select * from test5; +-----------+-------------+-----------------------+----------------------------------------------+------------+ | test5.id | test5.name | test5.likes | test5.address | test5.sex | +-----------+-------------+-----------------------+----------------------------------------------+------------+ | 1 | xiaoming | ["read","tv","code"] | {"beijing":"chaoyang","shanghai":"pudong"} | boy | | 2 | lisi | ["cook","game"] | {"chongqing":"yongchun","sichuan":"yibing"} | boy | | 3 | zhangsan | ["shop","eat"] | {"shanghai":"xujiahui"} | boy | +-----------+-------------+-----------------------+----------------------------------------------+------------+ 3 rows selected (0.784 seconds) 0: jdbc:hive2://hadoop5:10000>
9.為test5添加一個sex=girl的分區(qū)
alter table test5 add partition (sex='girl');
10.刪除一個分區(qū)
alter table test5 drop partition (sex='girl');
關(guān)于“Hive基礎(chǔ)操作的示例代碼”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。