deltalake的merge场景是怎么样的


deltalake的merge场景是怎么样的,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。下面主要是讲merge操作的四个案例。1.数据去重实际上,线上业务很多时候数据源在上报数据的时候,由于各种原因可能会重复上报数据,这就会导致数据重复,使用merge函数可以避免插入重复的数据。具体操作方法如下:
sqlscala注意:需要写入delta lake表的dataset自身要完成去重的 操作。我们可以通过merge语义区实现新数据和delt 香港云主机a lake表中已有的数据之间去重,但是如果新的dataset内部有重复数据,重复数据依然会被插入。因此在写入新数据之前一定要完成去重操作。如果数据确定可能会在某些时间周期内重复,那么可以对目标表进行按照时间分区,这样就可以在merge操作的时候指定时间范围。sqlscala
这种利用分区进行谓词下推,可以大幅减少数据加载的量,进而提升速度。此外,对于Structured Streaming可以使用insert-only merge操作来实现连续不断的去重操作。主要有以下场景:
a.对于一些streaming操作,可以在foreachBatch操作来实现连续不断的将数据写入delta lake表,同时具有去重的功能。
b.对于另一些流查询,你可以连续不断的从delta lake表中读取去重的数据。可以这么做的原因是insert-only merge操作仅仅会追加新的数据到delta lake表中。2.渐变纬度数据
另一个常见的操作是SCD Type 2,它维护对维表中每个key所做的所有变更的历史记录。此类操作需要更新现有行以将key的先前值标记为旧值,并插入新行作为最新值。给定具有更新的源表和具有维度数据的目标表,可以使用merge表达SCD type 2。维护客户地址历史记录以及每个地址的有效日期范围,是本小节常见的示例操作。当需要更新客户的地址时,必须将先前的地址标记为不是当前地址,更新其有效日期范围,然后将新地址添加为当前地址。scala的表达方法如下:val customersTable: DeltaTable = ... // table with schema (customerId, address, current, effectiveDate, endDate)
val updatesDF: DataFrame = ... // DataFrame with schema (customerId, address, effectiveDate)
// Rows to INSERT new addresses of existing customersval newAddressesToInsert = updatesDF .as("updates") .join(customersTable.toDF.as("customers"), "customerid") .where("customers.current = true AND updates.address customers.address")
// Stage the update by unioning two sets of rows// 1. Rows that will be inserted in the whenNotMatched clause// 2. Rows that will either update the current addresses of existing customers or insert the new addresses of new customersval stagedUpdates = newAddressesToInsert .selectExpr("NULL as mergeKey", "updates.*") // Rows for 1. .union( updatesDF.selectExpr("updates.customerId as mergeKey", "*") // Rows for 2. )
// Apply SCD Type 2 operation using mergecustomersTable .as("customers") .merge( stagedUpdates.as("staged_updates"), "customers.customerId = mergeKey") .whenMatched("customers.current = true AND customers.address staged_updates.address") .updateExpr(Map( // Set current to false and endDate to source's effective date. "current" -> "false", "endDate" -> "staged_updates.effectiveDate")) .whenNotMatched() .insertExpr(Map( "customerid" -> "staged_updates.customerId", "address" -> "staged_updates.address", "current" -> "true", "effectiveDate" -> "staged_updates.effectiveDate", // Set current to true along with the new address and its effective date. "endDate" -> "null")) .execute()3.cdc操作
和scd类似,另一个常见的案例是变化数据捕获,也即是常说的CDC,简单来说就是同步外部数据库的变更数据到deta lake。换句话说,对于外部数据库的 update,delete,insert操作,要同时作用于delta 表。这种情况,也可以使用merge操作来实现。
4. 整合foreachBatch实际上在使用delta lake的时候可以结合foreachBatch和merge,来实现复杂的流查询到delta lake表的upsert功能。总共有以下几个场景:
a.在update模式下写流聚合结果到delta lake。这种情况,实际上比Complete模式更加高效。
import io.delta.tables.*
val deltaTable = DeltaTable.forPath(spark, "/data/aggregates")
// Function to upsert microBatchOutputDF into Delta table using mergedef upsertToDelta(microBatchOutputDF: DataFrame, batchId: Long) { deltaTable.as("t") .merge( microBatchOutputDF.as("s"), "s.key = t.key") .whenMatched().updateAll() .whenNotMatched().insertAll() .execute()}
// Write the output of a streaming aggregation query into Delta tablestreamingAggregatesDF.writeStream .format("delta") .foreachBatch(upsertToDelta _) .outputMode("update") .start()b.将数据库变更操作同步到delta lake。该场景就是写变化数据到delta lake,也即是本问第三小节。c.流数据以去重的方式写入delta lake。这个就是本文第一小节。注意:确保foreachBatch中的merge语句是幂等的,因为重新启动流查询可以将对该操作对同一批数据重复执行。当在foreachBatch中使用merge时,流查询的输入数据速率可能会上报为在源处生成数据的实际速率的若干倍数。这是因为merge多次读取输入数据,导致输入指标倍增。如果这是瓶颈,则可以在合并之前缓存批处理DataFrame,然后在合并之后取消缓存。看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注开发云行业资讯频道,感谢您对开发云的支持。

相关推荐: LeetCode如何计算有多少小于当前数字的数字

这篇文章主要介绍了LeetCode如何计算有多少小于当前数字的数字,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。给你一个数组 nums,对于其中每个元素 nums[i],请你统计数组中比它小的所…

免责声明:本站发布的图片视频文字,以转载和分享为主,文章观点不代表本站立场,本站不承担相关法律责任;如果涉及侵权请联系邮箱:360163164@qq.com举报,并提供相关证据,经查实将立刻删除涉嫌侵权内容。

(0)
打赏 微信扫一扫 微信扫一扫
上一篇 10/09 16:54
下一篇 10/09 16:54

相关推荐