Hive表的基本操做

1. 建立表

create table語句聽從sql語法習慣,只不過Hive的語法更靈活。例如,能夠定義表的數據文件存儲位置,使用的存儲格式等。mysql

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關鍵字,則爲管理表,跟mysql同樣,if not exists若是表存在則不作操做,不然則新建表。comment能夠爲其作註釋,分區爲age年齡,列之間分隔符是\t,存儲格式爲列式存儲orc,存儲位置爲默認位置,即參數hive.metastore.warehouse.dir(默認:/user/hive/warehouse)指定的hdfs目錄。sql

2. 拷貝表

使用like能夠拷貝一張跟原表結構同樣的空表,裏面是沒有數據的。apache

create table if not exists test.user2 like test.user1;

3. 查看錶結構

經過desc [可選參數] tableName命令查看錶結構,能夠看出拷貝的表test.user1與原表test.user1的表結構是同樣的。安全

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,能夠看到更加詳細和冗長的輸出信息。oop

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;

對於管理表(內部表),直接把表完全刪除了;對於外部表,還須要刪除對應的hdfs文件纔會完全將這張表刪除掉,爲了安全,一般hadoop集羣是開啓回收站功能的,刪除外表表的數據就在回收站,後面若是想恢復也是能夠恢復的,直接從回收站mvhive對應目錄便可。大數據

5. 修改表

大多數表屬性能夠經過alter table來修改。url

5.1 表重命名

alter table test.user1 rename to test.user3;

5.2 增、修、刪分區

增長分區使用命令alter table table_name add partition(...) location hdfs_pathcode

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'

修改分區也是使用alter table ... set ...命令orm

alter table test.user2 partition (age = 101) set location '/user/hive/warehouse/test.db/user2/part-0000110'

刪除分區命令格式是alter table tableName drop if exists partition(...)

alter table test.user2 drop if exists partition(age = 101)

5.3 修改列信息

能夠對某個字段進行重命名,並修改位置、類型或者註釋:

修改前:

hive> desc user_log;
OK
userid              	string              	                    
time                	string              	                    
url                 	string

修改列名timetimes,而且使用after把位置放到url以後,原本是在以前的。

alter table test.user_log
change column time times string
comment 'salaries'
after url;

再來看錶結構:

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 刪除列

刪除列不是指定列刪除,須要把原有全部列寫一遍,要刪除的列排除掉便可:

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

若是要刪除列salary,只須要這樣寫:

alter table test.user3 replace columns(
name string,
address struct<country:string,city:string>
);

這裏會報錯:

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寫法是能夠刪除列的。一般狀況下不會輕易去刪除列的,增長列卻是常見。

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');

關注公衆號:Java大數據與數據倉庫,領取資料,學習大數據技術。

相關文章
相關標籤/搜索