Apache Hudi 与 Hive 集成手册
阅读原文时间:2021年12月13日阅读:1

Hudi源表对应一份HDFS数据,可以通过Spark,Flink 组件或者Hudi客户端将Hudi表的数据映射为Hive外部表,基于该外部表, Hive可以方便的进行实时视图,读优化视图以及增量视图的查询。

这里以Hive3.1.1、 Hudi 0.9.0为例, 其他版本类似

  • 将hudi-hadoop-mr-bundle-0.9.0xxx.jar , hudi-hive-sync-bundle-0.9.0xx.jar 放到hiveserver 节点的lib目录下

  • 修改hive-site.xml找到hive.default.aux.jars.path 以及hive.aux.jars.path 这两个配置项,将第一步中的jar包全路径给配置上去: 配置后如下

    <name>hive.default.aux.jars.path</name>
    <value>xxxx,jar,xxxx,jar,file:///mypath/hudi-hadoop-mr-bundle-0.9.0xxx.jar,file:///mypath/hudi-hive-sync-bundle-0.9.0xx.jar</value>
  • 配置完后重启hive-server

  • 对于Hudi的bootstrap表(tez查询),除了要添加hudi-hadoop-mr-bundle-0.9.0xxx.jar , hudi-hive-sync-bundle-0.9.0xx.jar这两个jar包,还需把hbase-shaded-miscellaneous-xxx.jar, hbase-metric-api-xxx.jar,hbase-metrics-xxx.jar, hbase-protocol-shaded-xx.jar,hbase-shaded-protobuf-xxx.jar,htrce-core4-4.2.0xxxx.jar 按上述步骤添加进去。

一般来说Hudi表在用Spark或者Flink写入数据时会自动同步到Hive外部表, 此时可以直接通过beeline查询同步的外部表, 若写入引擎没有开启自动同步,则需要手动利用hudi客户端工具run_hive_sync_tool.sh 进行同步具体可以参考官网查看相关参数。

4.1 操作前提

使用Hive查询Hudi表前,需要通过set命令设置hive.input.format,否则会出现数据重复,查询异常等错误,如下面这个报错就是典型的没有设置hive.input.format 导致的

java.lang.IllegalArgumentException: HoodieRealtimeReader can oly work on RealTimeSplit and not with xxxxxxxxxx

除此之外对于增量查询,还需要set命令额外设置3个参数

set hoodie.mytableName.consume.mode=INCREMENTAL;
set hoodie.mytableName.consume.max.commits=3;
set hoodie.mytableName.consume.start.timestamp=commitTime;

注意这3个参数是表级别参数

参数名

描述

hoodie.mytableName.consume.mode

Hudi表的查询模式。 增量查询 :INCREMENTAL非增量查询:不设置或者设为SNAPSHOT

hoodie.mytableName.consume.start.timestamp

Hudi表增量查询起始时间

hoodie. mytableName.consume.max.commits

Hudi表基于hoodie.mytableName.consume.start.timestamp 之后要查询的增量commit次数。提交次数,如设置为3时,代表增量查询从指定的起始时间之后commit 3次的数据,设为-1时,增量查询从指定的起始时间之后提交的所有数据

4.2 COW类型Hudi表的查询

例如Hudi原表表名为hudicow,同步给hive之后hive表名hudicow

4.2.1 COW表实时视图查询

设置hive.input.format 为org.apache.hadoop.hive.ql.io.HiveInputFormat或者org.apache.hudi.hadoop.hive.HoodieCombineHiveInputFormat后,像普通的hive表一样查询即可

set hive.input.format= org.apache.hadoop.hive.ql.io.HiveInputFormat;

select count(*) from hudicow;

4.2.2 COW表增量查询

除了要设置hive.input.format,还需要设置上述的3个增量查询参数,且增量查询语句中的必须添加where 关键字并将_hoodie_commit_time > 'startCommitTime'作为过滤条件(这地方主要是hudi的小文件合并会把新旧commit的数据合并成新数据,hive是没法直接从parquet文件知道哪些是新数据哪些是老数据)

set hive.input.format = org.apache.hadoop.hive.ql.io.HiveInputFormat;
set hoodie.hudicow.consume.mode = INCREMENTAL;
set hoodie.hudicow.consume.max.commits = 3;
set hoodie.hudicow.consume.start.timestamp = xxxx;
select count(*) from hudicow where `_hoodie_commit_time` > 'xxxx'

