目标:将Hive中已经存在的Lzo压缩格式表转换为Orc格式,并保证数据不丢失
执行与测试过程:
1. 创建lzo相关表:(验证过程,可忽略)
create external table test_lzo(
id int
)partitioned by(`date_par` string)
ROW FORMAT SERDE
'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe'
STORED AS INPUTFORMAT
'com.hadoop.mapred.DeprecatedLzoTextInputFormat'
OUTPUT FORMAT
'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat';
2. 插入数据:(验证过程,可忽略)
insert into table test_lzo partition (date_par='20190820') values(111);
insert into table test_lzo partition (date_par='20190820') values(222);
insert into table test_lzo partition (date_par='tttt') values(123);
3. 查看数据需要设置参数:(验证过程,可忽略)
set hive.exec.compress.output = true;
set mapred.output.compression.codec=com.hadoop.compression.lzo.LzopCodec;
4. 创建替换表, 指定路径为原表路径:
create external table test_lzo_new(
id int)partitioned by(`date_par` string)
STORED AS ORCFILE
LOCATION
'hdfs://hacluster/user/hive/warehouse/test_lzo';
5. 针对分区表需要开启特定参数:(分区表需要关注)
--允许使用动态分区可通过set hive.exec.dynamic.partition;查看
set hive.exec.dynamic.partition=true;
--当需要设置所有列为dynamic时需要这样设置
set hive.exec.dynamic.partition.mode=nonstrict;
--如果分区总数超过这个数量会报错
set hive.exec.max.dynamic.partitions=100000;
--单个MR Job允许创建分区的最大数量
set hive.exec.max.dynamic.partitions.pernode=100000;
6. 使用insert overwrite 语句将数据重新插入到替换表:
- 分区表:insert overwrite table test_lzo_new partition(date_par) select * from test_lzo;
- 非分区表:insert overwrite table test_lzo_new select * from test_lzo;
插入后新表分区数据正常,旧表数据被替换
7. 删除旧表:
drop table test_lzo_new;
8. 重新命名新表:
alter table test_lzo_new rename to test_lzo;
9. 全部删除后,如果有必要,删除有关lzo相关的配置:(删除后需要hive重启)
core-hive.xml
<property>
<name>io.compression.codecs</name>
<value>com.hadoop.compression.lzo.LzoCodec,com.hadoop.compression.lzo.LzopCodec</value>
</property>
<property>
<name>io.compression.codec.lzo.class</name>
<value>com.hadoop.compression.lzo.LzoCodec</value>
</property>
hive-site.xml
<name>hive.aux.jars.path</name>
file:///opt/huawei/Bigdata/hive-0.13.1/hive-0.13.1/lib/hadoop-lzo-0.4.15.jar