-
Notifications
You must be signed in to change notification settings - Fork 514
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
7 changed files
with
347 additions
and
32 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
28 changes: 28 additions & 0 deletions
28
scio-extra/src/main/scala/com/spotify/scio/extra/csv/CsvSink.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 @@ | ||
package com.spotify.scio.extra.csv | ||
|
||
import kantan.csv.ops.toCsvOutputOps | ||
import kantan.csv.{CsvConfiguration, CsvWriter, HeaderEncoder} | ||
import org.apache.beam.sdk.io.FileIO | ||
|
||
import java.io.Writer | ||
import java.nio.channels.{Channels, WritableByteChannel} | ||
import java.nio.charset.StandardCharsets | ||
|
||
final private[scio] class CsvSink[T: HeaderEncoder](csvConfig: CsvConfiguration) | ||
extends FileIO.Sink[T] { | ||
@transient private var csvWriter: CsvWriter[T] = _ | ||
@transient private var byteChannelWriter: Writer = _ | ||
|
||
override def open(channel: WritableByteChannel): Unit = { | ||
byteChannelWriter = Channels.newWriter(channel, StandardCharsets.UTF_8.name()) | ||
csvWriter = byteChannelWriter.asCsvWriter[T](csvConfig) | ||
} | ||
|
||
override def write(element: T): Unit = { | ||
csvWriter.write(element) | ||
() | ||
} | ||
|
||
override def flush(): Unit = | ||
byteChannelWriter.flush() | ||
} |
28 changes: 28 additions & 0 deletions
28
scio-extra/src/main/scala/com/spotify/scio/extra/csv/dynamic/dynamic.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.extra.csv | ||
|
||
import com.spotify.scio.extra.csv.dynamic.syntax.AllSyntax | ||
|
||
/** | ||
* CSV package for dynamic destinations. Import All. | ||
* | ||
* {{{ | ||
* import com.spotify.scio.extra.csv.dynamic._ | ||
* }}} | ||
*/ | ||
package object dynamic extends AllSyntax |
19 changes: 19 additions & 0 deletions
19
scio-extra/src/main/scala/com/spotify/scio/extra/csv/dynamic/syntax/AllSyntax.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,19 @@ | ||
/* | ||
* 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.extra.csv.dynamic.syntax | ||
|
||
trait AllSyntax extends SCollectionSyntax |
70 changes: 70 additions & 0 deletions
70
scio-extra/src/main/scala/com/spotify/scio/extra/csv/dynamic/syntax/SCollectionSyntax.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,70 @@ | ||
/* | ||
* 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.extra.csv.dynamic.syntax | ||
|
||
import com.spotify.scio.annotations.experimental | ||
import com.spotify.scio.coders.Coder | ||
import com.spotify.scio.extra.csv.{CsvIO, CsvSink} | ||
import com.spotify.scio.io.dynamic.syntax.DynamicSCollectionOps.writeDynamic | ||
import com.spotify.scio.io.{ClosedTap, EmptyTap} | ||
import com.spotify.scio.values.SCollection | ||
import kantan.csv.{CsvConfiguration, HeaderEncoder} | ||
import org.apache.beam.sdk.io.Compression | ||
|
||
final class DynamicCsvSCollectionOps[T]( | ||
private val self: SCollection[T] | ||
) extends AnyVal { | ||
|
||
/** Save this SCollection of records as CSV files written to dynamic destinations. */ | ||
@experimental | ||
def saveAsDynamicCsvFile( | ||
path: String, | ||
suffix: String = CsvIO.WriteParam.DefaultSuffix, | ||
prefix: String = CsvIO.WriteParam.DefaultPrefix, | ||
numShards: Int = CsvIO.WriteParam.DefaultNumShards, | ||
compression: Compression = CsvIO.WriteParam.DefaultCompression, | ||
tempDirectory: String = CsvIO.WriteParam.DefaultTempDirectory, | ||
csvConfig: CsvConfiguration = CsvIO.WriteParam.DefaultCsvConfig | ||
)( | ||
destinationFn: T => String | ||
)(implicit coder: Coder[T], enc: HeaderEncoder[T]): ClosedTap[Nothing] = { | ||
if (self.context.isTest) { | ||
throw new NotImplementedError( | ||
"CSV file with dynamic destinations cannot be used in a test context" | ||
) | ||
} else { | ||
val sink = new CsvSink(csvConfig) | ||
val write = writeDynamic( | ||
path = path, | ||
destinationFn = destinationFn, | ||
numShards = numShards, | ||
prefix = prefix, | ||
suffix = suffix, | ||
tempDirectory = tempDirectory | ||
).withCompression(compression).via(sink) | ||
self.applyInternal(write) | ||
} | ||
ClosedTap[Nothing](EmptyTap) | ||
} | ||
} | ||
|
||
trait SCollectionSyntax { | ||
implicit def dynamicCsvSCollectionOps[T]( | ||
sc: SCollection[T] | ||
): DynamicCsvSCollectionOps[T] = | ||
new DynamicCsvSCollectionOps(sc) | ||
} |
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.