小編給大家分享一下Hive表的基本操作有哪些,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
創(chuàng)新互聯(lián)建站于2013年開始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元源匯做網(wǎng)站,已為上家服務(wù),為源匯各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18982081108
1. 創(chuàng)建表
create table語句遵從sql語法習(xí)慣,只不過Hive的語法更靈活。例如,可以定義表的數(shù)據(jù)文件存儲(chǔ)位置,使用的存儲(chǔ)格式等。
create table if not exists test.user1( name string comment 'name', salary float comment 'salary', address struct<country:string, city:string> comment 'home address' ) comment 'description of the table' partitioned by (age int) row format delimited fields terminated by '\t' stored as orc;
沒有指定external關(guān)鍵字,則為管理表,跟MySQL一樣,if not exists如果表存在則不做操作,否則則新建表。comment可以為其做注釋,分區(qū)為age年齡,列之間分隔符是\t,存儲(chǔ)格式為列式存儲(chǔ)orc,存儲(chǔ)位置為默認(rèn)位置,即參數(shù)hive.metastore.warehouse.dir(默認(rèn):/user/hive/warehouse)指定的hdfs目錄。
2. 拷貝表
使用like可以拷貝一張跟原表結(jié)構(gòu)一樣的空表,里面是沒有數(shù)據(jù)的。
create table if not exists test.user2 like test.user1;
3. 查看表結(jié)構(gòu)
通過desc [可選參數(shù)] tableName命令查看表結(jié)構(gòu),可以看出拷貝的表test.user1與原表test.user1的表結(jié)構(gòu)是一樣的。
hive> desc test.user2; OK name string name salary float salary address struct<country:string,city:string> home address age int # Partition Information # col_name data_type comment age int
也可以加formatted,可以看到更加詳細(xì)和冗長的輸出信息。
hive> desc formatted test.user2; OK # col_name data_type comment name string name salary float salary address struct<country:string,city:string> home address # Partition Information # col_name data_type comment age int # Detailed Table Information Database: test Owner: hdfs CreateTime: Mon Dec 21 16:37:57 CST 2020 LastAccessTime: UNKNOWN Retention: 0 Location: hdfs://nameservice2/user/hive/warehouse/test.db/user2 Table Type: MANAGED_TABLE Table Parameters: COLUMN_STATS_ACCURATE {\"BASIC_STATS\":\"true\"} numFiles 0 numPartitions 0 numRows 0 rawDataSize 0 totalSize 0 transient_lastDdlTime 1608539877 # Storage Information SerDe Library: org.apache.hadoop.hive.ql.io.orc.OrcSerde InputFormat: org.apache.hadoop.hive.ql.io.orc.OrcInputFormat OutputFormat: org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat Compressed: No Num Buckets: -1 Bucket Columns: [] Sort Columns: [] Storage Desc Params: field.delim \t serialization.format \t
4. 刪除表
這跟sql中刪除命令drop table是一樣的:
drop table if exists table_name;
對(duì)于管理表(內(nèi)部表),直接把表徹底刪除了;對(duì)于外部表,還需要?jiǎng)h除對(duì)應(yīng)的hdfs文件才會(huì)徹底將這張表刪除掉,為了安全,通常hadoop集群是開啟回收站功能的,刪除外表表的數(shù)據(jù)就在回收站,后面如果想恢復(fù)也是可以恢復(fù)的,直接從回收站mv到hive對(duì)應(yīng)目錄即可。
5. 修改表
大多數(shù)表屬性可以通過alter table來修改。
5.1 表重命名
alter table test.user1 rename to test.user3;
5.2 增、修、刪分區(qū)
增加分區(qū)使用命令alter table table_name add partition(...) location hdfs_path
alter table test.user2 add if not exists partition (age = 101) location '/user/hive/warehouse/test.db/user2/part-0000101' partition (age = 102) location '/user/hive/warehouse/test.db/user2/part-0000102'
修改分區(qū)也是使用alter table ... set ...命令
alter table test.user2 partition (age = 101) set location '/user/hive/warehouse/test.db/user2/part-0000110'
刪除分區(qū)命令格式是alter table tableName drop if exists partition(...)
alter table test.user2 drop if exists partition(age = 101)
5.3 修改列信息
可以對(duì)某個(gè)字段進(jìn)行重命名,并修改位置、類型或者注釋:
修改前:
hive> desc user_log; OK userid string time string url string
修改列名time為times,并且使用after把位置放到url之后,本來是在之前的。
alter table test.user_log change column time times string comment 'salaries' after url;
再來看表結(jié)構(gòu):
hive> desc user_log; OK userid string url string times string salaries
time -> times,位置在url之后。
5.4 增加列
hive也是可以添加列的:
alter table test.user2 add columns ( birth date comment '生日', hobby string comment '愛好' );
5.5 刪除列
刪除列不是指定列刪除,需要把原有所有列寫一遍,要?jiǎng)h除的列排除掉即可:
hive> desc test.user3; OK name string name salary float salary address struct<country:string,city:string> home address age int # Partition Information # col_name data_type comment age int
如果要?jiǎng)h除列salary,只需要這樣寫:
alter table test.user3 replace columns( name string, address struct<country:string,city:string> );
這里會(huì)報(bào)錯(cuò):
FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. Replacing columns cannot drop columns for table test.user3. SerDe may be incompatible
這張test.user3表是orc格式的,不支持刪除,如果是textfile格式,上面這種replace寫法是可以刪除列的。通常情況下不會(huì)輕易去刪除列的,增加列倒是常見。
5.6 修改表的屬性
可以增加附加的表屬性,或者修改屬性,但是無法刪除屬性:
alter table tableName set tblproperties( 'key' = 'value' );
舉例:這里新建一張表:
create table t8(time string,country string,province string,city string) row format delimited fields terminated by '#' lines terminated by '\n' stored as textfile;
這條語句將t8表中的字段分隔符'#'修改成'\t';
alter table t8 set serdepropertyes('field.delim'='\t');
以上是“Hive表的基本操作有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
文章標(biāo)題:Hive表的基本操作有哪些
轉(zhuǎn)載來于:http://bm7419.com/article30/jdegpo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、品牌網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)公司、云服務(wù)器、手機(jī)網(wǎng)站建設(shè)、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)