你会Hive表的基本操作吗?

大数据 2023-07-05 17:29:38
54阅读

文中转载微信公众平台「Java互联网大数据与数据库管理」,创作者柯同学们。转截文中请联络Java互联网大数据与数据库管理微信公众号。 

1. 创建表

create table句子遵循sql语法习惯性,只不过是Hive的英语的语法更灵便。比如,能够界定表的数据信息文档存储部位,应用的储存文件格式等。

 
  1. create table if not exists test.user1( 
  2. name string comment 'name'
  3. salary float comment 'salary'
  4. address struct<country:string, city:string> comment 'home address' 
  5. comment 'description of the table' 
  6. partitioned by (age int
  7. row format delimited fields terminated by '\t' 
  8. stored as orc; 

沒有特定external关键词,则为管理方法表,跟mysql一样,if not exists假如表存有则不做实际操作,不然则新创建表。comment能够为其做注解,系统分区为age年纪,列中间分节符是\t,储存文件格式为列式储存orc,储存部位为默认设置部位,即主要参数hive.metastore.warehouse.dir(默认设置:/user/hive/warehouse)特定的hdfs文件目录。

2. 复制表

应用like能够复制一张跟原表结构一样的空表,里边是沒有数据信息的。

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

3. 查询表结构

根据desc [可选主要参数] tableName指令查询表结构,能够看得出复制的表test.user1与原表test.user1的表构造是一样的。

 
  1. hive> desc test.user2; 
  2. OK 
  3. name                    string                  name                 
  4. salary                  float                   salary               
  5. address                 struct<country:string,city:string>  home address         
  6. age                     int                                          
  7.  
  8. # Partition Information          
  9. # col_name                data_type               comment              
  10.  
  11. age                     int          

还可以加formatted,能够见到更为详尽和冗杂的輸出信息内容。

 
  1. hive> desc formatted test.user2; 
  2. OK 
  3. # col_name                data_type               comment              
  4.  
  5. name                    string                  name                 
  6. salary                  float                   salary               
  7. address                 struct<country:string,city:string>  home address         
  8.  
  9. # Partition Information          
  10. # col_name                data_type               comment              
  11.  
  12. age                     int                                          
  13.  
  14. # Detailed Table Information          
  15. Database:               test                      
  16. Owner:                  hdfs                      
  17. CreateTime:             Mon Dec 21 16:37:57 CST 2020      
  18. LastAccessTime:         UNKNOWN                   
  19. Retention:              0                         
  20. Location:               hdfs://nameservice2/user/hive/warehouse/test.db/user2     
  21. Table Type:             MANAGED_TABLE             
  22. Table Parameters:          
  23.     COLUMN_STATS_ACCURATE   {\"BASIC_STATS\":\"true\"
  24.     numFiles                0                    
  25.     numPartitions           0                    
  26.     numRows                 0                    
  27.     rawDataSize             0                    
  28.     totalSize               0                    
  29.     transient_lastDdlTime   1608539877           
  30.  
  31. # Storage Information          
  32. SerDe Library:          org.apache.hadoop.hive.ql.io.orc.OrcSerde     
  33. InputFormat:            org.apache.hadoop.hive.ql.io.orc.OrcInputFormat   
  34. OutputFormat:           org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat      
  35. Compressed:             No                        
  36. Num Buckets:            -1                        
  37. Bucket Columns:         []                        
  38. Sort Columns:           []                        
  39. Storage Desc Params:          
  40.     field.delim             \t                   
  41.     serialization.format    \t            

4. 删除表

这跟sql中删掉指令drop table是一样的:

 
  1. drop table if exists table_name; 

针对管理方法表(內部表),立即把表彻底删除了;针对外界表,还必须删掉相匹配的hdfs文档才会完全将这张表删掉掉,为了更好地安全性,一般hadoop群集是打开垃圾回收站作用的,删掉表面表的数据信息就在垃圾回收站,后边假如想修复也是能够修复的,立即从垃圾回收站mv到hive相匹配文件目录就可以。

5. 改动表

大部分表特性能够根据alter table来改动。

5.1 表重新命名

 
  1. alter table test.user1 rename to test.user3; 

5.2 增、修、删系统分区

提升系统分区应用指令alter table table_name add partition(...) location hdfs_path

 
  1. alter table test.user2 add if not exists 
  2. partition (age = 101) location '/user/hive/warehouse/test.db/user2/part-0000101' 
  3. partition (age = 102) location '/user/hive/warehouse/test.db/user2/part-0000102' 

改动系统分区也是应用alter table ... set ...指令

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

删除分区指令文件格式是alter table tableName drop if exists partition(...)

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

5.3 改动列信息内容

能够对某一字段名开展重新命名,并改动部位、种类或是注解:

改动前:

 
  1. hive> desc user_log; 
  2. OK 
  3. userid                  string                                       
  4. time                    string                                       
  5. url                     string             

改动字段名time为times,而且应用after把部位放进url以后,原本是在以前的。

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

再看来表结构:

 
  1. hive> desc user_log; 
  2. OK 
  3. userid                  string                                       
  4. url                     string                                       
  5. times                   string                  salaries             

time -> times,部位在url以后。

5.4 提升列

hive也是能够加上列的:

 
  1. alter table test.user2 add columns ( 
  2. birth date comment '生日'
  3. hobby string comment '喜好' 
  4. ); 

5.5 删掉列

删掉列并不是特定列删掉,必须把原来全部列写一遍,要删掉的列清除掉就可以:

 
  1. hive> desc test.user3; 
  2. OK 
  3. name                    string                  name                 
  4. salary                  float                   salary               
  5. address                 struct<country:string,city:string>  home address         
  6. age                     int                                          
  7.  
  8. # Partition Information          
  9. # col_name                data_type               comment              
  10.  
  11. age                     int               

假如要删掉列salary,只必须那样写:

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

这儿会出错:

 
  1. 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 改动表的特性

能够提升额外的表特性,或是改动特性,可是删不掉特性:

 
  1. alter table tableName set tblproperties( 
  2.     'key' = 'value' 
  3. ); 

举例说明:这儿新创建一张表:

 
  1. create table t8(time string,country string,province string,city string) 
  2. row format delimited fields terminated by '#'  
  3. lines terminated by '\n'  
  4. stored as textfile; 

这条句子将t8表格中的字段名分节符'#'改动成'\t';

 
  1. alter table t8 set serdepropertyes('field.delim'='\t'); 
the end
免责声明:本文不代表本站的观点和立场,如有侵权请联系本站删除!本站仅提供信息存储空间服务。