Skip to content

Commit a613201

Browse files
authored
---
yaml --- r: 5117 b: refs/heads/master c: 9ee8e1c h: refs/heads/master i: 5115: e34cd18
1 parent 9fbcdf9 commit a613201

8 files changed

Lines changed: 620 additions & 18 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 9fa618efb2c56388a04439b407cd80599050cbe5
2+
refs/heads/master: 9ee8e1c2317ae52e2feed7620f5123b8a8966c7c
33
refs/heads/travis: e21ee7b88a5edc3f3d8c71f90c3fc32abf7e8dd6
44
refs/heads/gh-pages: 7406918e071dd2c5677a638ae2a06e7592b6542c
55
refs/heads/pubsub-alpha: d6bbd32eed6cb48cda8d6798ee70ddd6bfc1f07d

trunk/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Job.java

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ public Job build() {
127127
/**
128128
* Checks if this job exists.
129129
*
130+
* <p>Example of checking that a job exists.
131+
* <pre> {@code
132+
* if (!job.exists()) {
133+
* // job doesn't exist
134+
* }
135+
* }</pre>
136+
*
130137
* @return {@code true} if this job exists, {@code false} otherwise
131138
* @throws BigQueryException upon failure
132139
*/
@@ -136,11 +143,14 @@ public boolean exists() {
136143

137144
/**
138145
* Checks if this job has completed its execution, either failing or succeeding. If the job does
139-
* not exist this method returns {@code true}. You can wait for job completion with:
146+
* not exist this method returns {@code true}.
147+
*
148+
* <p>Example of waiting for a job until it reports that it is done.
140149
* <pre> {@code
141-
* while(!job.isDone()) {
150+
* while (!job.isDone()) {
142151
* Thread.sleep(1000L);
143-
* }}</pre>
152+
* }
153+
* }</pre>
144154
*
145155
* @return {@code true} if this job is in {@link JobStatus.State#DONE} state or if it does not
146156
* exist, {@code false} if the state is not {@link JobStatus.State#DONE}
@@ -158,7 +168,7 @@ public boolean isDone() {
158168
* value use {@link WaitForOption#checkEvery(long, TimeUnit)}. Use
159169
* {@link WaitForOption#timeout(long, TimeUnit)} to set the maximum time to wait.
160170
*
161-
* <p>Example usage of {@code waitFor()}:
171+
* <p>Example usage of {@code waitFor()}.
162172
* <pre> {@code
163173
* Job completedJob = job.waitFor();
164174
* if (completedJob == null) {
@@ -167,19 +177,23 @@ public boolean isDone() {
167177
* // job failed, handle error
168178
* } else {
169179
* // job completed successfully
170-
* }}</pre>
180+
* }
181+
* }</pre>
171182
*
172-
* <p>Example usage of {@code waitFor()} with checking period and timeout:
183+
* <p>Example usage of {@code waitFor()} with checking period and timeout.
173184
* <pre> {@code
174-
* Job completedJob = job.waitFor(WaitForOption.checkEvery(1, TimeUnit.SECONDS),
175-
* WaitForOption.timeout(60, TimeUnit.SECONDS));
185+
* Job completedJob =
186+
* job.waitFor(
187+
* WaitForOption.checkEvery(1, TimeUnit.SECONDS),
188+
* WaitForOption.timeout(60, TimeUnit.SECONDS));
176189
* if (completedJob == null) {
177190
* // job no longer exists
178191
* } else if (completedJob.status().error() != null) {
179192
* // job failed, handle error
180193
* } else {
181194
* // job completed successfully
182-
* }}</pre>
195+
* }
196+
* }</pre>
183197
*
184198
* @param waitOptions options to configure checking period and timeout
185199
* @throws BigQueryException upon failure
@@ -207,6 +221,22 @@ public Job waitFor(WaitForOption... waitOptions) throws InterruptedException, Ti
207221
/**
208222
* Fetches current job's latest information. Returns {@code null} if the job does not exist.
209223
*
224+
* <p>Example of reloading all fields until job status is DONE.
225+
* <pre> {@code
226+
* while (job.status().state() != JobStatus.State.DONE) {
227+
* Thread.sleep(1000L);
228+
* job = job.reload();
229+
* }
230+
* }</pre>
231+
*
232+
* <p>Example of reloading status field until job status is DONE.
233+
* <pre> {@code
234+
* while (job.status().state() != JobStatus.State.DONE) {
235+
* Thread.sleep(1000L);
236+
* job = job.reload(BigQuery.JobOption.fields(BigQuery.JobField.STATUS));
237+
* }
238+
* }</pre>
239+
*
210240
* @param options job options
211241
* @return a {@code Job} object with latest information or {@code null} if not found
212242
* @throws BigQueryException upon failure
@@ -218,6 +248,15 @@ public Job reload(JobOption... options) {
218248
/**
219249
* Sends a job cancel request.
220250
*
251+
* <p>Example of cancelling a job.
252+
* <pre> {@code
253+
* if (job.cancel()) {
254+
* return true; // job successfully cancelled
255+
* } else {
256+
* // job not found
257+
* }
258+
* }</pre>
259+
*
221260
* @return {@code true} if cancel request was sent successfully, {@code false} if job was not
222261
* found
223262
* @throws BigQueryException upon failure

trunk/google-cloud-datastore/src/main/java/com/google/cloud/datastore/Query.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,15 @@ ToStringHelper toStringHelper() {
182182
/**
183183
* Returns a new {@link GqlQuery} builder.
184184
*
185+
* <p>Example of creating and running a GQL query.
186+
* <pre> {@code
187+
* String kind = "my_kind";
188+
* String gqlQuery = "select * from " + kind;
189+
* Query<?> query = Query.gqlQueryBuilder(gqlQuery).build();
190+
* QueryResults<?> results = datastore.run(query);
191+
* // Use results
192+
* }</pre>
193+
*
185194
* @see <a href="https://cloud.google.com/datastore/docs/apis/gql/gql_reference">GQL Reference</a>
186195
*/
187196
public static GqlQuery.Builder<?> gqlQueryBuilder(String gql) {
@@ -191,6 +200,15 @@ public static GqlQuery.Builder<?> gqlQueryBuilder(String gql) {
191200
/**
192201
* Returns a new {@link GqlQuery} builder.
193202
*
203+
* <p>Example of creating and running a typed GQL query.
204+
* <pre> {@code
205+
* String kind = "my_kind";
206+
* String gqlQuery = "select * from " + kind;
207+
* Query<Entity> query = Query.gqlQueryBuilder(Query.ResultType.ENTITY, gqlQuery).build();
208+
* QueryResults<Entity> results = datastore.run(query);
209+
* // Use results
210+
* }</pre>
211+
*
194212
* @see <a href="https://cloud.google.com/datastore/docs/apis/gql/gql_reference">GQL Reference</a>
195213
*/
196214
public static <V> GqlQuery.Builder<V> gqlQueryBuilder(ResultType<V> resultType, String gql) {
@@ -199,20 +217,51 @@ public static <V> GqlQuery.Builder<V> gqlQueryBuilder(ResultType<V> resultType,
199217

200218
/**
201219
* Returns a new {@link StructuredQuery} builder for full (complete entities) queries.
220+
*
221+
* <p>Example of creating and running an entity query.
222+
* <pre> {@code
223+
* String kind = "my_kind";
224+
* Query<Entity> query = Query.entityQueryBuilder().kind(kind).build();
225+
* QueryResults<Entity> results = datastore.run(query);
226+
* // Use results
227+
* }</pre>
228+
*
202229
*/
203230
public static EntityQuery.Builder entityQueryBuilder() {
204231
return new EntityQuery.Builder();
205232
}
206233

207234
/**
208235
* Returns a new {@link StructuredQuery} builder for key only queries.
236+
*
237+
* <p>Example of creating and running a key query.
238+
* <pre> {@code
239+
* String kind = "my_kind";
240+
* Query<Key> query = Query.keyQueryBuilder().kind(kind).build();
241+
* QueryResults<Key> results = datastore.run(query);
242+
* // Use results
243+
* }</pre>
244+
*
209245
*/
210246
public static KeyQuery.Builder keyQueryBuilder() {
211247
return new KeyQuery.Builder();
212248
}
213249

214250
/**
215251
* Returns a new {@link StructuredQuery} builder for projection queries.
252+
*
253+
* <p>Example of creating and running a projection entity query.
254+
* <pre> {@code
255+
* String kind = "my_kind";
256+
* String property = "my_property";
257+
* Query<ProjectionEntity> query = Query.projectionEntityQueryBuilder()
258+
* .kind(kind)
259+
* .addProjection(property)
260+
* .build();
261+
* QueryResults<ProjectionEntity> results = datastore.run(query);
262+
* // Use results
263+
* }</pre>
264+
*
216265
*/
217266
public static ProjectionEntityQuery.Builder projectionEntityQueryBuilder() {
218267
return new ProjectionEntityQuery.Builder();

trunk/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/InsertDataAndQueryTable.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,8 @@ public static void main(String... args) throws InterruptedException {
7070
firstRow.put("StringField", "value1");
7171
secondRow.put("StringField", "value2");
7272
// Create an insert request
73-
InsertAllRequest insertRequest = InsertAllRequest.builder(tableId)
74-
.addRow(firstRow)
75-
.addRow(secondRow)
76-
.build();
73+
InsertAllRequest insertRequest =
74+
InsertAllRequest.builder(tableId).addRow(firstRow).addRow(secondRow).build();
7775
// Insert rows
7876
InsertAllResponse insertResponse = bigquery.insertAll(insertRequest);
7977
// Check if errors occurred
@@ -82,10 +80,11 @@ public static void main(String... args) throws InterruptedException {
8280
}
8381

8482
// Create a query request
85-
QueryRequest queryRequest = QueryRequest.builder("SELECT * FROM my_dataset_id.my_table_id")
86-
.maxWaitTime(60000L)
87-
.pageSize(1000L)
88-
.build();
83+
QueryRequest queryRequest =
84+
QueryRequest.builder("SELECT * FROM my_dataset_id.my_table_id")
85+
.maxWaitTime(60000L)
86+
.pageSize(1000L)
87+
.build();
8988
// Request query to be executed and wait for results
9089
QueryResponse queryResponse = bigquery.query(queryRequest);
9190
while (!queryResponse.jobCompleted()) {
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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+
17+
/*
18+
* EDITING INSTRUCTIONS
19+
* This file is referenced in Job's javadoc. Any change to this file should be reflected in
20+
* Job's javadoc.
21+
*/
22+
23+
package com.google.cloud.examples.bigquery.snippets;
24+
25+
import com.google.cloud.WaitForOption;
26+
import com.google.cloud.bigquery.BigQuery;
27+
import com.google.cloud.bigquery.Job;
28+
import com.google.cloud.bigquery.JobStatus;
29+
30+
import java.util.concurrent.TimeUnit;
31+
import java.util.concurrent.TimeoutException;
32+
33+
public class JobSnippets {
34+
35+
private Job job;
36+
37+
public JobSnippets(Job job) {
38+
this.job = job;
39+
}
40+
41+
/**
42+
* Example of checking that a job exists.
43+
*/
44+
// [TARGET exists()]
45+
public boolean exists() throws InterruptedException {
46+
// [START exists]
47+
if (!job.exists()) {
48+
// job doesn't exist
49+
}
50+
// [END exists]
51+
return job.exists();
52+
}
53+
54+
/**
55+
* Example of waiting for a job until it reports that it is done.
56+
*/
57+
// [TARGET isDone()]
58+
public void isDone() throws InterruptedException {
59+
// [START isDone]
60+
while (!job.isDone()) {
61+
Thread.sleep(1000L);
62+
}
63+
// [END isDone]
64+
}
65+
66+
/**
67+
* Example usage of {@code waitFor()}.
68+
*/
69+
// [TARGET waitFor(WaitForOption...)]
70+
public boolean waitFor() throws InterruptedException {
71+
try {
72+
// [START waitFor]
73+
Job completedJob = job.waitFor();
74+
if (completedJob == null) {
75+
// job no longer exists
76+
} else if (completedJob.status().error() != null) {
77+
// job failed, handle error
78+
} else {
79+
// job completed successfully
80+
}
81+
// [END waitFor]
82+
} catch (TimeoutException e) {
83+
// Timeouts shouldn't happen without a timeout option.
84+
return false;
85+
}
86+
return true;
87+
}
88+
89+
/**
90+
* Example usage of {@code waitFor()} with checking period and timeout.
91+
*/
92+
// [TARGET waitFor(WaitForOption...)]
93+
public boolean waitForWithOptions() throws InterruptedException {
94+
try {
95+
// [START waitForWithOptions]
96+
Job completedJob =
97+
job.waitFor(
98+
WaitForOption.checkEvery(1, TimeUnit.SECONDS),
99+
WaitForOption.timeout(60, TimeUnit.SECONDS));
100+
if (completedJob == null) {
101+
// job no longer exists
102+
} else if (completedJob.status().error() != null) {
103+
// job failed, handle error
104+
} else {
105+
// job completed successfully
106+
}
107+
// [END waitForWithOptions]
108+
} catch (TimeoutException e) {
109+
return true;
110+
}
111+
return true;
112+
}
113+
114+
/**
115+
* Example of reloading all fields until job status is DONE.
116+
*/
117+
// [TARGET reload(JobOption...)]
118+
public JobStatus.State reload() throws InterruptedException {
119+
// [START reload]
120+
while (job.status().state() != JobStatus.State.DONE) {
121+
Thread.sleep(1000L);
122+
job = job.reload();
123+
}
124+
// [END reload]
125+
return job.status().state();
126+
}
127+
128+
/**
129+
* Example of reloading status field until job status is DONE.
130+
*/
131+
// [TARGET reload(JobOption...)]
132+
public JobStatus.State reloadStatus() throws InterruptedException {
133+
// [START reloadStatus]
134+
while (job.status().state() != JobStatus.State.DONE) {
135+
Thread.sleep(1000L);
136+
job = job.reload(BigQuery.JobOption.fields(BigQuery.JobField.STATUS));
137+
}
138+
// [END reloadStatus]
139+
return job.status().state();
140+
}
141+
142+
/**
143+
* Example of cancelling a job.
144+
*/
145+
// [TARGET cancel()]
146+
public boolean cancel() {
147+
// [START cancel]
148+
if (job.cancel()) {
149+
return true; // job successfully cancelled
150+
} else {
151+
// job not found
152+
}
153+
// [END cancel]
154+
return false;
155+
}
156+
}

0 commit comments

Comments
 (0)