当前位置:网站首页>数据湖(十一):Iceberg表数据组织与查询
数据湖(十一):Iceberg表数据组织与查询
2022-06-30 12:33:00 【Lansonli】

文章目录
二、在Hive中创建Iceberg表并插入数据
三、查看Iceberg底层数据存储
Iceberg表数据组织与查询
一、下载avro-tools jar包
由于后期需要查看avro文件内容,我们可以通过avro-tool.jar来查看avro数据内容。可以在以下网站中下载avro-tools对应的jar包,下载之后上传到node5节点上:
“https://mvnrepository.com/artifact/org.apache.avro/avro-tools”。
查看avro文件信息可以直接执行如下命令,可以将avro中的数据转换成对应的json数据。
[[email protected] ~]# java -jar /software/avro-tools-1.8.1.jar tojson snap-*-wqer.avro二、在Hive中创建Iceberg表并插入数据
在Hive中创建Iceberg格式表,并插入如下数据:
#在Hive中创建iceberg格式表
create table test_iceberg_tbl1(
id int ,
name string,
age int)
partitioned by (dt string)
stored by 'org.apache.iceberg.mr.hive.HiveIcebergStorageHandler';
#插入如下数据
insert into test_iceberg_tbl1 values (1,"zs",21,"20211212");
insert into test_iceberg_tbl1 values (2,"ls",22,"20211212");
insert into test_iceberg_tbl1 values (3,"ww",23,"20211213");
insert into test_iceberg_tbl1 values (4,"ml",24,"20211213");
insert into test_iceberg_tbl1 values (5,"tq",25,"20211213");三、查看Iceberg底层数据存储
下图为Iceberg表“test_iceberg_tbl1”在HDFS中存储的数据组织图:

通过上图我们可以看到有5个Snapshot快照,以上5个Snapshot实际上就是对应了5个Manifest list清单列表。
1、查询最新快照数据
为了了解Iceberg如何查询最新数据,可以参照下面这张图来详细了解底层实现。

查询Iceberg表数据时,首先获取最新的metadata信息,这里先获取到“00000-*ec504.metadata.json”元数据信息,解析当前元数据文件可以拿到当前表的快照id:“949358624197301886”以及这张表的所有快照信息,也就是json信息中snapshots数组对应的值。根据当前表的快照id值可以获取对应的snapshot对应的avro文件信息:“snap-*-32800.avro”,我们可以找到当前快照对应的路径,看到其包含的Manifest 清单文件有5个:"*32800-m0.avro"、"*2abba-m0.avro"、"*d33de-m0.avro"、"*748bf-m0.avro"、"*b946e-m0.avro",读取该Iceberg格式表最新数据就是读取这几个文件中描述对应的parquet数据文件即可。
我们可以看到“snap-*-32800.avro”快照文件中不仅有包含的manifest路径信息,还有“added_data_files_count”、“existing_data_files_count”、“deleted_data_files_count”三个属性,Iceberg 根据 deleted_data_files_count 大于 0 来判断对应的manifest清单文件里面是不是被删除的数据,如果一个manifest清单文件该值大于0代表数据删除,读数据时就无需读这个manifest清单文件对应的数据文件。
根据Manifest list找到了各个对应的manifest 清单文件,每个文件中描述了对应parquet文件存储的位置信息,可以看到在对应的avro文件中有“status”属性,该属性为1代表对应的parquet文件为新增文件,需要读取,为2代表parquet文件被删除。
2、查询某个快照的数据
Apache Iceberg支持查询历史上任何时刻的快照,在查询时需要指定snapshot-id属性即可,这个只能通过Spark/Flink来查询实现,例如在Spark中查询某个快照数据如下:
spark.read.option("snapshot-id",6155408340798912701L).format("iceberg").load("path")查询某个快照数据的原理如下图所示(以查询快照id为“6155408340798912701”的数据为例):

