|
| 1 | +/* |
| 2 | + * Copyright 2017 Google Inc. |
| 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, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.dataflow; |
| 18 | + |
| 19 | +import com.google.cloud.spanner.Dialect; |
| 20 | +import com.google.cloud.spanner.Struct; |
| 21 | +import org.apache.beam.sdk.Pipeline; |
| 22 | +import org.apache.beam.sdk.io.TextIO; |
| 23 | +import org.apache.beam.sdk.io.gcp.spanner.SpannerIO; |
| 24 | +import org.apache.beam.sdk.options.Default; |
| 25 | +import org.apache.beam.sdk.options.Default.Enum; |
| 26 | +import org.apache.beam.sdk.options.Description; |
| 27 | +import org.apache.beam.sdk.options.PipelineOptions; |
| 28 | +import org.apache.beam.sdk.options.PipelineOptionsFactory; |
| 29 | +import org.apache.beam.sdk.options.Validation; |
| 30 | +import org.apache.beam.sdk.transforms.Sum; |
| 31 | +import org.apache.beam.sdk.transforms.ToString; |
| 32 | +import org.apache.beam.sdk.values.PCollection; |
| 33 | + |
| 34 | +/** |
| 35 | + * This sample demonstrates how to read from a Spanner table using the Read API, reading from a |
| 36 | + * secondary index. |
| 37 | + */ |
| 38 | +public class SpannerReadApiWithIndex { |
| 39 | + |
| 40 | + public interface Options extends PipelineOptions { |
| 41 | + |
| 42 | + @Description("Spanner instance ID to query from") |
| 43 | + @Validation.Required |
| 44 | + String getInstanceId(); |
| 45 | + |
| 46 | + void setInstanceId(String value); |
| 47 | + |
| 48 | + @Description("Spanner database name to query from") |
| 49 | + @Validation.Required |
| 50 | + String getDatabaseId(); |
| 51 | + |
| 52 | + void setDatabaseId(String value); |
| 53 | + |
| 54 | + @Description("Dialect of the database that is used") |
| 55 | + @Default |
| 56 | + @Enum("GOOGLE_STANDARD_SQL") |
| 57 | + Dialect getDialect(); |
| 58 | + |
| 59 | + void setDialect(Dialect dialect); |
| 60 | + |
| 61 | + @Description("Output filename for records size") |
| 62 | + @Validation.Required |
| 63 | + String getOutput(); |
| 64 | + |
| 65 | + void setOutput(String value); |
| 66 | + } |
| 67 | + |
| 68 | + public static void main(String[] args) { |
| 69 | + Options options = PipelineOptionsFactory.fromArgs(args).withValidation().as(Options.class); |
| 70 | + Pipeline pipeline = Pipeline.create(options); |
| 71 | + |
| 72 | + String instanceId = options.getInstanceId(); |
| 73 | + String databaseId = options.getDatabaseId(); |
| 74 | + Dialect dialect = options.getDialect(); |
| 75 | + PCollection<Struct> records; |
| 76 | + if (dialect == Dialect.POSTGRESQL) { |
| 77 | + records = postgreSqlRead(instanceId, databaseId, pipeline); |
| 78 | + } else { |
| 79 | + records = googleSqlRead(instanceId, databaseId, pipeline); |
| 80 | + } |
| 81 | + |
| 82 | + PCollection<Long> tableEstimatedSize = |
| 83 | + records |
| 84 | + // Estimate the size of every row |
| 85 | + .apply(EstimateSize.create()) |
| 86 | + // Sum all the row sizes to get the total estimated size of the table |
| 87 | + .apply(Sum.longsGlobally()); |
| 88 | + |
| 89 | + // Write the total size to a file |
| 90 | + tableEstimatedSize |
| 91 | + .apply(ToString.elements()) |
| 92 | + .apply(TextIO.write().to(options.getOutput()).withoutSharding()); |
| 93 | + |
| 94 | + pipeline.run().waitUntilFinish(); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * GoogleSQL databases retain the casing of table and column names. It is therefore common to use |
| 99 | + * CamelCase for identifiers. |
| 100 | + */ |
| 101 | + static PCollection<Struct> googleSqlRead( |
| 102 | + String instanceId, String databaseId, Pipeline pipeline) { |
| 103 | + // [START spanner_dataflow_readapi_withindex] |
| 104 | + // Query for all the columns and rows in the specified Spanner table |
| 105 | + PCollection<Struct> records = |
| 106 | + pipeline.apply( |
| 107 | + SpannerIO.read() |
| 108 | + .withInstanceId(instanceId) |
| 109 | + .withDatabaseId(databaseId) |
| 110 | + .withTable("Songs") |
| 111 | + .withIndex("SongsBySongName") |
| 112 | + // Can only read columns that are either indexed, STORED in the index or |
| 113 | + // part of the primary key of the Songs table, |
| 114 | + .withColumns("SingerId", "AlbumId", "TrackId", "SongName")); |
| 115 | + // [END spanner_dataflow_readapi_withindex] |
| 116 | + return records; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * PostgreSQL databases automatically fold identifiers to lower case. It is therefore common to |
| 121 | + * use all lower case identifiers with underscores to separate multiple words in an identifier. |
| 122 | + */ |
| 123 | + static PCollection<Struct> postgreSqlRead( |
| 124 | + String instanceId, String databaseId, Pipeline pipeline) { |
| 125 | + // [START spanner_pg_dataflow_readapi_withindex] |
| 126 | + // Query for all the columns and rows in the specified Spanner table |
| 127 | + PCollection<Struct> records = |
| 128 | + pipeline.apply( |
| 129 | + SpannerIO.read() |
| 130 | + .withInstanceId(instanceId) |
| 131 | + .withDatabaseId(databaseId) |
| 132 | + .withTable("Songs") |
| 133 | + .withIndex("SongsBySongName") |
| 134 | + // Can only read columns that are either indexed, STORED in the index or |
| 135 | + // part of the primary key of the songs table, |
| 136 | + .withColumns("singer_id", "album_id", "track_id", "song_name")); |
| 137 | + // [END spanner_pg_dataflow_readapi_withindex] |
| 138 | + return records; |
| 139 | + } |
| 140 | +} |
0 commit comments