注意_hoodie_commit_time 的引号是反引号(tab键上面那个)不是单引号, 'xxxx'是单引号

4.3 MOR类型Hudi表的查询

例如mor类型Hudi源表的表名为hudimor,映射为两张Hive外部表hudimor_ro(ro表)和hudimor_rt(rt表)

4.3.1 MOR表读优化视图

实际上就是读 ro表,和cow表类似设置完hiveInputFormat 之后 和普通的hive表一样查询即可。

4.3.2 MOR表实时视图

设置了hive.input.format之后,即可查询到Hudi源表的最新数据

set hive.input.format = org.apache.hadoop.hive.ql.io.HiveInputFormat;
select * from hudicow_rt;

4.3.3 MOR表增量查询

这个增量查询针对的rt表,不是ro表。通COW表的增量查询类似

set hive.input.format = org.apache.hudi.hadoop.hive.HoodieCombineHiveInputFormat; // 这地方指定为HoodieCombineHiveInputFormat
set hoodie.hudimor.consume.mode = INCREMENTAL;set hoodie.hudimor.consume.max.commits = -1;
set hoodie.hudimor.consume.start.timestamp = xxxx;
select * from hudimor_rt where `_hoodie_commit_time` > 'xxxx'; // 这个表名要是rt表

说明如下

  • set hive.input.format=org.apache.hudi.hadoop.hive.HoodieCombineHiveInputFormat;最好只用于rt表的增量查询,当然其他种类的查询也可以设置为这个,这个参数会影响到普通的hive表查询,因此在rt表增量查询完成后,应该设置set hive.input.format=org.apache.hadoop.hive.ql.io.HiveInputFormat;或者改为默认值set hive.input.format=org.apache.hadoop.hive.ql.io.CombineHiveInputFormat;用于其他表的查询。

  • set hoodie.mytableName.consume.mode=INCREMENTAL;仅用于该表的增量查询模式,若要对该表切换为其他查询模式,应设置set hoodie.hudisourcetablename.consume.mode=SNAPSHOT;

当前Hudi(0.9.0)对接Hive的一些问题,请使用master分支或即将发布的0.10.0版本

  • hive读hudi表会将所有的数据给打印出来有严重的性能问题和数据安全问题。

  • MOR表的实时视图读取 请按需设置mapreduce.input.fileinputformat.split.maxsize的大小 禁止hive取切分读取的文件,否则会出现数据重复。这个问题当前是无解的,spark读hudi实时视图的时候代码直接写死不会切分文件,hive需要手动设置。

  • 如果碰到classNotFound, noSuchMethod等错误请检查hive lib库下面的jar包是否出现冲突。

为支持Hive查询Hudi的纯log文件需要对Hive侧源码进行修改。

具体修改org.apache.hadoop.hive.common.FileUtils 如下函数

public static final PathFilter HIDDEN_FILES_PATH_FILTER = new PathFilter() {&nbsp; &nbsp;
  @Override&nbsp; &nbsp;
  public boolean accept(Path p) {&nbsp; &nbsp; &nbsp;
    String name = p.getName();&nbsp; &nbsp; &nbsp;
    boolean isHudiMeta = name.startsWith(".hoodie");&nbsp; &nbsp; &nbsp;
    boolean isHudiLog = false;&nbsp; &nbsp; &nbsp;
    Pattern LOG_FILE_PATTERN = Pattern.compile("\\.(.*)_(.*)\\.(.*)\\.([0-9]*)(_(([0-9]*)-([0-9]*)-([0-9]*)))?");&nbsp; &nbsp; &nbsp;
    Matcher matcher = LOG_FILE_PATTERN.matcher(name);&nbsp; &nbsp; &nbsp;
    if (matcher.find()) {&nbsp; &nbsp; &nbsp; &nbsp;
      isHudiLog = true;&nbsp; &nbsp; &nbsp;
    }&nbsp; &nbsp; &nbsp;
    boolean isHudiFile = isHudiLog || isHudiMeta;&nbsp; &nbsp; &nbsp;
    return (!name.startsWith("_") && !name.startsWith(".")) || isHudiFile;&nbsp; &nbsp;
  }&nbsp;
};

重新编译hive, 把新编译的hive-common-xxx.jar, hive-exec-xxx.jar 替换到hive server的lib目录下注意权限和名字和原来的jar包保持一致。

最后重启hive-server即可。

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章