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
使用like
能夠拷貝一張跟原表結構同樣的空表,裏面是沒有數據的。apache
create table if not exists test.user2 like test.user1;
經過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
這跟sql
中刪除命令drop table
是同樣的:學習
drop table if exists table_name;
對於管理表(內部表),直接把表完全刪除了;對於外部表,還須要刪除對應的hdfs
文件纔會完全將這張表刪除掉,爲了安全,一般hadoop
集羣是開啓回收站功能的,刪除外表表的數據就在回收站,後面若是想恢復也是能夠恢復的,直接從回收站mv
到hive
對應目錄便可。大數據
大多數表屬性能夠經過alter table
來修改。url
alter table test.user1 rename to test.user3;
增長分區使用命令alter table table_name add partition(...) location hdfs_path
code
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)
能夠對某個字段進行重命名,並修改位置、類型或者註釋:
修改前:
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;
再來看錶結構:
hive> desc user_log; OK userid string url string times string salaries
time -> times
,位置在url
以後。
hive
也是能夠添加列的:
alter table test.user2 add columns ( birth date comment '生日', hobby string comment '愛好' );
刪除列不是指定列刪除,須要把原有全部列寫一遍,要刪除的列排除掉便可:
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
寫法是能夠刪除列的。一般狀況下不會輕易去刪除列的,增長列卻是常見。
能夠增長附加的表屬性,或者修改屬性,可是沒法刪除屬性:
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大數據與數據倉庫,領取資料,學習大數據技術。