|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.beam.sdk.io.delta; |
| 19 | + |
| 20 | +import static org.apache.beam.sdk.io.delta.DeltaReadSchemaTransformProvider.Configuration; |
| 21 | +import static org.apache.beam.sdk.util.construction.BeamUrns.getUrn; |
| 22 | + |
| 23 | +import com.google.auto.service.AutoService; |
| 24 | +import com.google.auto.value.AutoValue; |
| 25 | +import java.util.Collections; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | +import org.apache.beam.model.pipeline.v1.ExternalTransforms; |
| 29 | +import org.apache.beam.sdk.io.delta.DeltaReadSchemaTransformProvider.Configuration; |
| 30 | +import org.apache.beam.sdk.schemas.AutoValueSchema; |
| 31 | +import org.apache.beam.sdk.schemas.NoSuchSchemaException; |
| 32 | +import org.apache.beam.sdk.schemas.SchemaRegistry; |
| 33 | +import org.apache.beam.sdk.schemas.annotations.DefaultSchema; |
| 34 | +import org.apache.beam.sdk.schemas.annotations.SchemaFieldDescription; |
| 35 | +import org.apache.beam.sdk.schemas.transforms.SchemaTransform; |
| 36 | +import org.apache.beam.sdk.schemas.transforms.SchemaTransformProvider; |
| 37 | +import org.apache.beam.sdk.schemas.transforms.TypedSchemaTransformProvider; |
| 38 | +import org.apache.beam.sdk.values.PCollection; |
| 39 | +import org.apache.beam.sdk.values.PCollectionRowTuple; |
| 40 | +import org.apache.beam.sdk.values.Row; |
| 41 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 42 | + |
| 43 | +/** |
| 44 | + * SchemaTransform implementation for {@link DeltaIO#readRows}. Reads records from Delta Lake and |
| 45 | + * outputs a {@link org.apache.beam.sdk.values.PCollection} of Beam {@link |
| 46 | + * org.apache.beam.sdk.values.Row}s. |
| 47 | + */ |
| 48 | +@AutoService(SchemaTransformProvider.class) |
| 49 | +public class DeltaReadSchemaTransformProvider extends TypedSchemaTransformProvider<Configuration> { |
| 50 | + static final String OUTPUT_TAG = "output"; |
| 51 | + |
| 52 | + @Override |
| 53 | + protected SchemaTransform from(Configuration configuration) { |
| 54 | + return new DeltaReadSchemaTransform(configuration); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public List<String> outputCollectionNames() { |
| 59 | + return Collections.singletonList(OUTPUT_TAG); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public String identifier() { |
| 64 | + return getUrn(ExternalTransforms.ManagedTransforms.Urns.DELTA_LAKE_READ); |
| 65 | + } |
| 66 | + |
| 67 | + static class DeltaReadSchemaTransform extends SchemaTransform { |
| 68 | + private final Configuration configuration; |
| 69 | + |
| 70 | + DeltaReadSchemaTransform(Configuration configuration) { |
| 71 | + this.configuration = |
| 72 | + java.util.Objects.requireNonNull(configuration, "configuration cannot be null"); |
| 73 | + } |
| 74 | + |
| 75 | + Row getConfigurationRow() { |
| 76 | + try { |
| 77 | + return SchemaRegistry.createDefault() |
| 78 | + .getToRowFunction(Configuration.class) |
| 79 | + .apply(configuration) |
| 80 | + .sorted() |
| 81 | + .toSnakeCase(); |
| 82 | + } catch (NoSuchSchemaException e) { |
| 83 | + throw new RuntimeException(e); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public PCollectionRowTuple expand(PCollectionRowTuple input) { |
| 89 | + DeltaIO.ReadRows read = DeltaIO.readRows().from(configuration.getTable()); |
| 90 | + if (configuration.getVersion() != null) { |
| 91 | + read = read.withVersion(configuration.getVersion()); |
| 92 | + } |
| 93 | + if (configuration.getTimestamp() != null) { |
| 94 | + read = read.withTimestamp(configuration.getTimestamp()); |
| 95 | + } |
| 96 | + Map<String, String> hadoopConfig = configuration.getHadoopConfig(); |
| 97 | + if (hadoopConfig != null) { |
| 98 | + read = read.withConfig(hadoopConfig); |
| 99 | + } |
| 100 | + |
| 101 | + PCollection<Row> output = input.getPipeline().apply(read); |
| 102 | + |
| 103 | + return PCollectionRowTuple.of(OUTPUT_TAG, output); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + @DefaultSchema(AutoValueSchema.class) |
| 108 | + @AutoValue |
| 109 | + public abstract static class Configuration { |
| 110 | + static Builder builder() { |
| 111 | + return new AutoValue_DeltaReadSchemaTransformProvider_Configuration.Builder(); |
| 112 | + } |
| 113 | + |
| 114 | + @SchemaFieldDescription("Identifier of the Delta Lake table.") |
| 115 | + abstract String getTable(); |
| 116 | + |
| 117 | + @SchemaFieldDescription("Version of the Delta Lake table to read.") |
| 118 | + @Nullable |
| 119 | + abstract Long getVersion(); |
| 120 | + |
| 121 | + @SchemaFieldDescription("Timestamp of the Delta Lake table to read.") |
| 122 | + @Nullable |
| 123 | + abstract String getTimestamp(); |
| 124 | + |
| 125 | + @SchemaFieldDescription("Properties passed to the Hadoop Configuration.") |
| 126 | + @Nullable |
| 127 | + abstract Map<String, String> getHadoopConfig(); |
| 128 | + |
| 129 | + @AutoValue.Builder |
| 130 | + abstract static class Builder { |
| 131 | + abstract Builder setTable(String table); |
| 132 | + |
| 133 | + abstract Builder setVersion(@Nullable Long version); |
| 134 | + |
| 135 | + abstract Builder setTimestamp(@Nullable String timestamp); |
| 136 | + |
| 137 | + abstract Builder setHadoopConfig(@Nullable Map<String, String> hadoopConfig); |
| 138 | + |
| 139 | + abstract Configuration build(); |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments