当前位置:网站首页>数据湖(十一):Iceberg表数据组织与查询
数据湖(十一):Iceberg表数据组织与查询
2022-07-02 10: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再找到要读取的数据文件。
边栏推荐
- [usaco05jan]watchcow s (Euler loop)
- Student course selection information management system based on ssm+jsp framework [source code + database]
- BeanUtils--浅拷贝--实例/原理
- The xftp connection Haikang camera reported an error: the SFTP subsystem application has been rejected. Please ensure that the SFTP subsystem settings of the SSH connection are valid
- Mysql5.7 installation super easy tutorial
- Pointer from entry to advanced (1)
- In 2021, the global styrene butadiene styrene (SBS) revenue was about $3722.7 million, and it is expected to reach $5679.6 million in 2028
- 使用BLoC 构建 Flutter的页面实例
- 万物生长大会在杭召开,当贝入选2022中国未来独角兽TOP100榜单
- Multi rotor aircraft control using PID and LQR controllers
猜你喜欢
Daily practice of C language --- monkeys divide peaches
Pointer from entry to advanced (1)
当贝投影4K激光投影X3 Pro获得一致好评:万元投影仪首选
[技术发展-22]:网络与通信技术的应用与发展快速概览-2- 通信技术
qt中uic的使用
[cloud native database] what to do when encountering slow SQL (Part 1)?
Use bloc to build a page instance of shutter
2022家用投影仪首选!当贝F5强悍音画效果带来极致视听体验
千元投影小明Q1 Pro和极米NEW Play谁更好?和哈趣K1比哪款配置更高?
Student course selection information management system based on ssm+jsp framework [source code + database]
随机推荐
selenium的特点
[template] longest common subsequence ([DP or greedy] board)
Daily practice of C language --- monkeys divide peaches
MySQL 45 lecture - learning from the actual battle of geek time MySQL 45 Lecture Notes - 04 | easy to understand index (Part 1)
Tupang multi-target tracking! BOT sort: robust correlated multi pedestrian tracking
基于ssm+jsp框架实现的学生选课信息管理系统【源码+数据库】
Sum of the first n terms of Fibonacci (fast power of matrix)
Which do you choose between Alibaba P7 with an annual salary of 900000 and deputy department level cadres?
Pattern matching and regular expressions in PostgreSQL - Das
使用BLoC 构建 Flutter的页面实例
Qt入门-制作一个简易的计算器
Development skills of rxjs observable custom operator
QT new project_ MyNotepad++
Codeforces Round #803 (Div. 2)(A~D)
Everyone believes that the one-stop credit platform makes the credit scenario "useful"
P1908 reverse sequence pair
Countermeasures for the failure of MMPV billing period caused by negative inventory of materials in SAP mm
Getting started with QT - making a simple calculator
On flow delivery between microservices
QT how to set fixed size