-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bulk Load CDK: GZip Compression and S3V2 Usage (#46980)
- Loading branch information
1 parent
738655d
commit 8f2c6f9
Showing
22 changed files
with
646 additions
and
222 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
26 changes: 26 additions & 0 deletions
26
airbyte-cdk/bulk/core/load/src/main/kotlin/io/airbyte/cdk/load/file/StreamProcessor.kt
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,26 @@ | ||
/* | ||
* Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
*/ | ||
|
||
package io.airbyte.cdk.load.file | ||
|
||
import java.io.ByteArrayOutputStream | ||
import java.util.zip.GZIPOutputStream | ||
|
||
interface StreamProcessor<T> { | ||
val wrapper: (ByteArrayOutputStream) -> T | ||
val partFinisher: T.() -> Unit | ||
val extension: String? | ||
} | ||
|
||
data object NoopProcessor : StreamProcessor<ByteArrayOutputStream> { | ||
override val wrapper: (ByteArrayOutputStream) -> ByteArrayOutputStream = { it } | ||
override val partFinisher: ByteArrayOutputStream.() -> Unit = {} | ||
override val extension: String? = null | ||
} | ||
|
||
data object GZIPProcessor : StreamProcessor<GZIPOutputStream> { | ||
override val wrapper: (ByteArrayOutputStream) -> GZIPOutputStream = { GZIPOutputStream(it) } | ||
override val partFinisher: GZIPOutputStream.() -> Unit = { finish() } | ||
override val extension: String = "gz" | ||
} |
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
dependencies { | ||
implementation project(':airbyte-cdk:bulk:core:bulk-cdk-core-base') | ||
implementation project(':airbyte-cdk:bulk:core:bulk-cdk-core-load') | ||
|
||
testFixturesImplementation testFixtures(project(":airbyte-cdk:bulk:core:bulk-cdk-core-load")) | ||
} |
86 changes: 86 additions & 0 deletions
86
...otlin/io/airbyte/cdk/load/command/object_storage/ObjectStorageCompressionSpecification.kt
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,86 @@ | ||
/* | ||
* Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
*/ | ||
|
||
package io.airbyte.cdk.load.command.object_storage | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty | ||
import com.fasterxml.jackson.annotation.JsonPropertyDescription | ||
import com.fasterxml.jackson.annotation.JsonSubTypes | ||
import com.fasterxml.jackson.annotation.JsonTypeInfo | ||
import com.fasterxml.jackson.annotation.JsonValue | ||
import com.kjetland.jackson.jsonSchema.annotations.JsonSchemaTitle | ||
import io.airbyte.cdk.load.file.GZIPProcessor | ||
import io.airbyte.cdk.load.file.NoopProcessor | ||
import io.airbyte.cdk.load.file.StreamProcessor | ||
import java.io.ByteArrayOutputStream | ||
import java.io.OutputStream | ||
|
||
/** | ||
* Mix-in to provide file format configuration. | ||
* | ||
* The specification is intended to be applied to file formats that are compatible with file-level | ||
* compression (csv, jsonl) and does not need to be added to the destination spec directly. The | ||
* [ObjectStorageCompressionConfigurationProvider] can be added to the top-level | ||
* [io.airbyte.cdk.load.command.DestinationConfiguration] and initialized directly with | ||
* [ObjectStorageFormatSpecificationProvider.toObjectStorageFormatConfiguration]. (See the comments | ||
* on [io.airbyte.cdk.load.command.DestinationConfiguration] for more details.) | ||
*/ | ||
interface ObjectStorageCompressionSpecificationProvider { | ||
@get:JsonSchemaTitle("Compression") | ||
@get:JsonPropertyDescription( | ||
"Whether the output files should be compressed. If compression is selected, the output filename will have an extra extension (GZIP: \".jsonl.gz\").", | ||
) | ||
@get:JsonProperty("compression") | ||
val compression: ObjectStorageCompressionSpecification | ||
|
||
fun toCompressionConfiguration(): ObjectStorageCompressionConfiguration<*> { | ||
return when (compression) { | ||
is NoCompressionSpecification -> ObjectStorageCompressionConfiguration(NoopProcessor) | ||
is GZIPCompressionSpecification -> ObjectStorageCompressionConfiguration(GZIPProcessor) | ||
} | ||
} | ||
|
||
companion object { | ||
fun getNoCompressionConfiguration(): | ||
ObjectStorageCompressionConfiguration<ByteArrayOutputStream> { | ||
return ObjectStorageCompressionConfiguration(NoopProcessor) | ||
} | ||
} | ||
} | ||
|
||
@JsonTypeInfo( | ||
use = JsonTypeInfo.Id.NAME, | ||
include = JsonTypeInfo.As.EXISTING_PROPERTY, | ||
property = "compression_type", | ||
) | ||
@JsonSubTypes( | ||
JsonSubTypes.Type(value = NoCompressionSpecification::class, name = "No Compression"), | ||
JsonSubTypes.Type(value = GZIPCompressionSpecification::class, name = "GZIP"), | ||
) | ||
sealed class ObjectStorageCompressionSpecification( | ||
@JsonProperty("compression_type") open val compressionType: Type | ||
) { | ||
enum class Type(@get:JsonValue val typeName: String) { | ||
NoCompression("No Compression"), | ||
GZIP("GZIP"), | ||
} | ||
} | ||
|
||
@JsonSchemaTitle("No Compression") | ||
class NoCompressionSpecification( | ||
@JsonProperty("compression_type") override val compressionType: Type = Type.NoCompression | ||
) : ObjectStorageCompressionSpecification(compressionType) | ||
|
||
@JsonSchemaTitle("GZIP") | ||
class GZIPCompressionSpecification( | ||
@JsonProperty("compression_type") override val compressionType: Type = Type.GZIP | ||
) : ObjectStorageCompressionSpecification(compressionType) | ||
|
||
data class ObjectStorageCompressionConfiguration<T : OutputStream>( | ||
val compressor: StreamProcessor<T> | ||
) | ||
|
||
interface ObjectStorageCompressionConfigurationProvider<T : OutputStream> { | ||
val objectStorageCompressionConfiguration: ObjectStorageCompressionConfiguration<T> | ||
} |
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
62 changes: 62 additions & 0 deletions
62
...oad-object-storage/src/testFixtures/kotlin/io/airbyte/cdk/load/ObjectStorageDataDumper.kt
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,62 @@ | ||
/* | ||
* Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
*/ | ||
|
||
package io.airbyte.cdk.load | ||
|
||
import io.airbyte.cdk.load.command.DestinationStream | ||
import io.airbyte.cdk.load.command.object_storage.ObjectStorageCompressionConfiguration | ||
import io.airbyte.cdk.load.data.toAirbyteValue | ||
import io.airbyte.cdk.load.file.GZIPProcessor | ||
import io.airbyte.cdk.load.file.NoopProcessor | ||
import io.airbyte.cdk.load.file.object_storage.ObjectStorageClient | ||
import io.airbyte.cdk.load.file.object_storage.ObjectStoragePathFactory | ||
import io.airbyte.cdk.load.file.object_storage.RemoteObject | ||
import io.airbyte.cdk.load.test.util.OutputRecord | ||
import io.airbyte.cdk.load.test.util.toOutputRecord | ||
import io.airbyte.cdk.load.util.deserializeToNode | ||
import java.io.InputStream | ||
import java.util.zip.GZIPInputStream | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.flow.toList | ||
import kotlinx.coroutines.runBlocking | ||
import kotlinx.coroutines.withContext | ||
|
||
class ObjectStorageDataDumper( | ||
private val stream: DestinationStream, | ||
private val client: ObjectStorageClient<*, *>, | ||
private val pathFactory: ObjectStoragePathFactory, | ||
private val compressionConfig: ObjectStorageCompressionConfiguration<*>? = null | ||
) { | ||
fun dump(): List<OutputRecord> { | ||
val prefix = pathFactory.getFinalDirectory(stream).toString() | ||
return runBlocking { | ||
withContext(Dispatchers.IO) { | ||
client | ||
.list(prefix) | ||
.map { listedObject: RemoteObject<*> -> | ||
client.get(listedObject.key) { objectData: InputStream -> | ||
when (compressionConfig?.compressor) { | ||
is GZIPProcessor -> GZIPInputStream(objectData) | ||
is NoopProcessor, | ||
null -> objectData | ||
else -> error("Unsupported compressor") | ||
} | ||
.bufferedReader() | ||
.lineSequence() | ||
.map { line -> | ||
line | ||
.deserializeToNode() | ||
.toAirbyteValue(stream.schemaWithMeta) | ||
.toOutputRecord() | ||
} | ||
.toList() | ||
} | ||
} | ||
.toList() | ||
.flatten() | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.