-
Notifications
You must be signed in to change notification settings - Fork 513
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
16 changed files
with
564 additions
and
9 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
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._ | ||
import magnolify.beam.logical.nanos._ | ||
import org.joda.time.Instant | ||
|
||
// ## Iceberg IO example | ||
|
||
// Usage: | ||
|
||
// `sbt "runMain com.spotify.scio.examples.extra.IcebergExample | ||
// --project=[PROJECT] --runner=DataflowRunner --region=[REGION NAME] | ||
// FIXME | ||
object IcebergExample { | ||
/* | ||
---------------------------------------------------------------------------------------------> | ||
partition | row(__PARTITIONTIME timestamp(6)) > | ||
record_count | BigDecimal > | ||
file_count | BigDecimal > | ||
total_size | BigDecimal > | ||
data | row(timestamp row(min timestamp(6), max timestamp(6), null_count BigDecimal, nan_count BigDecimal), country_code row(min varchar, max varchar, null_count BigDecimal, nan_count BigDecimal), url row(min varchar, max varchar, null_count BigDecimal, nan_count BigDecimal), project row(min varchar, max varchar, null_count BigDecimal, nan_count BigDecimal), tls_protocol | ||
*/ | ||
case class FileDownloads( | ||
record_count: BigDecimal, | ||
file_count: BigDecimal, | ||
total_size: BigDecimal, | ||
data: Data | ||
) | ||
case class Data(timestamp: Timestamp) | ||
case class Timestamp(min: Instant, max: Instant, null_count: BigDecimal, nan_count: BigDecimal) | ||
|
||
def main(cmdlineArgs: Array[String]): Unit = { | ||
val (sc, args) = ContextAndArgs(cmdlineArgs) | ||
|
||
sc.iceberg[FileDownloads]( | ||
// TODO quoted things don't work via iceberg/hive | ||
"", | ||
"", | ||
Map( | ||
"type" -> "hive", | ||
"uri" -> "", | ||
"warehouse" -> "" | ||
) | ||
).debug(prefix = "FileDownload: ") | ||
|
||
sc.run() | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
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,62 @@ | ||
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 | ||
|
||
// ## Managed IO example | ||
|
||
// 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 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() | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
scio-iceberg/src/main/scala/com/spotify/scio/iceberg/IcebergIO.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,49 @@ | ||
/* | ||
* 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.spotify.scio.ScioContext | ||
import com.spotify.scio.coders.Coder | ||
import com.spotify.scio.io.{EmptyTapOf, ScioIO, Tap, TapT} | ||
import com.spotify.scio.values.SCollection | ||
import magnolify.beam.RowType | ||
import org.apache.beam.sdk.managed.Managed | ||
import com.spotify.scio.managed.ManagedIO | ||
import org.apache.beam.sdk.values.Row | ||
|
||
final case class IcebergIO[T : RowType : Coder](config: Map[String, AnyRef]) extends ScioIO[T] { | ||
override type ReadP = IcebergIO.ReadParam | ||
override type WriteP = IcebergIO.WriteParam | ||
override val tapT: TapT.Aux[T, Nothing] = EmptyTapOf[T] | ||
|
||
private lazy val rowType: RowType[T] = implicitly | ||
private lazy val underlying: ManagedIO = ManagedIO(Managed.ICEBERG, config) | ||
private lazy implicit val rowCoder: Coder[Row] = Coder.row(rowType.schema) | ||
|
||
override protected def read(sc: ScioContext, params: IcebergIO.ReadParam): SCollection[T] = | ||
underlying.readWithContext(sc, params).map(rowType.from) | ||
|
||
override protected def write(data: SCollection[T], params: IcebergIO.WriteParam): Tap[tapT.T] = | ||
underlying.writeWithContext(data.transform(_.map(rowType.to)), params).underlying | ||
|
||
override def tap(read: IcebergIO.ReadParam): Tap[tapT.T] = underlying.tap(read) | ||
} | ||
|
||
object IcebergIO { | ||
type ReadParam = ManagedIO.ReadParam | ||
type WriteParam = ManagedIO.WriteParam | ||
} |
28 changes: 28 additions & 0 deletions
28
scio-iceberg/src/main/scala/com/spotify/scio/iceberg/package.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,28 @@ | ||
/* | ||
* 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 | ||
|
||
import com.spotify.scio.iceberg.syntax.{ScioContextSyntax, SCollectionSyntax} | ||
|
||
/** | ||
* Iceberg IO APIs. Import all. | ||
* | ||
* {{{ | ||
* import com.spotify.scio.iceberg._ | ||
* }}} | ||
*/ | ||
package object iceberg extends ScioContextSyntax with SCollectionSyntax |
34 changes: 34 additions & 0 deletions
34
scio-iceberg/src/main/scala/com/spotify/scio/iceberg/syntax/IcebergSCollectionSyntax.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,34 @@ | ||
/* | ||
* 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.syntax | ||
|
||
import com.spotify.scio.coders.Coder | ||
import com.spotify.scio.iceberg.IcebergIO | ||
import com.spotify.scio.io.ClosedTap | ||
import com.spotify.scio.managed.ManagedIO | ||
import com.spotify.scio.values.SCollection | ||
import magnolify.beam.RowType | ||
|
||
class IcebergSCollectionSyntax[T : RowType : Coder](self: SCollection[T]) { | ||
def saveAsIceberg(config: Map[String, AnyRef]): ClosedTap[Nothing] = | ||
self.write(IcebergIO(config))(ManagedIO.WriteParam()) | ||
} | ||
|
||
trait SCollectionSyntax { | ||
implicit def icebergSCollectionSyntax[T : RowType : Coder](self: SCollection[T]): IcebergSCollectionSyntax[T] = | ||
new IcebergSCollectionSyntax(self) | ||
} |
Oops, something went wrong.