通过上图可以看出,实际上读取历史快照数据和读取最新数据不同之处就是找到的snapshot-id不同而已,原理都是一样。
3、根据时间戳查看某个快照的数据
Apache iceberg还支持通过as-of-timestamp参数执行时间戳来读取某个快照的数据,同样也是通过Spark/Flink来读取,Spark读取代码如下:
spark.read.option("as-of-timestamp","时间戳").format("iceberg").load("path")实际上通过时间戳找到对应数据文件的原理与通过snapshot-id找到数据文件原理一样,在*.metadata.json文件中,除了有“current-snapshot-id”、“snapshots”属性外还有“snapshot-log”属性,该属性对应的值如下:

我们可以看到其中有个 timestamp-ms 属性和 snapshot-id 属性,并且是按照 timestamp-ms 升序的。在 Iceberg 内部实现中,它会将 as-of-timestamp 指定的时间和 snapshot-log 数组里面每个元素的 timestamp-ms 进行比较,找出最后一个满足 timestamp-ms <= as-of-timestamp 对应的 snapshot-id,原理同上,通过snapshot-id再找到要读取的数据文件。
- 博客主页:https://lansonli.blog.csdn.net
- 欢迎点赞 收藏 留言 如有错误敬请指正!
- 本文由 Lansonli 原创,首发于 CSDN博客
- 停下休息的时候不要忘了别人还在奔跑,希望大家抓紧时间学习,全力奔赴更美好的生活
边栏推荐
- 【一天学awk】基础中的基础
- MySQL implements the division of two query results
- 数据仓库建设之确定主题域
- Shell基础入门
- MATLAB小技巧(22)矩阵分析--逐步回归
- Tencent cloud Database Engineer competency certification was launched, and people from all walks of life talked about talent training problems
- Golang Basics - string and int, Int64 inter conversion
- 黑马笔记---常用日期API
- ECDSA signature verification in crypt
- Wechat launched the picture big bang function; Apple's self-developed 5g chip may have failed; Microsoft solves the bug that causes edge to stop responding | geek headlines
猜你喜欢

【OpenGL】OpenGL Examples

【惊了】迅雷下载速度竟然比不上虚拟机中的下载速度

【300+精选大厂面试题持续分享】大数据运维尖刀面试题专栏(二)

Android development interview real question advanced version (with answer analysis)

MATLAB小技巧(22)矩阵分析--逐步回归

电机控制Clarke(α/β)等幅值变换推导

Postman automatically generates curl code snippets

微信小程序报错:TypeError: Cannot read property ‘setData‘ of undefined

Js根据相同值将数组转换为二维数组

【OpenGL】OpenGL Examples
随机推荐
黑马笔记---集合(Collection的常用方法与遍历方式)
Qt读写Excel--QXlsx工作表显示/隐藏状态设置4
Visual studio configures QT and implements project packaging through NSIS
[QNX Hypervisor 2.2用户手册]6.2.3 Guest与外部之间通信
【一天学awk】基础中的基础
Substrate 源码追新导读: Call调用索引化, 存储层事物化全面完成
zabbix-server启动失败处理方式
Introduction to the renewal of substrate source code: the pledge amount is greatly reduced, and rocksdb can be completely disabled
2022-06-23 帆软部分公式及sql生成(月份、季度取数)
时空预测2-GCN_LSTM
Derivation of Park transformation formula for motor control
Matlab tips (22) matrix analysis -- stepwise regression
常用的ui组件
Determining the subject area of data warehouse construction
Terms related to JMeter performance test and performance test passing standards
Basic interview questions for Software Test Engineers (required for fresh students and test dishes) the most basic interview questions
Apple executives openly "open the connection": Samsung copied the iPhone and only added a large screen
Unity脚本的基础语法(4)-访问其他游戏对象
Substrate 源码追新导读: 修复BEEFY的gossip引擎内存泄漏问题, 智能合约删除队列优化
Discussion on JMeter operation principle