Hive表的基本操作

简介: create table语句遵从sql语法习惯,只不过Hive的语法更灵活。例如,可以定义表的数据文件存储位置,使用的存储格式等。

1. 创建表


create table语句遵从sql语法习惯,只不过Hive的语法更灵活。例如,可以定义表的数据文件存储位置,使用的存储格式等。


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目录。


2. 拷贝表


使用like可以拷贝一张跟原表结构一样的空表,里面是没有数据的。


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,可以看到更加详细和冗长的输出信息。


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来修改。


5.1 表重命名


alter table test.user1 rename to test.user3;

5.2 增、修、删分区


增加分区使用命令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'

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


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');
目录
相关文章
|
4月前
|
SQL 存储 HIVE
Hive中的表是如何定义的?请解释表的结构和数据类型。
Hive中的表是如何定义的?请解释表的结构和数据类型。
34 0
|
5月前
|
SQL 消息中间件 数据处理
DataX读取Hive Orc格式表丢失数据处理记录
DataX读取Hive Orc格式表丢失数据处理记录
130 0
|
6月前
|
SQL HIVE
49 Hive修改表
49 Hive修改表
19 0
49 Hive修改表
|
20天前
|
SQL 数据库 HIVE
Hive【基础知识 05】常用DDL操作(数据库操作+创建表+修改表+清空删除表+其他命令)
【4月更文挑战第8天】Hive【基础知识 05】常用DDL操作(数据库操作+创建表+修改表+清空删除表+其他命令)
24 0
|
3月前
|
SQL 消息中间件 Kafka
Flink部署问题之hive表没有数据如何解决
Apache Flink是由Apache软件基金会开发的开源流处理框架,其核心是用Java和Scala编写的分布式流数据流引擎。本合集提供有关Apache Flink相关技术、使用技巧和最佳实践的资源。
|
4月前
|
SQL 分布式计算 关系型数据库
Sqoop数据导入到Hive表的最佳实践
Sqoop数据导入到Hive表的最佳实践
|
5月前
|
SQL 存储 HIVE
❤️Hive的基本知识(二)Hive中的各种表❤️
❤️Hive的基本知识(二)Hive中的各种表❤️
31 0
|
5月前
|
SQL 存储 关系型数据库
Presto【实践 01】Presto查询性能优化(数据存储+SQL优化+无缝替换Hive表+注意事项)及9个实践问题分享
Presto【实践 01】Presto查询性能优化(数据存储+SQL优化+无缝替换Hive表+注意事项)及9个实践问题分享
112 0
|
5月前
|
SQL 存储 分布式计算
Hive 基本操作(创建数据库与创建数据库表)
Hive 基本操作(创建数据库与创建数据库表)
109 0
|
5月前
|
SQL 分布式计算 MaxCompute
Apache Flink目前不支持直接写入MaxCompute,但是可以通过Hive Catalog将Flink的数据写入Hive表
Apache Flink目前不支持直接写入MaxCompute,但是可以通过Hive Catalog将Flink的数据写入Hive表
60 3