不同系统、不同存储格式(textfile, parquet)数据的传递


 描述:

本地测试环境hive中有数据,存储格式为textfile,现在要上传到公司开发环境,存储格式为parquet, 如何实现???

tb_textfile表---> local file --->tb_parquet(❌)

tb_textfile表---> local file --->tb_textfile_tmp ---> tb_parquet(✔️)

[因为是不同的系统,不能直接将tb_textfile表中的数据导入tb_parquet中,中间需要先导出到本地文件]

--建表tb_textfile:指明分隔符,textfile存储
create table if not exists tb_textfile(id int, name string) partitioned by(time string) row format delimited fields terminated by '\t' stored as textfile;

--加载数据到tb_textfile
insert into tb_textfile partition(time='20180616') values (111,'text111'),(222,'text222'),(333,'text333');

--导出tb_textfile数据到本地文件夹,指明分隔符
insert overwrite local directory '/Users/wooluwalker/Desktop/export_test' row format delimited fields terminated by '\t' select * from tb_textfile;

--目标文件夹export_test中出现 000000_0 文件

--cat /Users/wooluwalker/Desktop/export_test/000000_0

        111    text111    20180616
        222    text222    20180616
        333    text333    20180616

--创建tb_parquet表,指明分隔符,parquet格式存储        
create table if not exists tb_parquet(id int, name string) partitioned by(time string) row format delimited fields terminated by '\t' stored as parquet;

--上传export_test目录中的数据到hive的tb_parquet表
load data local inpath '/Users/wooluwalker/Desktop/export_test/000000_0' into table tb_parquet partition(time='20180616');
--查看上传的数据
select * from tb_parquet;
返回的结果是:
Failed with exception java.io.IOException:java.lang.RuntimeException: 
hdfs://0.0.0.0:9000/user/hive/warehouse/hivetest.db/tb_parquet/time=20180616/000000_0 is not a Parquet file. 
expected magic number at tail [80, 65, 82, 49] but found [54, 49, 54, 10]

由此证明,不能将textfile格式存储的表所导出的文件,直接上传到 parquet格式的表中
解决方式: 将export_test目录中的数据到hive的textfile格式存储的表,然后再由此表导出数据到parquet中
-- 上一步上传的数据格式不对,需要先清空,否则无法select truncate table tb_parquet; --创建textfile格式的中间表tb_textfile_tmp,指明分隔符,存储格式为textfile create table if not exists tb_textfile_tmp(id int, name string) partitioned by(time string) row format delimited fields terminated by '\t' stored as textfile; --上传数据到textfile格式的中间表中 load data local inpath '/Users/wooluwalker/Desktop/export_test/000000_0' into table tb_textfile_tmp partition(time='20180616'); --将textfile格式的中间表数据导出到parquet格式的目标表 tb_parquet insert into tb_parquet partition(time='20180616') select id, name from tb_textfile_tmp; --查看表数据 select * from tb_parquet; 111 text111 20180616 222 text222 20180616 333 text333 20180616

 hive编程指南 中讲,‘不管源表中数据如何存储,hive会将所有字段序列化生成字符串写入到文件中,hive使用和hive内存存储的表相同的编码方式来生成输出文件’,因此textfile导出的文件不能导入parquet表中


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM