hive中分区表创建:hive中 PARTITIONED BY 创建表分区
hive中创建表时,可以通过PARTITIONED BY来创建分区,语法:
分区表的分区字段,是虚拟字段,不是数据本身。实际上是数据存储的路径目录名称。
create table database.tablename(
uid bigint comment ‘主键’,
name string,
)
PARTITIONED BY (
ds string
)ROW FORMAT DELIMITED FIELDS TERMINATED BY ‘\t’ LINES TERMINATED BY ‘\n’ ;
在hive建表时,初始化设置存储格式, 字段\t分割, \n换行。在建表语句后通过PARTITIONED BY 关键字设计分区。分区的字段不必出现在基础的数据中。
分区表插入数据
当hive中的数据表没设置分区时, insert 插入数据的方式与mysql方式一致。
当hive中的表设置了分区时,insert时需要设置分区, 如果不设置分区,会提示Need to specify partition columns because the destination table is partition错误。
分区表写入数据有两种方式, insert..select 和 load 加载本地文件
insert 插入数据时设置分区,语法:
insert into tablename partition(ds = ‘ 2020-07-01 ‘) select uid, name from table2 ;
hive中插入数据有insert into 和 insert overwrite 两种方式,一种是追加写入,一种是将已有的分区数据删除重写。
insert overwrite table table_name partition(ds = ‘2020-11-22’)
select col1,col2…. from
table
本地load加载数据
load data local inpath ‘..filepath’ [overwrite] into table tablename partition(ds = ‘2020-11-22’)
本地load文件时,需要注意的是,文件的字段间分割,要与建立表时的分割符保持一致, 常用\t 分割,\n换行
当表为分区表示, 在tablename 后面跟上partition(分区字段= ”)
into 和 overwrite into , 追加写 和 覆盖写 两组方式。
测试
hive> create table dzphoenix.cyb_tmp_test(
> uid bigint,
> name string
> )
> PARTITIONED BY (
> ds string
> );
hive> insert into dzphoenix.cyb_tmp_test partition(ds = ‘2020-07-01’) values(12,’abc’);
select * from dzphoenix.cyb_tmp_test
where ds = ‘2020-07-01’
字符引号是英文‘
hive数据表扩容,增加字段
直接用alter table,可以在表后增加字段。
alter table tablename add columns(col string)
hive的数据表一般作为数据仓库使用,用来存在大量的数据,不支持数据的修改。扩容字段后,需要往字段内填补数据,需要对数据表进行全量更新,重跑数据。
hive中复制表
在hive复制表时,首先需要复制一张表结构, 在将原表路径下的文件, hadoop fs -cp 拷贝到目标表的路径下, 然后MSCK REPAIR刷新下元数据。
create table t2 like tl
hadoop fs -cp hdfs://t1路径/* hdfs://t2路径/
MSCK REPAIR TABLE t2
动态分区
在严格模式下,动态分区基本为关闭状态。需要时需要手动开启。
set hive.exec.dynamic.partition=true ;
set hive.exec.dynamic.partition.mode=nonstrict;
开启动态分区后,可以往分区表中一次性写入多个分区
特么能不能自己先验证一下
抱歉,我重新验证了下,不对的地方修改了