|
| 1 | +/* |
| 2 | + * Copyright 2026 Spotify AB. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, |
| 11 | + * software distributed under the License is distributed on an |
| 12 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 13 | + * KIND, either express or implied. See the License for the |
| 14 | + * specific language governing permissions and limitations |
| 15 | + * under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.spotify.scio.parquet |
| 19 | + |
| 20 | +import com.spotify.scio.ContextAndArgs |
| 21 | +import com.spotify.scio.parquet.ParquetConfiguration |
| 22 | +import com.spotify.scio.parquet.read.ParquetReadConfiguration |
| 23 | +import com.spotify.scio.parquet.types._ |
| 24 | +import com.spotify.scio.testing.PipelineSpec |
| 25 | +import com.spotify.scio.testing.util.ItUtils |
| 26 | +import org.apache.beam.sdk.extensions.gcp.options.GcpOptions |
| 27 | +import org.apache.beam.sdk.io.FileSystems |
| 28 | +import org.apache.beam.sdk.options.PipelineOptionsFactory |
| 29 | + |
| 30 | +import scala.jdk.CollectionConverters._ |
| 31 | + |
| 32 | +case class ParquetITRecord(id: Int, value: String) |
| 33 | + |
| 34 | +class ParquetReadIT extends PipelineSpec { |
| 35 | + |
| 36 | + private def gcsDir: String = ItUtils.gcpTempLocation("parquet-read-it") |
| 37 | + |
| 38 | + private def gcpOptions: Array[String] = { |
| 39 | + val tempLocation = ItUtils.gcpTempLocation("parquet-read-it-tmp") |
| 40 | + Array(s"--project=${ItUtils.project}", s"--tempLocation=$tempLocation") |
| 41 | + } |
| 42 | + |
| 43 | + private def deleteDir(dir: String): Unit = { |
| 44 | + val opts = PipelineOptionsFactory.create() |
| 45 | + opts.as(classOf[GcpOptions]).setProject(ItUtils.project) |
| 46 | + FileSystems.setDefaultPipelineOptions(opts) |
| 47 | + val files = FileSystems.`match`(s"$dir/**").metadata().asScala.map(_.resourceId()) |
| 48 | + if (files.nonEmpty) { |
| 49 | + FileSystems.delete(files.asJava) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + "typedParquetFile" should "round-trip through GCS" in { |
| 54 | + val dir = gcsDir |
| 55 | + val records = (1 to 10).map(i => ParquetITRecord(i, s"value-$i")).toList |
| 56 | + |
| 57 | + try { |
| 58 | + val (sc1, _) = ContextAndArgs(gcpOptions) |
| 59 | + sc1.parallelize(records).saveAsTypedParquetFile(dir) |
| 60 | + sc1.run().waitUntilDone() |
| 61 | + |
| 62 | + val legacyReadConf = ParquetConfiguration.of( |
| 63 | + ParquetReadConfiguration.UseSplittableDoFn -> false |
| 64 | + ) |
| 65 | + |
| 66 | + val (sc2, _) = ContextAndArgs(gcpOptions) |
| 67 | + sc2.typedParquetFile[ParquetITRecord]( |
| 68 | + path = s"$dir/*.parquet", |
| 69 | + conf = legacyReadConf |
| 70 | + ) should containInAnyOrder(records) |
| 71 | + sc2.run() |
| 72 | + } finally { |
| 73 | + deleteDir(dir) |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments