Skip to content

Commit 237e3aa

Browse files
authored
Merge pull request #353 from GoogleCloudPlatform/quickstarts
Add google-cloud-java client libraries quickstart samples.
2 parents 1dfce92 + 1d255d9 commit 237e3aa

File tree

25 files changed

+931
-6
lines changed

25 files changed

+931
-6
lines changed

bigquery/cloud-client/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ You can then run a given `ClassName` via:
2222
-DpropertyName=propertyValue \
2323
-Dexec.args="any arguments to the app"
2424

25+
### Creating a new dataset (using the quickstart sample)
26+
27+
mvn exec:java -Dexec.mainClass=com.example.bigquery.QuickstartSample
28+
2529
### Running a synchronous query
2630

2731
mvn exec:java -Dexec.mainClass=com.example.bigquery.SyncQuerySample \

bigquery/cloud-client/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
<dependency>
5151
<groupId>com.google.truth</groupId>
5252
<artifactId>truth</artifactId>
53-
<version>0.29</version>
53+
<version>0.30</version>
5454
<scope>test</scope>
5555
</dependency>
5656
</dependencies>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Copyright 2016, 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.bigquery;
18+
19+
// [START bigquery_quickstart]
20+
// Imports the Google Cloud client library
21+
import com.google.cloud.bigquery.BigQuery;
22+
import com.google.cloud.bigquery.BigQueryOptions;
23+
import com.google.cloud.bigquery.Dataset;
24+
import com.google.cloud.bigquery.DatasetInfo;
25+
26+
public class QuickstartSample {
27+
public static void main(String... args) throws Exception {
28+
// Instantiates a client
29+
BigQuery bigquery = BigQueryOptions.defaultInstance().service();
30+
31+
// The name for the new dataset
32+
String datasetName = "my_new_dataset";
33+
34+
// Prepares a new dataset
35+
Dataset dataset = null;
36+
DatasetInfo datasetInfo = DatasetInfo.builder(datasetName).build();
37+
38+
// Creates the dataset
39+
dataset = bigquery.create(datasetInfo);
40+
41+
System.out.printf("Dataset %s created.%n", dataset.datasetId().dataset());
42+
}
43+
}
44+
// [END bigquery_quickstart]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
Copyright 2016, 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.bigquery;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.cloud.bigquery.BigQuery;
22+
import com.google.cloud.bigquery.BigQuery.DatasetDeleteOption;
23+
import com.google.cloud.bigquery.BigQueryOptions;
24+
import com.google.cloud.bigquery.DatasetId;
25+
import org.junit.After;
26+
import org.junit.Before;
27+
import org.junit.Test;
28+
import org.junit.runner.RunWith;
29+
import org.junit.runners.JUnit4;
30+
31+
import java.io.ByteArrayOutputStream;
32+
import java.io.PrintStream;
33+
34+
/**
35+
* Tests for quickstart sample.
36+
*/
37+
@RunWith(JUnit4.class)
38+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
39+
public class QuickstartSampleIT {
40+
private ByteArrayOutputStream bout;
41+
private PrintStream out;
42+
43+
private static final void deleteMyNewDataset() {
44+
BigQuery bigquery = BigQueryOptions.defaultInstance().service();
45+
String datasetName = "my_new_dataset";
46+
DatasetId datasetId = DatasetId.of(datasetName);
47+
DatasetDeleteOption deleteContents = DatasetDeleteOption.deleteContents();
48+
bigquery.delete(datasetId, deleteContents);
49+
}
50+
51+
@Before
52+
public void setUp() {
53+
deleteMyNewDataset();
54+
55+
bout = new ByteArrayOutputStream();
56+
out = new PrintStream(bout);
57+
System.setOut(out);
58+
}
59+
60+
@After
61+
public void tearDown() {
62+
System.setOut(null);
63+
deleteMyNewDataset();
64+
}
65+
66+
@Test
67+
public void testQuickstart() throws Exception {
68+
QuickstartSample.main();
69+
String got = bout.toString();
70+
assertThat(got).contains("Dataset my_new_dataset created.");
71+
}
72+
}

