-
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
13 changed files
with
772 additions
and
13 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
51 changes: 51 additions & 0 deletions
51
...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,51 @@ | ||
/* | ||
* 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.RecordBatchStorage | ||
import com.adform.streamloader.util.Logging | ||
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 RecordBatchStorage[IcebergRecordBatch] with Logging { | ||
|
||
override def recover(topicPartitions: Set[TopicPartition]): Unit = {} | ||
|
||
override def commitBatch(batch: IcebergRecordBatch): Unit = { | ||
val transaction = table.newTransaction() | ||
|
||
batch.dataWriteResult.dataFiles().forEach(file => transaction.newAppend().appendFile(file).commit()) | ||
|
||
batch.recordRanges.foreach(range => { | ||
transaction | ||
.updateProperties() | ||
.set(s"${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(s"${tp.topic()}:${tp.partition()}")).map(offsetWatermark => { | ||
val Array(o, w) = offsetWatermark.split(':') | ||
StreamPosition(o.toLong + 1, Timestamp(w.toLong)) | ||
}) | ||
}) | ||
.toMap | ||
} | ||
} |
121 changes: 121 additions & 0 deletions
121
...-loader-iceberg/src/main/scala/com/adform/streamloader/iceberg/IcebergRecordBatcher.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,121 @@ | ||
/* | ||
* 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.{StreamRange, StreamRecord} | ||
import com.adform.streamloader.sink.batch.{RecordBatch, RecordBatchBuilder, RecordBatcher, RecordFormatter} | ||
import com.adform.streamloader.sink.file.{FileStats, MultiFileCommitStrategy} | ||
import com.adform.streamloader.util.TimeProvider | ||
import org.apache.iceberg.data.{GenericAppenderFactory, Record => IcebergRecord} | ||
import org.apache.iceberg.io.DataWriteResult | ||
import org.apache.iceberg.{FileFormat, PartitionKey, Table} | ||
|
||
import java.time.Duration | ||
import java.util.UUID | ||
import scala.collection.mutable | ||
import scala.jdk.CollectionConverters._ | ||
|
||
case class IcebergRecordBatch(dataWriteResult: DataWriteResult, recordRanges: Seq[StreamRange]) extends RecordBatch { | ||
override def discard(): Boolean = true | ||
} | ||
|
||
/** | ||
* Record batch builder that collects records to files per partition. | ||
*/ | ||
class IcebergRecordBatchBuilder( | ||
table: Table, | ||
recordFormatter: RecordFormatter[IcebergRecord], | ||
fileFormat: FileFormat, | ||
fileCommitStrategy: MultiFileCommitStrategy, | ||
writeProperties: Map[String, String] | ||
)(implicit timeProvider: TimeProvider = TimeProvider.system) | ||
extends RecordBatchBuilder[IcebergRecordBatch] { | ||
|
||
private class PartitionDataWriter(pk: PartitionKey) { | ||
private val startTimeMillis = timeProvider.currentMillis | ||
private var recordCount = 0L | ||
private val dataWriter = { | ||
val filename = fileFormat.addExtension(UUID.randomUUID().toString) | ||
val path = table.locationProvider().newDataLocation(table.spec(), pk, filename) | ||
val output = table.io().newOutputFile(path) | ||
|
||
val factory = new GenericAppenderFactory(table.schema(), table.spec()) | ||
factory.setAll(writeProperties.asJava) | ||
|
||
val encrypted = table.encryption().encrypt(output) | ||
factory.newDataWriter(encrypted, fileFormat, pk) | ||
} | ||
|
||
def write(record: IcebergRecord): Unit = { | ||
dataWriter.write(record) | ||
recordCount += 1 | ||
} | ||
|
||
def build(): DataWriteResult = { | ||
dataWriter.close() | ||
dataWriter.result() | ||
} | ||
|
||
def fileStats: FileStats = FileStats( | ||
Duration.ofMillis(timeProvider.currentMillis - startTimeMillis), | ||
dataWriter.length(), | ||
recordCount | ||
) | ||
} | ||
|
||
private val partitionWriters: mutable.HashMap[PartitionKey, PartitionDataWriter] = mutable.HashMap.empty | ||
|
||
override protected def addToBatch(record: StreamRecord): Int = { | ||
var cnt = 0 | ||
recordFormatter | ||
.format(record) | ||
.foreach(record => { | ||
val pk = new PartitionKey(table.spec(), table.schema()) | ||
pk.partition(record) | ||
|
||
val writer = partitionWriters.getOrElseUpdate(pk, new PartitionDataWriter(pk)) | ||
writer.write(record) | ||
|
||
cnt += 1 | ||
}) | ||
cnt | ||
} | ||
|
||
override def isBatchReady: Boolean = { | ||
fileCommitStrategy.shouldCommit(partitionWriters.map(kv => kv._2.fileStats).toSeq) | ||
} | ||
|
||
override def build(): Option[IcebergRecordBatch] = { | ||
val files = partitionWriters.map(kv => kv._2.build()).flatMap(_.dataFiles().asScala) | ||
if (files.nonEmpty) { | ||
Some(IcebergRecordBatch(new DataWriteResult(files.toList.asJava), currentRecordRanges)) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
override def discard(): Unit = partitionWriters.foreach(_._2.build()) | ||
} | ||
|
||
class IcebergRecordBatcher( | ||
table: Table, | ||
recordFormatter: RecordFormatter[IcebergRecord], | ||
fileFormat: FileFormat, | ||
fileCommitStrategy: MultiFileCommitStrategy, | ||
writeProperties: Map[String, String] | ||
) extends RecordBatcher[IcebergRecordBatch] { | ||
|
||
override def newBatchBuilder(): RecordBatchBuilder[IcebergRecordBatch] = new IcebergRecordBatchBuilder( | ||
table, | ||
recordFormatter, | ||
fileFormat, | ||
fileCommitStrategy, | ||
writeProperties | ||
) | ||
} |
Oops, something went wrong.