当前位置:网站首页>数据湖(十一):Iceberg表数据组织与查询
数据湖(十一):Iceberg表数据组织与查询
2022-07-16 11:31:00 【Lanson】
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再找到要读取的数据文件。
边栏推荐
- How to choose MySQL database storage engine? Let's take a look at the performance test
- It's getting more and more difficult. If you can't, you have to listen more times
- Grab efficiency! What has the CTO of this enterprise done right to shorten the cloud planning time by half?
- No 996, no involution, LETV has a "fairy day"?
- 抢效率!上云规划时间缩短一半,这家企业的CTO做对了什么?
- 873. 最长的斐波那契子序列的长度
- Enough speed, passion! The July 2022 Amazon deeperacer competition is high-profile
- 记录一个温度曲线的View
- 从物理转 AI 、战数据库,95后程序员的职业选择
- 浅析websocket劫持
猜你喜欢

Stc8h Development (XIV): I2C Drive rx8025t high precision Real time clock chip

图片验证,滑块验证解决
![[machine learning] logic regression principle and code](/img/7b/b7180a7a1cfb60f2cc73aeff7436b8.png)
[machine learning] logic regression principle and code

2022年全球職業教育行業發展報告

Create and generate WiFi QR code mobile phone scanning link

支付宝沙箱测试手机网站支付,提示商户合作协议已到期,无法继续使用

Introduction and simple application of thymeleaf

错误:The source of the existing CacheManager is: URLConfigurationSource

【华为联机对战】下载运行华为官方Unity示例代码,提示鉴权失败并返回错误码100114

污水排放监控,环保数采仪助力城市黑臭水体治理
随机推荐
Kotlin classes and interfaces
国际NFT交易所排行榜前10名
2022年全球職業教育行業發展報告
Architecture layering of standard web system of Architecture Series
【快应用】快应用用户协议、隐私政策内容中可以多次跳转,点击返回未能返回上一级页面,该如何处理?
STC8H開發(十四): I2C驅動RX8025T高精度實時時鐘芯片
Pytest+allure custom report
View for recording a temperature curve
糖尿病遗传风险检测挑战赛-Coggle 30 Days of ML
IPFs record
如何通过客户价值分析让银行收入倍增
From physics to AI and war database, the career choice of post-95 programmers
无心剑中译扎加耶夫斯基《试着赞美这残缺的世界》
OSPF(Open Shortest Path First開放式最短路徑優先)防環以及計算錯誤帶來的組網應用隱患
10 pictures teach you synchronous and asynchronous (Reprint)
Image verification, slider verification solution
[quick application] quick application user agreement and privacy policy content can jump many times. Click back and fail to return to the previous page. How to deal with it?
Huawei cloud x Black Lake is a system to solve all kinds of production difficulties in the manufacturing industry!
It's getting more and more difficult. If you can't, you have to listen more times
醋酸氯霉素的实验室程序&参考文献