bigquery/cloud-client/src/test/java/com/example/bigquery/SyncQuerySampleTest.java renamed to bigquery/cloud-client/src/test/java/com/example/bigquery/SyncQuerySampleIT.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
* Tests for synchronous query sample.
3232
*/
3333
@RunWith(JUnit4.class)
34-
public class SyncQuerySampleTest {
34+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
35+
public class SyncQuerySampleIT {
3536
private ByteArrayOutputStream bout;
3637
private PrintStream out;
3738

datastore/cloud-client/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Getting Started with Cloud Datastore and the Google Cloud Client libraries
2+
3+
[Google Cloud Datastore][Datastore] is a highly-scalable NoSQL database for your applications.
4+
These sample Java applications demonstrate how to access the Datastore API using
5+
the [Google Cloud Client Library for Java][google-cloud-java].
6+
7+
[Datastore]: https://cloud.google.com/datastore/
8+
[google-cloud-java]: https://github.com/GoogleCloudPlatform/google-cloud-java
9+
10+
## Quickstart
11+
12+
Install [Maven](http://maven.apache.org/).
13+
14+
Build your project with:
15+
16+
mvn clean package -DskipTests
17+
18+
You can then run a given `ClassName` via:
19+
20+
mvn exec:java -Dexec.mainClass=com.example.bigquery.ClassName \
21+
-DpropertyName=propertyValue \
22+
-Dexec.args="any arguments to the app"
23+
24+
### Creating a new entity (using the quickstart sample)
25+
26+
mvn exec:java -Dexec.mainClass=com.example.datastore.QuickstartSample

datastore/cloud-client/pom.xml

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<!--
2+
Copyright 2016 Google Inc. All Rights Reserved.
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>come.example.datastore</groupId>
19+
<artifactId>datastore-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+
<dependency>
38+
<groupId>com.google.cloud</groupId>
39+
<artifactId>google-cloud-datastore</artifactId>
40+
<version>0.4.0</version>
41+
</dependency>
42+
43+
<!-- Test dependencies -->
44+
<dependency>
45+
<groupId>junit</groupId>
46+
<artifactId>junit</artifactId>
47+
<version>4.12</version>
48+
<scope>test</scope>
49+
</dependency>
50+
<dependency>
51+
<groupId>com.google.truth</groupId>
52+
<artifactId>truth</artifactId>
53+
<version>0.30</version>
54+
<scope>test</scope>
55+
</dependency>
56+
</dependencies>
57+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Copyright 2016, 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.datastore;
18+
19+
// [START datastore_quickstart]
20+
// Imports the Google Cloud client library
21+
import com.google.cloud.datastore.Datastore;
22+
import com.google.cloud.datastore.DatastoreOptions;
23+
import com.google.cloud.datastore.Entity;
24+
import com.google.cloud.datastore.Key;
25+
26+
public class QuickstartSample {
27+
public static void main(String... args) throws Exception {
28+
// Instantiates a client
29+
Datastore datastore = DatastoreOptions.defaultInstance().service();
30+
31+
// The kind for the new entity
32+
String kind = "Task";
33+
// The name/ID for the new entity
34+
String name = "sampletask1";
35+
// The Cloud Datastore key for the new entity
36+
Key taskKey = datastore.newKeyFactory().kind(kind).newKey(name);
37+
38+
// Prepares the new entity
39+
Entity task = Entity.builder(taskKey)
40+
.set("description", "Buy milk")
41+
.build();
42+
43+
// Saves the entity
44+
datastore.put(task);
45+
46+
System.out.printf("Saved %s: %s%n", task.key().name(), task.getString("description"));
47+
}
48+
}
49+
// [END datastore_quickstart]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
Copyright 2016, 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.datastore;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.cloud.datastore.Datastore;
22+
import com.google.cloud.datastore.DatastoreOptions;
23+
import com.google.cloud.datastore.Key;
24+
import org.junit.After;
25+
import org.junit.Before;
26+
import org.junit.Test;
27+
import org.junit.runner.RunWith;
28+
import org.junit.runners.JUnit4;
29+
30+
import java.io.ByteArrayOutputStream;
31+
import java.io.PrintStream;
32+
33+
/**
34+
* Tests for quickstart sample.
35+
*/
36+
@RunWith(JUnit4.class)
37+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
38+
public class QuickstartSampleIT {
39+
private ByteArrayOutputStream bout;
40+
private PrintStream out;
41+
42+
private static final void deleteTestEntity() {
43+
Datastore datastore = DatastoreOptions.defaultInstance().service();
44+
String kind = "Task";
45+
String name = "sampletask1";
46+
Key taskKey = datastore.newKeyFactory().kind(kind).newKey(name);
47+
datastore.delete(taskKey);
48+
}
49+
50+
@Before
51+
public void setUp() {
52+
deleteTestEntity();
53+
54+
bout = new ByteArrayOutputStream();
55+
out = new PrintStream(bout);
56+
System.setOut(out);
57+
}
58+
59+
@After
60+
public void tearDown() {
61+
System.setOut(null);
62+
deleteTestEntity();
63+
}
64+
65+
@Test
66+
public void testQuickstart() throws Exception {
67+
QuickstartSample.main();
68+
String got = bout.toString();
69+
assertThat(got).contains("Saved sampletask1: Buy milk");
70+
}
71+
}
72+
// [END datastore_quickstart]

pom.xml

+8-2
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,13 @@
7777
<module>appengine/users</module>
7878
<module>appengine/xmpp</module>
7979
<module>bigquery</module>
80+
<module>bigquery/cloud-client</module>
8081
<module>compute/cmdline</module>
8182
<module>compute/error-reporting</module>
8283
<module>compute/mailjet</module>
8384
<module>compute/sendgrid</module>
8485
<module>datastore</module>
85-
<module>logging</module>
86+
<module>datastore/cloud-client</module>
8687
<module>flexible/analytics</module>
8788
<module>flexible/async-rest</module>
8889
<module>flexible/cloudsql</module>
@@ -101,15 +102,20 @@
101102
-->
102103
<module>flexible/static-files</module>
103104
<module>flexible/twilio</module>
105+
<module>language/analysis</module>
106+
<module>logging</module>
104107
<module>monitoring/v2</module>
105108
<module>monitoring/v3</module>
109+
<module>pubsub/cloud-client</module>
106110
<module>speech/grpc</module>
111+
<module>storage/cloud-client</module>
107112
<module>storage/json-api</module>
108113
<module>storage/storage-transfer</module>
109114
<module>storage/xml-api/cmdline-sample</module>
110115
<module>storage/xml-api/serviceaccount-appengine-sample</module>
111116
<module>taskqueue/deferred</module>
112-
<module>language/analysis</module>
117+
<module>translate</module>
118+
<module>translate/cloud-client</module>
113119
<module>unittests</module>
114120
<module>vision/face-detection</module>
115121
<module>vision/label</module>

0 commit comments

Comments
 (0)