简介基于 Ubuntu 玩转 Hudi Docker Demo 系列文章:
《基于 Ubuntu 玩转 Hudi Docker Demo (1)—— 集群安装》
《基于 Ubuntu 玩转 Hudi Docker Demo (2)—— 测试数据写入 Kafka》
《基于 Ubuntu 玩转 Hudi Docker Demo (3)—— Spark写入和查询》
《基于 Ubuntu 玩转 Hudi Docker Demo (4)—— Hive 查询 Hudi 表》
上一篇介绍了《基于 Ubuntu 玩转 Hudi Docker Demo (3)—— Spark写入和查询》,
本文介绍了如何使用 Hive 查询 Hudi 表。
包括:
CopyonWrite 表的快照查询和增量查询MergeonRead 表的快照查询,读优化查询和增量查询
sudo docker exec -it adhoc-2 /bin/bash
进入 hive beeline 客户端beeline -u jdbc:hive2://hiveserver:10000 --hiveconf hive.input.format=org.apache.hadoop.hive.ql.io.HiveInputFormat --hiveconf hive.stats.autogather=false
执行以下Hive SQL:
1、cow 的快照查询
0: jdbc:hive2://hiveserver:10000> select symbol, max(ts) from stock_ticks_cow group by symbol HAVINg symbol = 'GOOG';WARNING: Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions、Consider using a different execution engine (i.e、spark, tez) or using Hive 1.X releases.+---------+----------------------+--+| symbol | _c1 |+---------+----------------------+--+| GOOG | 2018-08-31 10:59:00 |+---------+----------------------+--+1 row selected (3.85 seconds)0: jdbc:hive2://hiveserver:10000> select `_hoodie_commit_time`, symbol, ts, volume, open, close from stock_ticks_cow where symbol = 'GOOG';+----------------------+---------+----------------------+---------+------------+-----------+--+| _hoodie_commit_time | symbol | ts | volume | open | close |+----------------------+---------+----------------------+---------+------------+-----------+--+| 20220211091945389 | GOOG | 2018-08-31 09:59:00 | 6330 | 1230.5 | 1230.02 || 20220211092319844 | GOOG | 2018-08-31 10:59:00 | 9021 | 1227.1993 | 1227.215 |+----------------------+---------+----------------------+---------+------------+-----------+--+2 rows selected (0.206 seconds)
2、cow 的增量查询
0: jdbc:hive2://hiveserver:10000> set hoodie.stock_ticks_cow.consume.mode=INCREMENTAL;No rows affected (0.047 seconds)0: jdbc:hive2://hiveserver:10000> set hoodie.stock_ticks_cow.consume.max.commits=3;No rows affected (0.005 seconds)0: jdbc:hive2://hiveserver:10000> set hoodie.stock_ticks_cow.consume.start.timestamp=20220211091945389;No rows affected (0.003 seconds)0: jdbc:hive2://hiveserver:10000> select `_hoodie_commit_time`, symbol, ts, volume, open, close from stock_ticks_cow where symbol = 'GOOG' and `_hoodie_commit_time` > '20220211091945389';+----------------------+---------+----------------------+---------+------------+-----------+--+| _hoodie_commit_time | symbol | ts | volume | open | close |+----------------------+---------+----------------------+---------+------------+-----------+--+| 20220211092319844 | GOOG | 2018-08-31 10:59:00 | 9021 | 1227.1993 | 1227.215 |+----------------------+---------+----------------------+---------+------------+-----------+--+1 row selected (0.176 seconds)
3、mor 表的读优化查询
0: jdbc:hive2://hiveserver:10000> select symbol, max(ts) from stock_ticks_mor_ro group by symbol HAVINg symbol = 'GOOG';WARNING: Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions、Consider using a different execution engine (i.e、spark, tez) or using Hive 1.X releases.+---------+----------------------+--+| symbol | _c1 |+---------+----------------------+--+| GOOG | 2018-08-31 10:29:00 |+---------+----------------------+--+1 row selected (1.481 seconds)0: jdbc:hive2://hiveserver:10000> select `_hoodie_commit_time`, symbol, ts, volume, open, close from stock_ticks_mor_ro where symbol = 'GOOG';+----------------------+---------+----------------------+---------+------------+-----------+--+| _hoodie_commit_time | symbol | ts | volume | open | close |+----------------------+---------+----------------------+---------+------------+-----------+--+| 20220211092001671 | GOOG | 2018-08-31 09:59:00 | 6330 | 1230.5 | 1230.02 || 20220211092001671 | GOOG | 2018-08-31 10:29:00 | 3391 | 1230.1899 | 1230.085 |+----------------------+---------+----------------------+---------+------------+-----------+--+2 rows selected (0.15 seconds)
4、mor 表的快照查询
0: jdbc:hive2://hiveserver:10000> select symbol, max(ts) from stock_ticks_mor_rt group by symbol HAVINg symbol = 'GOOG';WARNING: Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions、Consider using a different execution engine (i.e、spark, tez) or using Hive 1.X releases.+---------+----------------------+--+| symbol | _c1 |+---------+----------------------+--+| GOOG | 2018-08-31 10:59:00 |+---------+----------------------+--+1 row selected (1.528 seconds)0: jdbc:hive2://hiveserver:10000> select `_hoodie_commit_time`, symbol, ts, volume, open, close from stock_ticks_mor_rt where symbol = 'GOOG';+----------------------+---------+----------------------+---------+------------+-----------+--+| _hoodie_commit_time | symbol | ts | volume | open | close |+----------------------+---------+----------------------+---------+------------+-----------+--+| 20220211092001671 | GOOG | 2018-08-31 09:59:00 | 6330 | 1230.5 | 1230.02 || 20220211092403140 | GOOG | 2018-08-31 10:59:00 | 9021 | 1227.1993 | 1227.215 |+----------------------+---------+----------------------+---------+------------+-----------+--+2 rows selected (0.186 seconds)
5、mor 表的增量查询
0: jdbc:hive2://hiveserver:10000> set hoodie.stock_ticks_mor.consume.mode=INCREMENTAL;No rows affected (0.003 seconds)0: jdbc:hive2://hiveserver:10000> set hoodie.stock_ticks_mor.consume.max.commits=3;No rows affected (0.004 seconds)0: jdbc:hive2://hiveserver:10000> set hoodie.stock_ticks_mor.consume.start.timestamp=20220211092001671;No rows affected (0.004 seconds)0: jdbc:hive2://hiveserver:10000> select `_hoodie_commit_time`, symbol, ts, volume, open, close from stock_ticks_mor_rt where symbol = 'GOOG' and `_hoodie_commit_time` > '20220211092001671';+----------------------+---------+----------------------+---------+------------+-----------+--+| _hoodie_commit_time | symbol | ts | volume | open | close |+----------------------+---------+----------------------+---------+------------+-----------+--+| 20220211092403140 | GOOG | 2018-08-31 10:59:00 | 9021 | 1227.1993 | 1227.215 |+----------------------+---------+----------------------+---------+------------+-----------+--+1 row selected (0.185 seconds)0: jdbc:hive2://hiveserver:10000> select `_hoodie_commit_time`, symbol, ts, volume, open, close from stock_ticks_mor_ro where symbol = 'GOOG' and `_hoodie_commit_time` > '20220211092001671';+----------------------+---------+-----+---------+-------+--------+--+| _hoodie_commit_time | symbol | ts | volume | open | close |+----------------------+---------+-----+---------+-------+--------+--++----------------------+---------+-----+---------+-------+--------+--+No rows selected (0.136 seconds)
stock_ticks_mor_ro 是读优化模式的,查询只返回列存数据文件,在本例中时间戳20220211092001671后的数据还没有进行合并,该时间戳后的数据中在 mor 表还是以行存方式存储在 avro 日志中,因此最后一条Hive SQL 无数据返回。
有关 Hudi 的表类型和查询方式可以阅读此文:《Hudi 表的类型和查询方式》