Skip to content

Move examples from the cloud-storage-docs-xml-api-examples repo, and update them to use Application Default Credentials. #14

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

Merged
merged 27 commits into from
May 29, 2015
Merged
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
bafd8c3
Move from cloud-storagedocs-xml-api-examples
May 22, 2015
fed4ae8
Update READMEs for new dir structure
May 22, 2015
9ebe7fa
Rename directories to be less redundant.
May 22, 2015
dcf91cc
Update to use App Default Creds.
May 22, 2015
ccf5fc8
Remove package, so eclipse isn't confused.
May 22, 2015
dd10b2c
Add Application Default Credentials quirks.
May 22, 2015
309d399
Update: no longer using service accounts.
May 26, 2015
f606873
Merge remote-tracking branch 'origin/master' into jerjou/xml-api
May 28, 2015
014079d
Add checkstyle plugin
May 28, 2015
c2b6935
More check style configuration
May 28, 2015
1b9f309
Fix style on DeferSampleServlet
May 28, 2015
45920e1
More check style changes
May 28, 2015
0e4514f
Finish fixing Async Query and Defer Sample
May 28, 2015
99a7533
Fix style in BigqueryServiceFactory
May 28, 2015
3f58947
Fix style errors on BigqueryUtils
May 28, 2015
8fc1c6d
Fix style on ExportDataCloudStorageSample
May 28, 2015
e514139
Fix style on LoadDataCsvSample
May 28, 2015
d989529
Remove vestigial key.json. README points to doc
May 28, 2015
78daa80
Fix StreamingSample checkstyle
May 28, 2015
005689f
Fix SyncQuerySample
May 28, 2015
69e24dc
Fail on bad style
May 28, 2015
b34b01c
Fix java style violations.
May 28, 2015
5019fb6
Merge branch 'jerjou/xml-api' of github.com:GoogleCloudPlatform/java-…
May 28, 2015
957da6b
Fix java style violations.
May 28, 2015
a0d74ee
Remove unused 'all' tag.
May 28, 2015
3f26d5b
Style: prettier line break.
May 29, 2015
a33bbb0
AsyncQuerySample extends BigqueryUtils, so can't be final
May 29, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix style errors on BigqueryUtils
  • Loading branch information
Bill Prin committed May 28, 2015
commit 3f5894771d710b2e26fdcaf66e4ca05400fa17e9
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/*
Copyright 2015, 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
Copyright 2015, 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.google.cloud.bigquery.samples;
Expand All @@ -35,12 +35,26 @@
import java.util.NoSuchElementException;

/**
* TODO: Insert description here. (generated by elibixby)
* Helper functions for the other classes.
*/
public class BigqueryUtils {

public final class BigqueryUtils {

/**
* Private contructor to prevent creation of this class, which is just all
* static helper methods.
*/
private BigqueryUtils() {

}

/**
* Print rows to the output stream in a formatted way.
* @param rows rows in bigquery
* @param out Output stream we want to print to
*/
// [START print_rows]
public static void printRows(List<TableRow> rows, PrintStream out){
public static void printRows(final List<TableRow> rows, final PrintStream
out) {
for (TableRow row : rows) {
for (TableCell field : row.getF()) {
out.printf("%-50s", field.getV());
Expand All @@ -49,40 +63,73 @@ public static void printRows(List<TableRow> rows, PrintStream out){
}
}
// [END print_rows]


/**
* Polls the job for completion.
* @param request The bigquery request to poll for completion
* @param interval Number of milliseconds between each poll
* @return The finished job
* @throws IOException IOException
* @throws InterruptedException InterruptedException
*/
// [START poll_job]
public static Job pollJob(Bigquery.Jobs.Get request, long interval)
throws IOException, InterruptedException{
public static Job pollJob(final Bigquery.Jobs.Get request, final long
interval)
throws IOException, InterruptedException {
Job job = request.execute();
while(!job.getStatus().getState().equals("DONE")) {
System.out.println("Job is "
+ job.getStatus().getState()
while (!job.getStatus().getState().equals("DONE")) {
System.out.println("Job is "
+ job.getStatus().getState()
+ " waiting " + interval + " milliseconds...");
Thread.sleep(interval);
job = request.execute();
}
return job;
}
// [END poll_job]


/**
* Pages through the results of an arbitrary Bigquery request.
* @param requestTemplate The object that represents the call to fetch
* the results.
* @param <T> The type of the returned objects
* @return An iterator that pages through the returned object
*/
// [START paging]
public static <T extends GenericJson> Iterator<T> getPages(BigqueryRequest<T> request_template){
public static <T extends GenericJson> Iterator<T> getPages(
final BigqueryRequest<T> requestTemplate) {

class PageIterator implements Iterator<T>{
/**
* An iterator class that pages through a Bigquery request.
*/
class PageIterator implements Iterator<T> {

BigqueryRequest<T> request;
boolean hasNext = true;
private BigqueryRequest<T> request;
private boolean hasNext = true;

public PageIterator(BigqueryRequest<T> request_template){
this.request = request_template;
/**
* Inner class that represents our iterator to page through results.
* @param requestTemplate The object that represents the call to fetch
* the results.
*/
public PageIterator(final BigqueryRequest<T> requestTemplate) {
this.request = requestTemplate;
}

/**
* Checks whether there is another page of results.
* @return True if there is another page of results.
*/
public boolean hasNext() {
return hasNext ;
return hasNext;
}


/**
* Returns the next page of results.
* @return The next page of resul.ts
*/
public T next() {
if(!hasNext){
if (!hasNext) {
throw new NoSuchElementException();
}
try {
Expand All @@ -98,38 +145,49 @@ public T next() {
return null;
}
}

/**
* Skips the page by moving the iterator to the next page.
*/
public void remove() {
this.next();
}
}
}

return new PageIterator(request_template);
return new PageIterator(requestTemplate);
}
// [END paging]


/**
* Loads a Bigquery schema.
* @param schemaSource The source of the schema
* @return The TableSchema
*/
// [START load_schema]
public static TableSchema loadSchema(Reader schemaSource){
public static TableSchema loadSchema(final Reader schemaSource) {
TableSchema sourceSchema = new TableSchema();
List<TableFieldSchema> fields = (new Gson()).<List<TableFieldSchema>>fromJson(
schemaSource,

List<TableFieldSchema> fields = (new Gson())
.<List<TableFieldSchema>>fromJson(schemaSource,
(new ArrayList<TableFieldSchema>()).getClass());

sourceSchema.setFields(fields);

return sourceSchema;
}
// [END load_schema]

// [START list_datasets]
/**
* Display all BigQuery datasets associated with a project
* Display all BigQuery datasets associated with a project.
*
* @param bigquery an authorized BigQuery client
* @param projectId a string containing the current project ID
* @throws IOException
* @throws IOException Thrown if there is a network error connecting to
* Bigquery.
*/
public static void listDatasets(Bigquery bigquery, String projectId)
public static void listDatasets(final Bigquery bigquery, final String
projectId)
throws IOException {
Datasets.List datasetRequest = bigquery.datasets().list(projectId);
DatasetList datasetList = datasetRequest.execute();
Expand All @@ -143,5 +201,5 @@ public static void listDatasets(Bigquery bigquery, String projectId)
}
}
// [END list_datasets]

}