-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
688 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...er-iceberg/src/main/scala/com/adform/streamloader/iceberg/IcebergRecordBatchStorage.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright (c) 2020 Adform | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package com.adform.streamloader.iceberg | ||
|
||
import com.adform.streamloader.model.{StreamPosition, Timestamp} | ||
import com.adform.streamloader.sink.batch.storage.InDataOffsetBatchStorage | ||
import org.apache.iceberg.Table | ||
import org.apache.kafka.common.TopicPartition | ||
|
||
/** | ||
* Iceberg record batch storage that appends multiple files and stores Kafka offsets in table properties | ||
* in a single atomic table transaction. | ||
*/ | ||
class IcebergRecordBatchStorage(table: Table) extends InDataOffsetBatchStorage[IcebergRecordBatch] { | ||
|
||
private def offsetKey(topic: String, partition: Int): String = { | ||
s"__consumer_offset:${kafkaContext.consumerGroup}:$topic:$partition" | ||
} | ||
|
||
override def recover(topicPartitions: Set[TopicPartition]): Unit = {} | ||
|
||
override def commitBatchWithOffsets(batch: IcebergRecordBatch): Unit = { | ||
val transaction = table.newTransaction() | ||
|
||
batch.dataWriteResult.dataFiles().forEach(file => transaction.newAppend().appendFile(file).commit()) | ||
|
||
batch.recordRanges.foreach(range => { | ||
transaction | ||
.updateProperties() | ||
.set(offsetKey(range.topic, range.partition), s"${range.end.offset}:${range.end.watermark.millis}") | ||
.commit() | ||
}) | ||
|
||
transaction.commitTransaction() | ||
log.info(s"Successfully commited Iceberg transaction for ranges ${batch.recordRanges.mkString(",")}") | ||
} | ||
|
||
override def committedPositions(topicPartitions: Set[TopicPartition]): Map[TopicPartition, Option[StreamPosition]] = { | ||
topicPartitions | ||
.map(tp => { | ||
tp -> Option(table.properties().get(offsetKey(tp.topic(), tp.partition()))).map(offsetWatermark => { | ||
val Array(o, w) = offsetWatermark.split(':') | ||
StreamPosition(o.toLong + 1, Timestamp(w.toLong)) | ||
}) | ||
}) | ||
.toMap | ||
} | ||
} |
Oops, something went wrong.