Sqoop支持ORC文件格式

ORC介绍

ORC文件格式是Hive 0.11.0版本引入的一种文件格式。ORC的引入是为了解决其他Hive文件格式的局限性。使用ORC文件格式提升Hive读取、写入及处理数据的性能。

与RCFile对比,ORC文件格式有很多优点:

  • 每个Task只输出一个文件,降低NameNode的负载。
  • Hive数据类型支持,包括:datetime、decimal以及复杂数据类型(struct、list、map、union)。
  • 文件中存储轻量级的索引:
    • 跳过不通过谓语过滤的行组
    • 跳转到指定的行
  • 基于数据类型的块模式压缩:
    • 整型数据列采用行程长度编码(run-length encoding)
    • 字符串数据列采用词典编码(dictionary encoding)
  • 使用独立的RecordReader并发读取相同的文件
  • 无需扫描markers就可以分割文件的能力
  • 绑定读写需要的内存量
  • 使用Protocol Buffer存储元数据,允许添加、移除字段

Hive官网介绍:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+ORC

Sqoop支持ORC

通过Sqoop-HCatalog集成解决Sqoop不支持ORC的问题。

HCatalog背景

HCatalog是Hadoop的一个table与存储管理的一个服务,用户可以更容易地使用不同的数据处理工具Pig、MapReduce和Hive读写数据。HCatalog表的抽象呈现给用户一个HDFS分布式文件系统(HDFS)中的关系视图,用户不需要担心数据存储在哪里及数据的存储格式:RCFile格式、text文件、或者SequenceFile。

HCatalog支持读写任何实现了Hive SerDe(serializer-deserializer)的文件格式。默认的,HCatalog支持RCFile、CSV、JSON和SequenceFile。要使用用户自定义格式,必须提供InputFormat和OutputFormat及SerDe。

Sqoop使用HCatalog抽象不同存储格式的能力来支持RCFile(以及未来的文件类型)。

集成HCatalog后新增的参数

见Sqoop官方文档:http://sqoop.apache.org/docs/1.4.6/SqoopUserGuide.html#_sqoop_hcatalog_integration

定制Sqoop改造

修改bin/sqoop命令脚本:为import操作增加Hive Table分区数据删除逻辑,在执行导入前不需要另行清理数据,从而简化Sqoop import脚本的开发工作。

Sqoop导入ORC实例

第一步:创建Hive表

1
2
3
4
5
6
7
8
9
10
11
12
13
CREATE TABLE `dev.bims_device`(
`code` string,
`mac` string,
`wifi_mac` string,
`create_date` string,
`activate_date` string,
`state` string,
`area_id` bigint,
`city_id` bigint,
`sp_define_id` bigint)
PARTITIONED BY (
`dt` string)
stored as orc;

第二步:Sqoop import

1
2
3
4
5
6
7
8
9
10
sqoop import \
--hcatalog-database dev \
--hcatalog-table bims_device \
--hcatalog-partition-keys dt \
--hcatalog-partition-values ${dt} \
--connect jdbc:mysql://${host}:${port}/bims \
--username "${userName}" \
--password "${password}" \
--table bims_device \
--columns "code,mac,wifi_mac,create_date,activate_date,state,area_id,city_id,sp_define_id"

Sqoop导出ORC实例

第一步:创建MySQL数据表

1
2
3
4
5
6
7
8
9
10
CREATE TABLE `test`.`bims_device`(
`code` varchar(50),
`mac` varchar(50),
`wifi_mac` varchar(50),
`create_date` varchar(50),
`activate_date` varchar(50),
`state` varchar(50),
`area_id` bigint,
`city_id` bigint,
`sp_define_id` bigint);

第二步:Sqoop export

1
2
3
4
5
6
7
8
9
sqoop export \
--hcatalog-database dev \
--hcatalog-table bims_device \
--hcatalog-partition-keys dt \
--hcatalog-partition-values ${dt} \
--connect jdbc:mysql://${host}:${port}/test?useCompression=true \
--username ${userName} \
--password ${password} \
--table bims_device

注意事项

  • MySQL表字段名称必须与Hive仓库中的表字段名称一致
  • Sqoop ORC导出时,数据类型需要转换时如果存在脏数据会导致导出失败。例如,长度为0的String如果导出为int时。
  • 导出时需要提前清理MySQL中之前导出的数据,避免重复运行时造成数据重复。