-
Notifications
You must be signed in to change notification settings - Fork 513
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Managed + Iceberg IO #5494
Draft
kellen
wants to merge
14
commits into
main
Choose a base branch
from
kellen/iceberg
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Managed + Iceberg IO #5494
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
2207f5d
wip
kellen 5ed2f04
wip
kellen d0a01e8
wip
kellen 05d1311
wip
kellen 20e758c
Merge branch 'main' into kellen/iceberg
kellen 85a8708
wip
kellen aeed697
wip
kellen eb933e3
wip
kellen 34fa5a9
wip
kellen aa37670
wip
kellen 55ed828
wip
kellen eb57321
wip
kellen cd76b91
wip
kellen d3b67dc
Update scio-core/src/main/scala/com/spotify/scio/values/SCollection.s…
kellen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
96 changes: 96 additions & 0 deletions
96
integration/src/test/scala/com/spotify/scio/iceberg/IcebergIOIT.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,96 @@ | ||
/* | ||
* Copyright 2024 Spotify AB | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.spotify.scio.iceberg | ||
|
||
import com.dimafeng.testcontainers.{ForAllTestContainer, GenericContainer} | ||
import com.spotify.scio.testing.PipelineSpec | ||
import magnolify.beam._ | ||
import org.apache.iceberg.catalog.{Namespace, TableIdentifier} | ||
import org.apache.iceberg.rest.RESTCatalog | ||
import org.apache.iceberg.types.Types.{IntegerType, NestedField, StringType} | ||
import org.apache.iceberg.{CatalogProperties, CatalogUtil, PartitionSpec} | ||
import org.testcontainers.containers.wait.strategy.HostPortWaitStrategy | ||
|
||
import java.time.Duration | ||
import java.io.File | ||
import java.nio.file.Files | ||
import scala.jdk.CollectionConverters._ | ||
|
||
case class IcebergIOITRecord(a: Int, b: String) | ||
object IcebergIOITRecord { | ||
implicit val icebergIOITRecordRowType: RowType[IcebergIOITRecord] = RowType[IcebergIOITRecord] | ||
} | ||
|
||
class IcebergIOIT extends PipelineSpec with ForAllTestContainer { | ||
val ContainerPort = 8181 | ||
val CatalogName = "iceberg_it" | ||
val NamespaceName = "iceberg_it_ns" | ||
val TableName = s"${NamespaceName}.iceberg_records" | ||
|
||
lazy val tempDir: File = { | ||
val t = Files.createTempDirectory("iceberg-it").toFile | ||
t.deleteOnExit() | ||
t | ||
} | ||
|
||
override val container: GenericContainer = | ||
GenericContainer( | ||
GenericContainer.stringToDockerImage("tabulario/iceberg-rest:1.6.0"), | ||
exposedPorts = Seq(ContainerPort), | ||
waitStrategy = new HostPortWaitStrategy() | ||
.forPorts(ContainerPort) | ||
.withStartupTimeout(Duration.ofSeconds(180)) | ||
) | ||
|
||
lazy val uri = s"http://${container.containerIpAddress}:${container.mappedPort(ContainerPort)}" | ||
|
||
override def afterStart(): Unit = { | ||
val cat = new RESTCatalog() | ||
cat.initialize(CatalogName, Map("uri" -> uri).asJava) | ||
|
||
import org.apache.iceberg.Schema | ||
cat.createNamespace(Namespace.of(NamespaceName)) | ||
cat.createTable( | ||
TableIdentifier.parse(TableName), | ||
new Schema( | ||
NestedField.required(0, "a", IntegerType.get()), | ||
NestedField.required(1, "b", StringType.get()) | ||
), | ||
PartitionSpec.unpartitioned() | ||
) | ||
} | ||
|
||
"IcebergIO" should "work" in { | ||
val catalogProperties = Map( | ||
CatalogUtil.ICEBERG_CATALOG_TYPE -> CatalogUtil.ICEBERG_CATALOG_TYPE_REST, | ||
CatalogProperties.URI -> uri | ||
) | ||
val elements = 1.to(10).map(i => IcebergIOITRecord(i, s"$i")) | ||
|
||
runWithRealContext() { sc => | ||
sc.parallelize(elements) | ||
.saveAsIceberg(TableName, catalogProperties = catalogProperties) | ||
} | ||
|
||
runWithRealContext() { sc => | ||
sc.iceberg[IcebergIOITRecord]( | ||
TableName, | ||
catalogProperties = catalogProperties | ||
) should containInAnyOrder(elements) | ||
} | ||
} | ||
} |
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
66 changes: 66 additions & 0 deletions
66
scio-examples/src/main/scala/com/spotify/scio/examples/extra/IcebergExample.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,66 @@ | ||
/* | ||
* Copyright 2024 Spotify AB | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.spotify.scio.examples.extra | ||
|
||
import com.spotify.scio.ContextAndArgs | ||
import com.spotify.scio.iceberg._ | ||
import magnolify.beam._ | ||
|
||
// Example: Apache Iceberg read/write Example | ||
|
||
// Usage: | ||
|
||
// `sbt "runMain com.spotify.scio.examples.extra.IcebergExample | ||
// --project=[PROJECT] --runner=DataflowRunner --region=[REGION NAME] | ||
// --inputTable=[INPUT TABLE] --catalogName=[CATALOG NAME] | ||
// --catalogType=[CATALOG TYPE] --catalogUri=[CATALOG URI] | ||
// --catalogWarehouse=[CATALOG WAREHOUSE] --outputTable=[OUTPUT TABLE]"` | ||
object IcebergExample { | ||
|
||
case class Record(a: Int, b: String) | ||
|
||
def main(cmdlineArgs: Array[String]): Unit = { | ||
val (sc, args) = ContextAndArgs(cmdlineArgs) | ||
|
||
// Catalog configuration | ||
val catalogConfig = Map( | ||
"type" -> args("catalogType"), | ||
"uri" -> args("catalogUri"), | ||
"warehouse" -> args("catalogWarehouse") | ||
) | ||
|
||
// Derive a conversion between Record and Beam Row | ||
implicit val rt: RowType[Record] = RowType[Record] | ||
|
||
sc | ||
// Read Records from Iceberg | ||
.iceberg[Record]( | ||
args("inputTable"), | ||
args.optional("catalogName").orNull, | ||
catalogConfig | ||
) | ||
.map(r => r.copy(a = r.a + 1)) | ||
// Write Records to Iceberg | ||
.saveAsIceberg( | ||
args("outputTable"), | ||
args.optional("catalogName").orNull, | ||
catalogConfig | ||
) | ||
|
||
sc.run() | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
scio-examples/src/main/scala/com/spotify/scio/examples/extra/ManagedExample.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,78 @@ | ||
/* | ||
* Copyright 2024 Spotify AB | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.spotify.scio.examples.extra | ||
|
||
import com.spotify.scio.ContextAndArgs | ||
import com.spotify.scio.coders.Coder | ||
import com.spotify.scio.managed._ | ||
import com.spotify.scio.values.SCollection | ||
import magnolify.beam._ | ||
import org.apache.beam.sdk.managed.Managed | ||
import org.apache.beam.sdk.values.Row | ||
|
||
// Example: Beam's Managed IO | ||
|
||
// Usage: | ||
|
||
// `sbt "runMain com.spotify.scio.examples.extra.ManagedExample | ||
// --project=[PROJECT] --runner=DataflowRunner --region=[REGION NAME] | ||
// --table=[TABLE] --catalogName=[CATALOG] --catalogType=[CATALOG TYPE] | ||
// --catalogUri=[CATALOG URI] --catalogWarehouse=[CATALOG WAREHOUSE] | ||
// --output=[OUTPUT PATH]"` | ||
object ManagedExample { | ||
|
||
case class Record(a: Int, b: String) | ||
|
||
def main(cmdlineArgs: Array[String]): Unit = { | ||
val (sc, args) = ContextAndArgs(cmdlineArgs) | ||
|
||
val config: Map[String, Object] = Map( | ||
"table" -> args("table"), | ||
"catalog_name" -> args("catalogName"), | ||
"catalog_properties" -> | ||
Map( | ||
"type" -> args("catalogType"), | ||
"uri" -> args("catalogUri"), | ||
"warehouse" -> args("catalogWarehouse") | ||
) | ||
) | ||
|
||
val rt = RowType[Record] | ||
// Provide an implicit coder for Row with the schema derived from Record case class | ||
implicit val recordRowCoder: Coder[Row] = Coder.row(rt.schema) | ||
|
||
// Read beam Row instances from iceberg | ||
val records: SCollection[Record] = sc | ||
.managed( | ||
Managed.ICEBERG, | ||
// Schema derived from the Record case class | ||
rt.schema, | ||
config | ||
) | ||
// Convert the Row instance to a Record | ||
.map(rt.apply) | ||
|
||
records | ||
.map(r => r.copy(a = r.a + 1)) | ||
// Convert the Record to a Row | ||
.map(rt.apply) | ||
// Save Row instances to Iceberg | ||
.saveAsManaged(Managed.ICEBERG, config) | ||
|
||
sc.run() | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't this be given by the
RowType
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, this is an Iceberg schema rather than the Beam schema. The lack of create-on-write does raise the question of whether we also need to derive the iceberg schemas
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe
RowType
could also offer adef icebergSchema
? (Similar to how magnolify-parquet has bothdef schema
anddef avroSchema
...)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would mean pulling in iceberg deps into the beam module fyi
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could make it provided, I guess, but point taken
Seems like Beam should have a utility function for converting between Beam/Icerberg Schemas. They have similar stuff for BQ/Avro/BeamSchema interop. Maybe we could contribute there
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Beam does have this but it introduces a dep on the iceberg part of the sdk that in theory should be managed.
I could use it in the integration test directly but that wouldn't help users at all.
OTOH ... the class has this comment so 🤷