Skip to content

Commit d0a3a3f

Browse files
authored
BQ Data Transfer: Add quickstart sample (#972)
* BQ Data Transfer: Add quickstart sample A sample showing how to use the BigQuery Data Transfer API to list available data sources. * Hello 2018. Update license headers for data transfer. Add info about auth. * Use Java 8 for data transfer sample. * Add bigquery data transfer samples to root pom.xml
1 parent e2ccaca commit d0a3a3f

File tree

7 files changed

+212
-1
lines changed

7 files changed

+212
-1
lines changed

bigquery/cloud-client/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ the [Google Cloud Client Library for Java][google-cloud-java].
1212

1313
Install [Maven](http://maven.apache.org/).
1414

15+
[Authenticate using a service account](https://cloud.google.com/docs/authentication/getting-started).
16+
Create a service account, download a JSON key file, and set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable.
17+
1518
Build your project with:
1619

1720
mvn clean package -DskipTests

bigquery/cloud-client/src/main/java/com/example/bigquery/QuickstartSample.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525

2626
public class QuickstartSample {
2727
public static void main(String... args) throws Exception {
28-
// Instantiates a client
28+
// Instantiate a client. If you don't specify credentials when constructing a client, the
29+
// client library will look for credentials in the environment, such as the
30+
// GOOGLE_APPLICATION_CREDENTIALS environment variable.
2931
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
3032

3133
// The name for the new dataset
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Getting Started with BigQuery Data Transfer API
2+
3+
[BigQuery Data Transfer Service][BigQuery Data Transfer] features an API that
4+
allows developers to create transfer jobs from data sources to BigQuery.
5+
These sample Java applications demonstrate how to access the BigQuery Data
6+
Transfer API using the [Google Cloud Client Library for
7+
Java][google-cloud-java].
8+
9+
[BigQuery Data Transfer]: https://cloud.google.com/bigquery/docs/transfer-service-overview
10+
[google-cloud-java]: https://github.com/GoogleCloudPlatform/google-cloud-java
11+
12+
## Quickstart
13+
14+
Install [Maven](http://maven.apache.org/).
15+
16+
[Authenticate using a service account](https://cloud.google.com/docs/authentication/getting-started).
17+
Create a service account, download a JSON key file, and set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable.
18+
19+
Build your project with:
20+
21+
mvn clean package -DskipTests
22+
23+
You can then run a given `ClassName` via:
24+
25+
mvn exec:java -Dexec.mainClass=com.example.bigquerydatatransfer.ClassName \
26+
-DpropertyName=propertyValue \
27+
-Dexec.args="any arguments to the app"
28+
29+
### Listing available data sources
30+
31+
mvn exec:java -Dexec.mainClass=com.example.bigquerydatatransfer.QuickstartSample \
32+
-Dexec.args='YOUR_PROJECT_ID'
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<!--
2+
Copyright 2018 Google LLC
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+
<project>
17+
<modelVersion>4.0.0</modelVersion>
18+
<groupId>com.example.bigquerydatatransfer</groupId>
19+
<artifactId>bigquerydatatransfer-google-cloud-samples</artifactId>
20+
<packaging>jar</packaging>
21+
22+
<!-- Parent defines config for testing & linting. -->
23+
<parent>
24+
<artifactId>doc-samples</artifactId>
25+
<groupId>com.google.cloud</groupId>
26+
<version>1.0.0</version>
27+
<relativePath>../../..</relativePath>
28+
</parent>
29+
30+
<properties>
31+
<maven.compiler.target>1.8</maven.compiler.target>
32+
<maven.compiler.source>1.8</maven.compiler.source>
33+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
34+
</properties>
35+
36+
<dependencies>
37+
<!-- [START dependencies] -->
38+
<dependency>
39+
<groupId>com.google.cloud</groupId>
40+
<artifactId>google-cloud-bigquerydatatransfer</artifactId>
41+
<version>0.32.0-beta</version>
42+
</dependency>
43+
<!-- [END dependencies] -->
44+
45+
<!-- Test dependencies -->
46+
<dependency>
47+
<groupId>junit</groupId>
48+
<artifactId>junit</artifactId>
49+
<version>4.12</version>
50+
<scope>test</scope>
51+
</dependency>
52+
<dependency>
53+
<groupId>com.google.truth</groupId>
54+
<artifactId>truth</artifactId>
55+
<version>0.36</version>
56+
<scope>test</scope>
57+
</dependency>
58+
</dependencies>
59+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
Copyright 2018 Google LLC
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.bigquerydatatransfer;
18+
19+
// [START bigquery_datatransfer_quickstart]
20+
// Imports the Google Cloud client library
21+
import com.google.cloud.bigquery.datatransfer.v1.DataSource;
22+
import com.google.cloud.bigquery.datatransfer.v1.DataTransferServiceClient;
23+
import com.google.cloud.bigquery.datatransfer.v1.ListDataSourcesRequest;
24+
import com.google.cloud.bigquery.datatransfer.v1.PagedResponseWrappers.ListDataSourcesPagedResponse;
25+
26+
public class QuickstartSample {
27+
/**
28+
* List available data sources for the BigQuery Data Transfer service.
29+
*/
30+
public static void main(String... args) throws Exception {
31+
// Sets your Google Cloud Platform project ID.
32+
// String projectId = "YOUR_PROJECT_ID";
33+
String projectId = args[0];
34+
35+
// Instantiate a client. If you don't specify credentials when constructing a client, the
36+
// client library will look for credentials in the environment, such as the
37+
// GOOGLE_APPLICATION_CREDENTIALS environment variable.
38+
try (DataTransferServiceClient client = DataTransferServiceClient.create()) {
39+
// Request the list of available data sources.
40+
String parent = String.format("projects/%s", projectId);
41+
ListDataSourcesRequest request =
42+
ListDataSourcesRequest.newBuilder()
43+
.setParent(parent)
44+
.build();
45+
ListDataSourcesPagedResponse response = client.listDataSources(request);
46+
47+
// Print the results.
48+
System.out.println("Supported Data Sources:");
49+
for (DataSource dataSource : response.iterateAll()) {
50+
System.out.println(dataSource.getDisplayName());
51+
System.out.printf("\tID: %s%n", dataSource.getDataSourceId());
52+
System.out.printf("\tFull path: %s%n", dataSource.getName());
53+
System.out.printf("\tDescription: %s%n", dataSource.getDescription());
54+
}
55+
}
56+
}
57+
}
58+
// [END bigquery_datatransfer_quickstart]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Copyright 2018 Google LLC
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.bigquerydatatransfer;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.PrintStream;
23+
import org.junit.After;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.junit.runners.JUnit4;
28+
29+
/** Tests for quickstart sample. */
30+
@RunWith(JUnit4.class)
31+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
32+
public class QuickstartSampleIT {
33+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
34+
35+
private ByteArrayOutputStream bout;
36+
private PrintStream out;
37+
38+
@Before
39+
public void setUp() {
40+
bout = new ByteArrayOutputStream();
41+
out = new PrintStream(bout);
42+
System.setOut(out);
43+
}
44+
45+
@After
46+
public void tearDown() {
47+
System.setOut(null);
48+
}
49+
50+
@Test
51+
public void testQuickstart() throws Exception {
52+
QuickstartSample.main(PROJECT_ID);
53+
String got = bout.toString();
54+
assertThat(got).contains("Supported Data Sources:");
55+
}
56+
}

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
<module>compute</module>
4848

4949
<module>bigquery/cloud-client</module>
50+
<module>bigquery/datatransfer/cloud-client</module>
5051
<module>bigquery/rest</module>
5152

5253
<module>dataflow/spanner-io</module>

0 commit comments

Comments
 (0)