-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dataflow SpannerIO sample #931
Merged
lesv
merged 9 commits into
GoogleCloudPlatform:master
from
davidcavazos:dataflow-spanner
Dec 4, 2017
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
97b69e8
Add Dataflow SpannerIO sample
davidcavazos 2e8aee5
Removed counts and just left the size estimation
davidcavazos f57da14
Tidy up dependencies
davidcavazos f9de752
Add sample to write to Spanner
davidcavazos 19aee9b
Add dataflow module
davidcavazos 6e67266
Cleaned up dependencies
davidcavazos 8cba515
Added more comments and a description on how to run
davidcavazos 4acc43e
Merge branch 'master' into dataflow-spanner
c73f62f
Merge branch 'master' into dataflow-spanner
lesv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<!-- | ||
Copyright 2017 Google Inc. | ||
|
||
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. | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.example.dataflow</groupId> | ||
<artifactId>dataflow-spanner</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<java.version>1.8</java.version> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<apache_beam.version>2.2.0</apache_beam.version> | ||
</properties> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.7.0</version> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
<dependencies> | ||
|
||
<!-- Apache Beam --> | ||
<dependency> | ||
<groupId>org.apache.beam</groupId> | ||
<artifactId>beam-sdks-java-core</artifactId> | ||
<version>${apache_beam.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.apache.beam</groupId> | ||
<artifactId>beam-sdks-java-io-google-cloud-platform</artifactId> | ||
<version>${apache_beam.version}</version> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>com.google.api.grpc</groupId> | ||
<artifactId>grpc-google-common-protos</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.apache.beam</groupId> | ||
<artifactId>beam-runners-direct-java</artifactId> | ||
<version>${apache_beam.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.apache.beam</groupId> | ||
<artifactId>beam-runners-google-cloud-dataflow-java</artifactId> | ||
<version>${apache_beam.version}</version> | ||
</dependency> | ||
|
||
<!-- Google Cloud --> | ||
<dependency> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>google-cloud-spanner</artifactId> | ||
<version>0.20.0-beta</version> | ||
</dependency> | ||
|
||
<!-- Misc --> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-jdk14</artifactId> | ||
<version>1.7.25</version> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
</project> |
154 changes: 154 additions & 0 deletions
154
dataflow/spanner-io/src/main/java/com/example/dataflow/SpannerRead.java
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,154 @@ | ||
/* | ||
Copyright 2017, Google, Inc. | ||
|
||
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.example.dataflow; | ||
|
||
import com.google.cloud.spanner.Struct; | ||
import org.apache.beam.sdk.Pipeline; | ||
import org.apache.beam.sdk.io.TextIO; | ||
import org.apache.beam.sdk.io.gcp.spanner.SpannerIO; | ||
import org.apache.beam.sdk.options.Description; | ||
import org.apache.beam.sdk.options.PipelineOptions; | ||
import org.apache.beam.sdk.options.PipelineOptionsFactory; | ||
import org.apache.beam.sdk.options.Validation; | ||
import org.apache.beam.sdk.transforms.Count; | ||
import org.apache.beam.sdk.transforms.DoFn; | ||
import org.apache.beam.sdk.transforms.PTransform; | ||
import org.apache.beam.sdk.transforms.ParDo; | ||
import org.apache.beam.sdk.transforms.Sum; | ||
import org.apache.beam.sdk.transforms.ToString; | ||
import org.apache.beam.sdk.values.PCollection; | ||
|
||
/* | ||
This sample demonstrates how to read from a Spanner table. | ||
|
||
## Prerequisites | ||
* Maven installed | ||
* Set up GCP default credentials, one of the following: | ||
- export GOOGLE_APPLICATION_CREDENTIALS=path/to/credentials.json | ||
- gcloud auth application-default login | ||
[https://developers.google.com/identity/protocols/application-default-credentials] | ||
* Create the Spanner table to read from, you'll need: | ||
- Instance ID | ||
- Database ID | ||
- Any table, preferably populated | ||
[https://cloud.google.com/spanner/docs/quickstart-console] | ||
|
||
## How to run | ||
cd java-docs-samples/dataflow/spanner-io | ||
mvn clean | ||
mvn compile | ||
mvn exec:java \ | ||
-Dexec.mainClass=com.example.dataflow.SpannerRead \ | ||
-Dexec.args="--instanceId=my-instance-id \ | ||
--databaseId=my-database-id \ | ||
--table=my_table \ | ||
--output=path/to/output_file" | ||
*/ | ||
public class SpannerRead { | ||
|
||
public interface Options extends PipelineOptions { | ||
|
||
@Description("Spanner instance ID to query from") | ||
@Validation.Required | ||
String getInstanceId(); | ||
void setInstanceId(String value); | ||
|
||
@Description("Spanner database name to query from") | ||
@Validation.Required | ||
String getDatabaseId(); | ||
void setDatabaseId(String value); | ||
|
||
@Description("Spanner table name to query from") | ||
@Validation.Required | ||
String getTable(); | ||
void setTable(String value); | ||
|
||
@Description("Output filename for records size") | ||
@Validation.Required | ||
String getOutput(); | ||
void setOutput(String value); | ||
} | ||
|
||
/** | ||
* Estimates the size of a Spanner row. For simplicity, arrays and structs aren't supported. | ||
*/ | ||
public static class EstimateStructSizeFn extends DoFn<Struct, Long> { | ||
|
||
@ProcessElement | ||
public void processElement(ProcessContext c) throws Exception { | ||
Struct row = c.element(); | ||
long sum = 0; | ||
for (int i = 0; i < row.getColumnCount(); i++) { | ||
if (row.isNull(i)) { | ||
continue; | ||
} | ||
|
||
switch (row.getColumnType(i).getCode()) { | ||
case BOOL: | ||
sum += 1; | ||
break; | ||
case INT64: | ||
case FLOAT64: | ||
sum += 8; | ||
break; | ||
case TIMESTAMP: | ||
case DATE: | ||
sum += 12; | ||
break; | ||
case BYTES: | ||
sum += row.getBytes(i).length(); | ||
break; | ||
case STRING: | ||
sum += row.getString(i).length(); | ||
break; | ||
case ARRAY: | ||
throw new IllegalArgumentException("Arrays are not supported :("); | ||
case STRUCT: | ||
throw new IllegalArgumentException("Structs are not supported :("); | ||
} | ||
} | ||
c.output(sum); | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
Options options = PipelineOptionsFactory.fromArgs(args).withValidation().as(Options.class); | ||
Pipeline p = Pipeline.create(options); | ||
|
||
String instanceId = options.getInstanceId(); | ||
String databaseId = options.getDatabaseId(); | ||
String query = "SELECT * FROM " + options.getTable(); | ||
|
||
PCollection<Long> tableEstimatedSize = p | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we also create a sample that uses Read API? |
||
// Query for all the columns and rows in the specified Spanner table | ||
.apply(SpannerIO.read() | ||
.withInstanceId(instanceId) | ||
.withDatabaseId(databaseId) | ||
.withQuery(query)) | ||
// Estimate the size of every row | ||
.apply(ParDo.of(new EstimateStructSizeFn())) | ||
// Sum all the row sizes to get the total estimated size of the table | ||
.apply(Sum.longsGlobally()); | ||
|
||
// Write the total size to a file | ||
tableEstimatedSize | ||
.apply(ToString.elements()) | ||
.apply(TextIO.write().to(options.getOutput())); | ||
|
||
p.run().waitUntilFinish(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are you sure this is required ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apparently not, removed