From 1b89481c196931b387230144b28885316b3ee3f0 Mon Sep 17 00:00:00 2001 From: Tim Swast <swast@google.com> Date: Thu, 28 Dec 2017 16:18:39 -0800 Subject: [PATCH 1/4] BQ Data Transfer: Add quickstart sample A sample showing how to use the BigQuery Data Transfer API to list available data sources. --- bigquery/datatransfer/cloud-client/README.md | 29 +++++++++ bigquery/datatransfer/cloud-client/pom.xml | 59 +++++++++++++++++++ .../QuickstartSample.java | 56 ++++++++++++++++++ .../QuickstartSampleIT.java | 56 ++++++++++++++++++ 4 files changed, 200 insertions(+) create mode 100644 bigquery/datatransfer/cloud-client/README.md create mode 100644 bigquery/datatransfer/cloud-client/pom.xml create mode 100644 bigquery/datatransfer/cloud-client/src/main/java/com/example/bigquerydatatransfer/QuickstartSample.java create mode 100644 bigquery/datatransfer/cloud-client/src/test/java/com/example/bigquerydatatransfer/QuickstartSampleIT.java diff --git a/bigquery/datatransfer/cloud-client/README.md b/bigquery/datatransfer/cloud-client/README.md new file mode 100644 index 00000000000..e4131d2a6ad --- /dev/null +++ b/bigquery/datatransfer/cloud-client/README.md @@ -0,0 +1,29 @@ +# Getting Started with BigQuery Data Transfer API + +[BigQuery Data Transfer Service][BigQuery Data Transfer] features an API that +allows developers to create transfer jobs from data sources to BigQuery. +These sample Java applications demonstrate how to access the BigQuery Data +Transfer API using the [Google Cloud Client Library for +Java][google-cloud-java]. + +[BigQuery Data Transfer]: https://cloud.google.com/bigquery/docs/transfer-service-overview +[google-cloud-java]: https://github.com/GoogleCloudPlatform/google-cloud-java + +## Quickstart + +Install [Maven](http://maven.apache.org/). + +Build your project with: + + mvn clean package -DskipTests + +You can then run a given `ClassName` via: + + mvn exec:java -Dexec.mainClass=com.example.bigquerydatatransfer.ClassName \ + -DpropertyName=propertyValue \ + -Dexec.args="any arguments to the app" + +### Listing available data sources + + mvn exec:java -Dexec.mainClass=com.example.bigquerydatatransfer.QuickstartSample \ + -Dexec.args='YOUR_PROJECT_ID' diff --git a/bigquery/datatransfer/cloud-client/pom.xml b/bigquery/datatransfer/cloud-client/pom.xml new file mode 100644 index 00000000000..1bed58fb547 --- /dev/null +++ b/bigquery/datatransfer/cloud-client/pom.xml @@ -0,0 +1,59 @@ +<!-- + Copyright 2016 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> + <modelVersion>4.0.0</modelVersion> + <groupId>com.example.bigquerydatatransfer</groupId> + <artifactId>bigquery-google-cloud-samples</artifactId> + <packaging>jar</packaging> + + <!-- Parent defines config for testing & linting. --> + <parent> + <artifactId>doc-samples</artifactId> + <groupId>com.google.cloud</groupId> + <version>1.0.0</version> + <relativePath>../../..</relativePath> + </parent> + + <properties> + <maven.compiler.target>1.7</maven.compiler.target> + <maven.compiler.source>1.7</maven.compiler.source> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> + + <dependencies> + <!-- [START dependencies] --> + <dependency> + <groupId>com.google.cloud</groupId> + <artifactId>google-cloud-bigquerydatatransfer</artifactId> + <version>0.32.0-beta</version> + </dependency> + <!-- [END dependencies] --> + + <!-- Test dependencies --> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.12</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>com.google.truth</groupId> + <artifactId>truth</artifactId> + <version>0.36</version> + <scope>test</scope> + </dependency> + </dependencies> +</project> diff --git a/bigquery/datatransfer/cloud-client/src/main/java/com/example/bigquerydatatransfer/QuickstartSample.java b/bigquery/datatransfer/cloud-client/src/main/java/com/example/bigquerydatatransfer/QuickstartSample.java new file mode 100644 index 00000000000..772af8f53b7 --- /dev/null +++ b/bigquery/datatransfer/cloud-client/src/main/java/com/example/bigquerydatatransfer/QuickstartSample.java @@ -0,0 +1,56 @@ +/* + Copyright 2017, Google LLC + + 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.bigquerydatatransfer; + +// [START bigquery_datatransfer_quickstart] +// Imports the Google Cloud client library +import com.google.cloud.bigquery.datatransfer.v1.DataSource; +import com.google.cloud.bigquery.datatransfer.v1.DataTransferServiceClient; +import com.google.cloud.bigquery.datatransfer.v1.ListDataSourcesRequest; +import com.google.cloud.bigquery.datatransfer.v1.PagedResponseWrappers.ListDataSourcesPagedResponse; + +public class QuickstartSample { + /** + * List available data sources for the BigQuery Data Transfer service. + */ + public static void main(String... args) throws Exception { + // Sets your Google Cloud Platform project ID. + // String projectId = "YOUR_PROJECT_ID"; + String projectId = args[0]; + + // Creates a client. + try (DataTransferServiceClient client = DataTransferServiceClient.create()) { + // Request the list of available data sources. + String parent = String.format("projects/%s", projectId); + ListDataSourcesRequest request = + ListDataSourcesRequest.newBuilder() + .setParent(parent) + .build(); + ListDataSourcesPagedResponse response = client.listDataSources(request); + + // Print the results. + System.out.println("Supported Data Sources:"); + for (DataSource dataSource : response.iterateAll()) { + System.out.println(dataSource.getDisplayName()); + System.out.printf("\tID: %s%n", dataSource.getDataSourceId()); + System.out.printf("\tFull path: %s%n", dataSource.getName()); + System.out.printf("\tDescription: %s%n", dataSource.getDescription()); + } + } + } +} +// [END bigquery_datatransfer_quickstart] diff --git a/bigquery/datatransfer/cloud-client/src/test/java/com/example/bigquerydatatransfer/QuickstartSampleIT.java b/bigquery/datatransfer/cloud-client/src/test/java/com/example/bigquerydatatransfer/QuickstartSampleIT.java new file mode 100644 index 00000000000..85acb983d7b --- /dev/null +++ b/bigquery/datatransfer/cloud-client/src/test/java/com/example/bigquerydatatransfer/QuickstartSampleIT.java @@ -0,0 +1,56 @@ +/* + Copyright 2017, Google LLC + + 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.bigquerydatatransfer; + +import static com.google.common.truth.Truth.assertThat; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for quickstart sample. */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class QuickstartSampleIT { + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testQuickstart() throws Exception { + QuickstartSample.main(PROJECT_ID); + String got = bout.toString(); + assertThat(got).contains("Supported Data Sources:"); + } +} From 391db12fa1a1cbdf1088eefc8f562065b9b18261 Mon Sep 17 00:00:00 2001 From: Tim Swast <swast@google.com> Date: Tue, 2 Jan 2018 11:03:03 -0800 Subject: [PATCH 2/4] Hello 2018. Update license headers for data transfer. Add info about auth. --- bigquery/cloud-client/README.md | 3 +++ .../main/java/com/example/bigquery/QuickstartSample.java | 4 +++- bigquery/datatransfer/cloud-client/README.md | 3 +++ bigquery/datatransfer/cloud-client/pom.xml | 2 +- .../com/example/bigquerydatatransfer/QuickstartSample.java | 6 ++++-- .../example/bigquerydatatransfer/QuickstartSampleIT.java | 2 +- 6 files changed, 15 insertions(+), 5 deletions(-) diff --git a/bigquery/cloud-client/README.md b/bigquery/cloud-client/README.md index a8dd6cf6702..6b86d256e79 100644 --- a/bigquery/cloud-client/README.md +++ b/bigquery/cloud-client/README.md @@ -12,6 +12,9 @@ the [Google Cloud Client Library for Java][google-cloud-java]. Install [Maven](http://maven.apache.org/). +[Authenticate using a service account](https://cloud.google.com/docs/authentication/getting-started). +Create a service account, download a JSON key file, and set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable. + Build your project with: mvn clean package -DskipTests diff --git a/bigquery/cloud-client/src/main/java/com/example/bigquery/QuickstartSample.java b/bigquery/cloud-client/src/main/java/com/example/bigquery/QuickstartSample.java index e1b40c4e7cd..b2fce8bcf00 100644 --- a/bigquery/cloud-client/src/main/java/com/example/bigquery/QuickstartSample.java +++ b/bigquery/cloud-client/src/main/java/com/example/bigquery/QuickstartSample.java @@ -25,7 +25,9 @@ public class QuickstartSample { public static void main(String... args) throws Exception { - // Instantiates a client + // Instantiate a client. If you don't specify credentials when constructing a client, the + // client library will look for credentials in the environment, such as the + // GOOGLE_APPLICATION_CREDENTIALS environment variable. BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); // The name for the new dataset diff --git a/bigquery/datatransfer/cloud-client/README.md b/bigquery/datatransfer/cloud-client/README.md index e4131d2a6ad..b3599d89c71 100644 --- a/bigquery/datatransfer/cloud-client/README.md +++ b/bigquery/datatransfer/cloud-client/README.md @@ -13,6 +13,9 @@ Java][google-cloud-java]. Install [Maven](http://maven.apache.org/). +[Authenticate using a service account](https://cloud.google.com/docs/authentication/getting-started). +Create a service account, download a JSON key file, and set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable. + Build your project with: mvn clean package -DskipTests diff --git a/bigquery/datatransfer/cloud-client/pom.xml b/bigquery/datatransfer/cloud-client/pom.xml index 1bed58fb547..b175e021155 100644 --- a/bigquery/datatransfer/cloud-client/pom.xml +++ b/bigquery/datatransfer/cloud-client/pom.xml @@ -1,5 +1,5 @@ <!-- - Copyright 2016 Google Inc. + Copyright 2018 Google LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/bigquery/datatransfer/cloud-client/src/main/java/com/example/bigquerydatatransfer/QuickstartSample.java b/bigquery/datatransfer/cloud-client/src/main/java/com/example/bigquerydatatransfer/QuickstartSample.java index 772af8f53b7..0fad9ebed87 100644 --- a/bigquery/datatransfer/cloud-client/src/main/java/com/example/bigquerydatatransfer/QuickstartSample.java +++ b/bigquery/datatransfer/cloud-client/src/main/java/com/example/bigquerydatatransfer/QuickstartSample.java @@ -1,5 +1,5 @@ /* - Copyright 2017, Google LLC + Copyright 2018 Google LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -32,7 +32,9 @@ public static void main(String... args) throws Exception { // String projectId = "YOUR_PROJECT_ID"; String projectId = args[0]; - // Creates a client. + // Instantiate a client. If you don't specify credentials when constructing a client, the + // client library will look for credentials in the environment, such as the + // GOOGLE_APPLICATION_CREDENTIALS environment variable. try (DataTransferServiceClient client = DataTransferServiceClient.create()) { // Request the list of available data sources. String parent = String.format("projects/%s", projectId); diff --git a/bigquery/datatransfer/cloud-client/src/test/java/com/example/bigquerydatatransfer/QuickstartSampleIT.java b/bigquery/datatransfer/cloud-client/src/test/java/com/example/bigquerydatatransfer/QuickstartSampleIT.java index 85acb983d7b..01de37c5d9e 100644 --- a/bigquery/datatransfer/cloud-client/src/test/java/com/example/bigquerydatatransfer/QuickstartSampleIT.java +++ b/bigquery/datatransfer/cloud-client/src/test/java/com/example/bigquerydatatransfer/QuickstartSampleIT.java @@ -1,5 +1,5 @@ /* - Copyright 2017, Google LLC + Copyright 2018 Google LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. From 49d606e9dbd434a376169c627589c7a13e4efdb8 Mon Sep 17 00:00:00 2001 From: Tim Swast <swast@google.com> Date: Tue, 2 Jan 2018 11:06:11 -0800 Subject: [PATCH 3/4] Use Java 8 for data transfer sample. --- bigquery/datatransfer/cloud-client/pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bigquery/datatransfer/cloud-client/pom.xml b/bigquery/datatransfer/cloud-client/pom.xml index b175e021155..fec5810b79c 100644 --- a/bigquery/datatransfer/cloud-client/pom.xml +++ b/bigquery/datatransfer/cloud-client/pom.xml @@ -28,8 +28,8 @@ </parent> <properties> - <maven.compiler.target>1.7</maven.compiler.target> - <maven.compiler.source>1.7</maven.compiler.source> + <maven.compiler.target>1.8</maven.compiler.target> + <maven.compiler.source>1.8</maven.compiler.source> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> From 879a3fb9552299bd3c24b98c7e50f83993eca715 Mon Sep 17 00:00:00 2001 From: Tim Swast <swast@google.com> Date: Tue, 2 Jan 2018 14:23:05 -0800 Subject: [PATCH 4/4] Add bigquery data transfer samples to root pom.xml --- bigquery/datatransfer/cloud-client/pom.xml | 2 +- pom.xml | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/bigquery/datatransfer/cloud-client/pom.xml b/bigquery/datatransfer/cloud-client/pom.xml index fec5810b79c..eec8a9a49ea 100644 --- a/bigquery/datatransfer/cloud-client/pom.xml +++ b/bigquery/datatransfer/cloud-client/pom.xml @@ -16,7 +16,7 @@ <project> <modelVersion>4.0.0</modelVersion> <groupId>com.example.bigquerydatatransfer</groupId> - <artifactId>bigquery-google-cloud-samples</artifactId> + <artifactId>bigquerydatatransfer-google-cloud-samples</artifactId> <packaging>jar</packaging> <!-- Parent defines config for testing & linting. --> diff --git a/pom.xml b/pom.xml index 0f8154e45e7..1c3707418e0 100644 --- a/pom.xml +++ b/pom.xml @@ -47,6 +47,7 @@ <module>compute</module> <module>bigquery/cloud-client</module> + <module>bigquery/datatransfer/cloud-client</module> <module>bigquery/rest</module> <module>dataflow/spanner-io</module>