diff --git a/bigquery/src/main/java/com/google/cloud/bigquery/samples/AsyncQuerySample.java b/bigquery/src/main/java/com/google/cloud/bigquery/samples/AsyncQuerySample.java index 774f7834ba4..ca62512377a 100644 --- a/bigquery/src/main/java/com/google/cloud/bigquery/samples/AsyncQuerySample.java +++ b/bigquery/src/main/java/com/google/cloud/bigquery/samples/AsyncQuerySample.java @@ -1,14 +1,16 @@ -/* +/** * Copyright (c) 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 + * 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 + * 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. */ @@ -30,16 +32,16 @@ /** * Example of authorizing with BigQuery and reading from a public dataset. */ -public class AsyncQuerySample extends BigqueryUtils{ +public class AsyncQuerySample extends BigqueryUtils { + - // [START main] /** - * @param args - * @throws IOException - * @throws InterruptedException + * @param args Command line args + * @throws IOException IOException + * @throws InterruptedException InterruptedException */ - public static void main(String[] args) + public static void main(final String[] args) throws IOException, InterruptedException { Scanner scanner = new Scanner(System.in); @@ -49,66 +51,79 @@ public static void main(String[] args) String queryString = scanner.nextLine(); System.out.println("Run query in batch mode? [true|false] "); boolean batch = Boolean.valueOf(scanner.nextLine()); - System.out.println("Enter how often to check if your job is complete (milliseconds): "); + System.out.println("Enter how often to check if your job is complete " + + "(milliseconds): "); long waitTime = scanner.nextLong(); scanner.close(); - Iterator pages = run(projectId, queryString, batch, waitTime); - while(pages.hasNext()){ + Iterator pages = run(projectId, queryString, + batch, waitTime); + while (pages.hasNext()) { printRows(pages.next().getRows(), System.out); } } // [END main] - // [START run] - public static Iterator run(String projectId, - String queryString, - boolean batch, - long waitTime) - throws IOException, InterruptedException{ - + + /** + * + * @param projectId Get this from Google Developers console + * @param queryString Query we want to run against BigQuery + * @param batch True if you want to batch the queries + * @param waitTime How long to wait before retries + * @return An interator to the result of your pages + * @throws IOException Thrown if there's an IOException + * @throws InterruptedException Thrown if there's an Interrupted Exception + */ + public static Iterator run(final String projectId, + final String queryString, + final boolean batch, + final long waitTime) + throws IOException, InterruptedException { + Bigquery bigquery = BigqueryServiceFactory.getService(); Job query = asyncQuery(bigquery, projectId, queryString, batch); Bigquery.Jobs.Get getRequest = bigquery.jobs().get( projectId, query.getJobReference().getJobId()); - - //Poll every waitTime milliseconds, + + //Poll every waitTime milliseconds, //retrying at most retries times if there are errors pollJob(getRequest, waitTime); GetQueryResults resultsRequest = bigquery.jobs().getQueryResults( projectId, query.getJobReference().getJobId()); - + return getPages(resultsRequest); } // [END run] - + // [START asyncQuery] /** - * Inserts an asynchronous query Job for a particular query + * Inserts an asynchronous query Job for a particular query. * * @param bigquery an authorized BigQuery client * @param projectId a String containing the project ID * @param querySql the actual query string + * @param batch True if you want to run the query as BATCH * @return a reference to the inserted query job - * @throws IOException + * @throws IOException Thrown if there's a network exception */ - public static Job asyncQuery(Bigquery bigquery, - String projectId, - String querySql, - boolean batch) throws IOException { - - JobConfigurationQuery query_config = new JobConfigurationQuery() + public static Job asyncQuery(final Bigquery bigquery, + final String projectId, + final String querySql, + final boolean batch) throws IOException { + + JobConfigurationQuery queryConfig = new JobConfigurationQuery() .setQuery(querySql); - - if(batch){ - query_config.setPriority("BATCH"); + + if (batch) { + queryConfig.setPriority("BATCH"); } - + Job job = new Job().setConfiguration( - new JobConfiguration().setQuery(query_config)); - + new JobConfiguration().setQuery(queryConfig)); + return bigquery.jobs().insert(projectId, job).execute(); } // [END asyncQuery] diff --git a/bigquery/src/main/java/com/google/cloud/bigquery/samples/BigqueryServiceFactory.java b/bigquery/src/main/java/com/google/cloud/bigquery/samples/BigqueryServiceFactory.java index c69212f84d9..4a657935012 100644 --- a/bigquery/src/main/java/com/google/cloud/bigquery/samples/BigqueryServiceFactory.java +++ b/bigquery/src/main/java/com/google/cloud/bigquery/samples/BigqueryServiceFactory.java @@ -1,14 +1,16 @@ /* * Copyright (c) 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 + * 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 + * 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. */ @@ -25,32 +27,61 @@ import java.io.IOException; import java.util.Collection; -public class BigqueryServiceFactory { +/** + * This class creates our Service to connect to Bigquery including auth. + */ +public final class BigqueryServiceFactory { + + /** + * Private constructor to disable creation of this utility Factory class. + */ + private BigqueryServiceFactory() { + } + + /** + * Singleton service used through the app. + */ private static Bigquery service = null; - private static Object service_lock = new Object(); - public static Bigquery getService() throws IOException{ - if(service==null){ - synchronized(service_lock){ - if(service==null){ - service=createAuthorizedClient(); + /** + * Mutex created to create the singleton in thread-safe fashion. + */ + private static Object serviceLock = new Object(); + + /** + * Threadsafe Factory that provides an authorized Bigquery service. + * @return The Bigquery service + * @throws IOException Thronw if there is an error connecting to Bigquery. + */ + public static Bigquery getService() throws IOException { + if (service == null) { + synchronized (serviceLock) { + if (service == null) { + service = createAuthorizedClient(); } } } return service; } + /** + * Creates an authorized client to Google Bigquery. + * @return The BigQuery Service + * @throws IOException Thrown if there is an error connecting + */ // [START get_service] private static Bigquery createAuthorizedClient() throws IOException { - Collection BIGQUERY_SCOPES = BigqueryScopes.all(); - HttpTransport TRANSPORT = new NetHttpTransport(); - JsonFactory JSON_FACTORY = new JacksonFactory(); - GoogleCredential credential = GoogleCredential.getApplicationDefault(TRANSPORT, JSON_FACTORY); - if(credential.createScopedRequired()){ - credential = credential.createScoped(BIGQUERY_SCOPES); + Collection bigqueryScopes = BigqueryScopes.all(); + HttpTransport transport = new NetHttpTransport(); + JsonFactory jsonFactory = new JacksonFactory(); + GoogleCredential credential = GoogleCredential.getApplicationDefault( + transport, jsonFactory); + if (credential.createScopedRequired()) { + credential = credential.createScoped(bigqueryScopes); } - return new Bigquery.Builder(TRANSPORT, JSON_FACTORY, credential).setApplicationName("BigQuery Samples").build(); + return new Bigquery.Builder(transport, jsonFactory, credential) + .setApplicationName("BigQuery Samples").build(); } // [END get_service] diff --git a/bigquery/src/main/java/com/google/cloud/bigquery/samples/BigqueryUtils.java b/bigquery/src/main/java/com/google/cloud/bigquery/samples/BigqueryUtils.java index a5e926d4fe3..bab223f7c72 100644 --- a/bigquery/src/main/java/com/google/cloud/bigquery/samples/BigqueryUtils.java +++ b/bigquery/src/main/java/com/google/cloud/bigquery/samples/BigqueryUtils.java @@ -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; @@ -35,12 +35,26 @@ import java.util.NoSuchElementException; /** - * TODO: Insert description here. (generated by elibixby) + * Helper functions for the other classes. */ public class BigqueryUtils { - + + /** + * Private contructor to prevent creation of this class, which is just all + * static helper methods. + */ + protected 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 rows, PrintStream out){ + public static void printRows(final List rows, final PrintStream + out) { for (TableRow row : rows) { for (TableCell field : row.getF()) { out.printf("%-50s", field.getV()); @@ -49,14 +63,23 @@ public static void printRows(List 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(); @@ -64,25 +87,49 @@ public static Job pollJob(Bigquery.Jobs.Get request, long interval) 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 The type of the returned objects + * @return An iterator that pages through the returned object + */ // [START paging] - public static Iterator getPages(BigqueryRequest request_template){ + public static Iterator getPages( + final BigqueryRequest requestTemplate) { - class PageIterator implements Iterator{ + /** + * An iterator class that pages through a Bigquery request. + */ + class PageIterator implements Iterator { - BigqueryRequest request; - boolean hasNext = true; + private BigqueryRequest request; + private boolean hasNext = true; - public PageIterator(BigqueryRequest 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 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 { @@ -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 fields = (new Gson()).>fromJson( - schemaSource, + + List fields = (new Gson()) + .>fromJson(schemaSource, (new ArrayList()).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(); @@ -143,5 +201,5 @@ public static void listDatasets(Bigquery bigquery, String projectId) } } // [END list_datasets] - + } diff --git a/bigquery/src/main/java/com/google/cloud/bigquery/samples/ExportDataCloudStorageSample.java b/bigquery/src/main/java/com/google/cloud/bigquery/samples/ExportDataCloudStorageSample.java index 9c21b2d6488..67fee35b185 100644 --- a/bigquery/src/main/java/com/google/cloud/bigquery/samples/ExportDataCloudStorageSample.java +++ b/bigquery/src/main/java/com/google/cloud/bigquery/samples/ExportDataCloudStorageSample.java @@ -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; @@ -24,14 +24,27 @@ import java.util.Scanner; /** - * TODO: Insert description here. (generated by elibixby) + * Sample of how to Export Cloud Data. */ -public class ExportDataCloudStorageSample extends BigqueryUtils { +public class ExportDataCloudStorageSample { + /** + * Protected constructor since this is a collection of static functions. + */ + protected ExportDataCloudStorageSample() { + super(); + } - + /** + * This program can be run to demonstrate running a Bigquery query from the + * CLI. + * @param args Command line args + * @throws IOException If there is an error connceting to bigquery + * @throws InterruptedException Should never be thrown. + */ // [START main] - public static void main(String[] args) throws IOException, InterruptedException{ + public static void main(final String[] args) throws IOException, + InterruptedException { Scanner scanner = new Scanner(System.in); System.out.println("Enter your project id: "); String projectId = scanner.nextLine(); @@ -39,27 +52,39 @@ public static void main(String[] args) throws IOException, InterruptedException{ String datasetId = scanner.nextLine(); System.out.println("Enter your table id: "); String tableId = scanner.nextLine(); - System.out.println("Enter the Google Cloud Storage Path to which you'd like to export: "); + System.out.println("Enter the Google Cloud Storage Path to which you'd " + + "like to export: "); String cloudStoragePath = scanner.nextLine(); - System.out.println("Enter how often to check if your job is complete (milliseconds): "); + System.out.println("Enter how often to check if your job is complete " + + "(milliseconds): "); long interval = scanner.nextLong(); scanner.close(); - + run(cloudStoragePath, projectId, datasetId, tableId, interval); - + } // [END main] - + + /** + * Run the bigquery ClI. + * @param cloudStoragePath The bucket we are using + * @param projectId Project id + * @param datasetId datasetid + * @param tableId tableid + * @param interval interval to wait between polling in milliseconds + * @throws IOException Thrown if there is an error connecting to Bigquery. + * @throws InterruptedException Should never be thrown + */ // [START run] - public static void run( - String cloudStoragePath, - String projectId, - String datasetId, - String tableId, - long interval) throws IOException, InterruptedException{ + public static void run( + final String cloudStoragePath, + final String projectId, + final String datasetId, + final String tableId, + final long interval) throws IOException, InterruptedException { Bigquery bigquery = BigqueryServiceFactory.getService(); - + Job extractJob = extractJob( bigquery, cloudStoragePath, @@ -67,36 +92,44 @@ public static void run( .setProjectId(projectId) .setDatasetId(datasetId) .setTableId(tableId)); - - Bigquery.Jobs.Get get_job = bigquery.jobs().get( - extractJob.getJobReference().getProjectId(), + + Bigquery.Jobs.Get getJob = bigquery.jobs().get( + extractJob.getJobReference().getProjectId(), extractJob.getJobReference().getJobId()); - - pollJob(get_job, interval); - + + BigqueryUtils.pollJob(getJob, interval); + System.out.println("Export is Done!"); - + } // [END run] - - + + + /** + * A job that extracts data from a table. + * @param bigquery Bigquery service to use + * @param cloudStoragePath Cloud storage bucket we are inserting into + * @param table Table to extract from + * @return The job to extract data from the table + * @throws IOException Thrown if error connceting to Bigtable + */ // [START extract_job] public static Job extractJob( - Bigquery bigquery, - String cloudStoragePath, - TableReference table) throws IOException{ - + final Bigquery bigquery, + final String cloudStoragePath, + final TableReference table) throws IOException { + JobConfigurationExtract extract = new JobConfigurationExtract() .setSourceTable(table) .setDestinationUri(cloudStoragePath); - return bigquery.jobs().insert(table.getProjectId(), + return bigquery.jobs().insert(table.getProjectId(), new Job().setConfiguration(new JobConfiguration().setExtract(extract))) .execute(); } // [END extract_job] - - + + } diff --git a/bigquery/src/main/java/com/google/cloud/bigquery/samples/LoadDataCSVSample.java b/bigquery/src/main/java/com/google/cloud/bigquery/samples/LoadDataCSVSample.java index c328bf570ed..922ba82bda6 100644 --- a/bigquery/src/main/java/com/google/cloud/bigquery/samples/LoadDataCSVSample.java +++ b/bigquery/src/main/java/com/google/cloud/bigquery/samples/LoadDataCSVSample.java @@ -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; @@ -29,13 +29,26 @@ import java.util.Scanner; /** - * TODO: Insert description here. (generated by elibixby) + * Cli tool to load data from a CSV into Bigquery. */ -public class LoadDataCSVSample extends BigqueryUtils { +public class LoadDataCSVSample { + + /** + * Protected constructor since this is a collection of static methods. + */ + protected LoadDataCSVSample() { - + } + + /** + * Cli tool to load data from a CSV into Bigquery. + * @param args Command line args, should be empty + * @throws IOException IOException + * @throws InterruptedException InterruptedException + */ // [START main] - public static void main(String[] args) throws IOException, InterruptedException{ + public static void main(final String[] args) throws IOException, + InterruptedException { Scanner scanner = new Scanner(System.in); System.out.println("Enter your project id: "); String projectId = scanner.nextLine(); @@ -43,38 +56,51 @@ public static void main(String[] args) throws IOException, InterruptedException{ String datasetId = scanner.nextLine(); System.out.println("Enter your table id: "); String tableId = scanner.nextLine(); - System.out.println("Enter the Google Cloud Storage Path to the data you'd like to load: "); + System.out.println("Enter the Google Cloud Storage Path to the data " + + "you'd like to load: "); String cloudStoragePath = scanner.nextLine(); System.out.println("Enter the filepath to your schema: "); String sourceSchemaPath = scanner.nextLine(); - - - System.out.println("Enter how often to check if your job is complete (milliseconds): "); + + + System.out.println("Enter how often to check if your job is complete " + + "(milliseconds): "); long interval = scanner.nextLong(); scanner.close(); - + run(cloudStoragePath, projectId, datasetId, tableId, new FileReader(new File(sourceSchemaPath)), interval); - + } // [END main] - + + /** + * Run the bigquery ClI. + * @param cloudStoragePath The bucket we are using + * @param projectId Project id + * @param datasetId datasetid + * @param tableId tableid + * @param schemaSource Source of the schema + * @param interval interval to wait between polling in milliseconds + * @throws IOException Thrown if there is an error connecting to Bigquery. + * @throws InterruptedException Should never be thrown + */ // [START run] - public static void run( - String cloudStoragePath, - String projectId, - String datasetId, - String tableId, - Reader schemaSource, - long interval) throws IOException, InterruptedException{ + public static void run( + final String cloudStoragePath, + final String projectId, + final String datasetId, + final String tableId, + final Reader schemaSource, + final long interval) throws IOException, InterruptedException { Bigquery bigquery = BigqueryServiceFactory.getService(); - - + + Job loadJob = loadJob( bigquery, cloudStoragePath, @@ -82,36 +108,45 @@ public static void run( .setProjectId(projectId) .setDatasetId(datasetId) .setTableId(tableId), - loadSchema(schemaSource)); + BigqueryUtils.loadSchema(schemaSource)); - Bigquery.Jobs.Get get_job = bigquery.jobs().get( - loadJob.getJobReference().getProjectId(), + Bigquery.Jobs.Get getJob = bigquery.jobs().get( + loadJob.getJobReference().getProjectId(), loadJob.getJobReference().getJobId()); - - pollJob(get_job, interval); - + + BigqueryUtils.pollJob(getJob, interval); + System.out.println("Load is Done!"); - + } // [END run] - + + /** + * A job that extracts data from a table. + * @param bigquery Bigquery service to use + * @param cloudStoragePath Cloud storage bucket we are inserting into + * @param table Table to extract from + * @param schema The schema of the table we are loading into + * @return The job to extract data from the table + * @throws IOException Thrown if error connceting to Bigtable + */ // [START load_job] public static Job loadJob( - Bigquery bigquery, - String cloudStoragePath, - TableReference table, - TableSchema schema) throws IOException{ - + final Bigquery bigquery, + final String cloudStoragePath, + final TableReference table, + final TableSchema schema) throws IOException { + JobConfigurationLoad load = new JobConfigurationLoad() .setDestinationTable(table) .setSchema(schema) .setSourceUris(Collections.singletonList(cloudStoragePath)); - return bigquery.jobs().insert(table.getProjectId(), + return bigquery.jobs().insert(table.getProjectId(), new Job().setConfiguration(new JobConfiguration().setLoad(load))) .execute(); } // [END load_job] - + } diff --git a/bigquery/src/main/java/com/google/cloud/bigquery/samples/StreamingSample.java b/bigquery/src/main/java/com/google/cloud/bigquery/samples/StreamingSample.java index d914f5fa5bc..b2d01c76562 100644 --- a/bigquery/src/main/java/com/google/cloud/bigquery/samples/StreamingSample.java +++ b/bigquery/src/main/java/com/google/cloud/bigquery/samples/StreamingSample.java @@ -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; @@ -31,14 +31,25 @@ /** - * TODO: Insert description here. (generated by elibixby) + * Example of Bigquery Streaming. */ -public class StreamingSample extends BigqueryUtils { +public class StreamingSample { + + /** + * Empty constructor since this is just a collection of static methods. + */ + protected StreamingSample() { + + } + - - + /** + * Command line that demonstrates Bigquery streaming. + * @param args Command line args, should be empty + * @throws IOException IOexception + */ // [START main] - public static void main(String[] args) throws IOException{ + public static void main(final String[] args) throws IOException { final Scanner scanner = new Scanner(System.in); System.out.println("Enter your project id: "); String projectId = scanner.nextLine(); @@ -47,54 +58,70 @@ public static void main(String[] args) throws IOException{ System.out.println("Enter your table id: "); String tableId = scanner.nextLine(); scanner.close(); - + System.out.println("Enter JSON to stream to BigQuery: \n" + "Press End-of-stream (CTRL-D) to stop"); - + JsonReader fromCLI = new JsonReader(new InputStreamReader(System.in)); - + Iterator responses = run(projectId, datasetId, tableId, fromCLI); - - while(responses.hasNext()){ + + while (responses.hasNext()) { System.out.println(responses.next()); } - + fromCLI.close(); } // [END main] - - - -// [START run] + + + /** + * Run the bigquery ClI. + * @param projectId Project id + * @param datasetId datasetid + * @param tableId tableid + * @param rows The source of the JSON rows we are streaming in. + * @return Returns Iterates through the stream responses + * @throws IOException Thrown if there is an error connecting to Bigquery. + * @throws InterruptedException Should never be thrown + */ + // [START run] public static Iterator run(final String projectId, - final String datasetId, + final String datasetId, final String tableId, - final JsonReader rows) throws IOException{ - - + final JsonReader rows) throws IOException { + + final Bigquery bigquery = BigqueryServiceFactory.getService(); final Gson gson = new Gson(); rows.beginArray(); - - return new Iterator(){ + return new Iterator() { + + /** + * Get the next row in the stream + * @return True if there is another row in the stream + */ public boolean hasNext() { try { return rows.hasNext(); } catch (IOException e) { - // TODO(elibixby): Auto-generated catch block e.printStackTrace(); } return false; } + /** + * + * @return Next page of data + */ public TableDataInsertAllResponse next() { try { Map rowData = gson.>fromJson( - rows, + rows, (new HashMap()).getClass()); return streamRow(bigquery, projectId, @@ -112,25 +139,35 @@ public TableDataInsertAllResponse next() { public void remove() { this.next(); } - + }; - + } // [END run] - + + /** + * + * @param bigquery The bigquery service + * @param projectId project id from Google Developers console + * @param datasetId id of teh dataset + * @param tableId if the table we're streaming + * @param row Id of the row we're inserting + * @return Response from the insert + * @throws IOException ioexception + */ // [START streamRow] - public static TableDataInsertAllResponse streamRow(Bigquery bigquery, - String projectId, - String datasetId, - String tableId, - TableDataInsertAllRequest.Rows row) throws IOException{ - + public static TableDataInsertAllResponse streamRow(final Bigquery bigquery, + final String projectId, + final String datasetId, + final String tableId, + final TableDataInsertAllRequest.Rows row) throws IOException { + return bigquery.tabledata().insertAll( - projectId, - datasetId, - tableId, - new TableDataInsertAllRequest().setRows(Collections.singletonList(row))).execute(); - + projectId, + datasetId, + tableId, + new TableDataInsertAllRequest().setRows( + Collections.singletonList(row))).execute(); } // [END streamRow] } diff --git a/bigquery/src/main/java/com/google/cloud/bigquery/samples/SyncQuerySample.java b/bigquery/src/main/java/com/google/cloud/bigquery/samples/SyncQuerySample.java index 8d644a2d2f6..d331f8cc2bf 100644 --- a/bigquery/src/main/java/com/google/cloud/bigquery/samples/SyncQuerySample.java +++ b/bigquery/src/main/java/com/google/cloud/bigquery/samples/SyncQuerySample.java @@ -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; @@ -24,58 +24,74 @@ import java.util.Iterator; import java.util.Scanner; /** - * TODO: Insert description here. (generated by elibixby) + * Runs a synchronous query against Bigtable. */ -public class SyncQuerySample extends BigqueryUtils{ +public class SyncQuerySample { + + /** + * Protected because this is a collection of static methods. + */ + protected SyncQuerySample() { + + } //[START main] /** - * @param args - * @throws IOException + * @param args args + * @throws IOException ioexceptino */ - public static void main(String[] args) - throws IOException{ - - + public static void main(final String[] args) + throws IOException { Scanner scanner = new Scanner(System.in); System.out.println("Enter your project id: "); String projectId = scanner.nextLine(); System.out.println("Enter your query string: "); String queryString = scanner.nextLine(); - System.out.println("Enter how long to wait for the query to complete (in milliseconds):\n " + - "(if longer than 10 seconds, use an asynchronous query)"); + System.out.println("Enter how long to wait for the query to complete" + + " (in milliseconds):\n " + + "(if longer than 10 seconds, use an asynchronous query)"); long waitTime = scanner.nextLong(); scanner.close(); - Iterator pages = run(projectId, queryString, waitTime); - while(pages.hasNext()){ - printRows(pages.next().getRows(), System.out); + Iterator pages = run(projectId, queryString, + waitTime); + while (pages.hasNext()) { + BigqueryUtils.printRows(pages.next().getRows(), System.out); } } // [END main] - + + /** + * + * @param projectId project id from developer console + * @param queryString query to run + * @param waitTime Timeout in milliseconds before we abort + * @return Iterator that pages through the results of the query + * @throws IOException ioexception + */ // [START run] - public static Iterator run(String projectId, - String queryString, - long waitTime) throws IOException{ + public static Iterator run(final String projectId, + final String queryString, + final long waitTime) throws IOException { Bigquery bigquery = BigqueryServiceFactory.getService(); //Wait until query is done with 10 second timeout, at most 5 retries on error QueryResponse query = bigquery.jobs().query( projectId, - new QueryRequest().setTimeoutMs(waitTime).setQuery(queryString)).execute(); - - //Make a request to get the results of the query + new QueryRequest().setTimeoutMs(waitTime).setQuery(queryString)) + .execute(); + + //Make a request to get the results of the query //(timeout is zero since job should be complete) - + GetQueryResults getRequest = bigquery.jobs().getQueryResults( query.getJobReference().getProjectId(), query.getJobReference().getJobId()); - - - return getPages(getRequest); + + + return BigqueryUtils.getPages(getRequest); } // [END run] - + } diff --git a/checkstyle-checker.xml b/checkstyle-checker.xml new file mode 100644 index 00000000000..efb5b85c951 --- /dev/null +++ b/checkstyle-checker.xml @@ -0,0 +1,201 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/cloud-storage/xml-api/README.md b/cloud-storage/xml-api/README.md new file mode 100644 index 00000000000..8b74056c7c7 --- /dev/null +++ b/cloud-storage/xml-api/README.md @@ -0,0 +1,8 @@ +java-docs-samples/cloud-storage XML API Examples +=================================== + +Samples used in Google Cloud Storage documentation (https://developers.google.com/storage/docs/xml-api-java-samples). + +- **cmdline-sample** - Uses a [Application Default Credentials](https://developers.google.com/identity/protocols/application-default-credentials) to access a specified bucket. + +- **serviceaccount-appengine-sample** - Uses Google App Engine credentials to access a specified bucket. You must add the App Engine Service Account Name to the Permissions of the project that contains the bucket. diff --git a/cloud-storage/xml-api/cmdline-sample/README.md b/cloud-storage/xml-api/cmdline-sample/README.md new file mode 100644 index 00000000000..508a9660238 --- /dev/null +++ b/cloud-storage/xml-api/cmdline-sample/README.md @@ -0,0 +1,72 @@ +This is the sample used in the [Cloud Storage Java documentation](https://cloud.google.com/storage/docs/xml-api-java-samples). + +Using the Command Line Sample +============================================================== + +Browse Online +-------------- + +The main file is [StorageSample.java](https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/cloud-storage/xml-api/cmdline-sample/src/main/java/StorageSample.java). + + +Setup +----- + +* [Create](https://cloud.google.com/storage/docs/cloud-console#_creatingbuckets) a Google Cloud Storage bucket +* This module uses [Application Default Credentials](https://developers.google.com/accounts/docs/application-default-credentials). If you are running it outside of [Google Compute Engine](https://cloud.google.com/compute/), you'll need to + * Download the json private key for a [Service Account](https://cloud.google.com/storage/docs/authentication#service_accounts) and have it available. + * Set an environment variable: `export GOOGLE_APPLICATION_CREDENTIALS=path/to/your-key.json` +* You must also be able to work with [GitHub](https://help.github.com/articles/set-up-git) repositories. +* Clone repository. + + git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git + + +Command-line Instructions +------------------------- + +* **Prerequisites:** + * Install the latest version of [Java](http//java.com) and [Maven](http://maven.apache.org/download.html). + * Set the environment variable: `export GOOGLE_APPLICATION_CREDENTIALS=your-key-filename.json` + * You may need to set your `JAVA_HOME`. + +```bash +cd java-docs-samples/cloud-storage/xml-api/cmdline-sample +# Compile and run +mvn compile install +mvn -q exec:java -Dexec.args="your-bucket-name" +``` + +To enable logging of HTTP requests and responses (highly recommended when +developing), please take a look at logging.properties. + + +Eclipse Instructions +-------------------- + +* **Prerequisites:** + * Install [Eclipse](http://www.eclipse.org/downloads/), the [Maven plugin](http://eclipse.org/m2e/), and optionally the [GitHub plugin](http://eclipse.github.com/). + +* Set up Eclipse Preferences + + * Window > Preferences... (or on Mac, Eclipse > Preferences...) + * Select Maven + + * check on "Download Artifact Sources" + * check on "Download Artifact JavaDoc" + +* Create a new project using `cloud-storage/xml-api/cmdline-sample` + + * Create a new Java Project. + * Choose the **Location** of the project to be the location of `cmdline-sample` + * Select the project and **Convert to Maven Project** to add Maven Dependencies. + * Click on Run > Run configurations + * Navigate to your **Java Application**'s configuration section + * In the **Arguments** tab, add the name of the bucket you created above as a **Program argument** + * In the **Environment** tab, create a variable `GOOGLE_APPLICATION_CREDENTIALS` and set it to the path to your json private key file. + +* Run + + * Right-click on project + * Run As > Java Application + * If asked, type "StorageSample" and click OK diff --git a/cloud-storage/xml-api/cmdline-sample/logging.properties b/cloud-storage/xml-api/cmdline-sample/logging.properties new file mode 100644 index 00000000000..faec34876e0 --- /dev/null +++ b/cloud-storage/xml-api/cmdline-sample/logging.properties @@ -0,0 +1,10 @@ +# Properties file which configures the operation of the JDK logging facility. +# The system will look for this config file to be specified as a system property: +# -Djava.util.logging.config.file=${project_loc:cmdline-sample}/logging.properties + +# Set up the console handler (uncomment "level" to show more fine-grained messages) +handlers = java.util.logging.ConsoleHandler +#java.util.logging.ConsoleHandler.level = CONFIG + +# Set up logging of HTTP requests and responses (uncomment "level" to show) +#com.google.api.client.http.level = CONFIG diff --git a/cloud-storage/xml-api/cmdline-sample/pom.xml b/cloud-storage/xml-api/cmdline-sample/pom.xml new file mode 100644 index 00000000000..da2a28d8e10 --- /dev/null +++ b/cloud-storage/xml-api/cmdline-sample/pom.xml @@ -0,0 +1,58 @@ + + + + + doc-samples + com.google.cloud + 1.0.0 + ../../.. + + + + 4.0.0 + com.google.apis-samples + storage-cmd-line-sample + 1 + Example for the Google Cloud Storage JSON API using OAuth 2.0. + + + + org.codehaus.mojo + exec-maven-plugin + 1.1 + + + + java + + + + + StorageSample + + + + ${project.artifactId}-${project.version} + + + + com.google.apis + google-api-services-storage + v1-rev35-1.20.0 + + + com.google.http-client + google-http-client-jackson2 + ${project.http.version} + + + com.google.guava + guava + 18.0 + + + + 1.20.0 + UTF-8 + + diff --git a/cloud-storage/xml-api/cmdline-sample/src/main/java/StorageSample.java b/cloud-storage/xml-api/cmdline-sample/src/main/java/StorageSample.java new file mode 100644 index 00000000000..2fbb86dc468 --- /dev/null +++ b/cloud-storage/xml-api/cmdline-sample/src/main/java/StorageSample.java @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2014 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. + */ + +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.http.GenericUrl; +import com.google.api.client.http.HttpRequest; +import com.google.api.client.http.HttpRequestFactory; +import com.google.api.client.http.HttpResponse; +import com.google.api.client.http.HttpTransport; +import com.google.api.client.util.Preconditions; + +import java.io.IOException; +import java.io.StringReader; +import java.io.StringWriter; +import java.util.Collections; +import java.net.URLEncoder; + +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Source; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.stream.StreamSource; + +/** + * Sample code used in the Cloud Storage Java documentation. + * https://cloud.google.com/storage/docs/xml-api-java-samples + */ +public final class StorageSample { + + /** This class is never instantiated. */ + private StorageSample() { } + + /** Global configuration of Google Cloud Storage OAuth 2.0 scope. */ + private static final String STORAGE_SCOPE = + "https://www.googleapis.com/auth/devstorage.read_write"; + + /** Global instance of the HTTP transport. */ + private static HttpTransport httpTransport; + + /** + * A command-line handler to display the bucket passed in as an argument. + * + * @param args the array of command-line arguments. + */ + public static void main(final String[] args) { + try { + httpTransport = GoogleNetHttpTransport.newTrustedTransport(); + // Check for valid setup. + Preconditions.checkArgument(args.length == 1, + "Please pass in the Google Cloud Storage bucket name to display"); + String bucketName = args[0]; + + //[START snippet] + // Build an account credential. + GoogleCredential credential = GoogleCredential.getApplicationDefault() + .createScoped(Collections.singleton(STORAGE_SCOPE)); + + // Set up and execute a Google Cloud Storage request. + String uri = "https://storage.googleapis.com/" + + URLEncoder.encode(bucketName, "UTF-8"); + HttpRequestFactory requestFactory = httpTransport.createRequestFactory( + credential); + GenericUrl url = new GenericUrl(uri); + HttpRequest request = requestFactory.buildGetRequest(url); + HttpResponse response = request.execute(); + String content = response.parseAsString(); + //[END snippet] + + // Instantiate transformer input. + Source xmlInput = new StreamSource(new StringReader(content)); + StreamResult xmlOutput = new StreamResult(new StringWriter()); + + // Configure transformer. + Transformer transformer = TransformerFactory.newInstance() + .newTransformer(); // An identity transformer + transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "testing.dtd"); + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); + transformer.setOutputProperty( + "{http://xml.apache.org/xslt}indent-amount", "2"); + transformer.transform(xmlInput, xmlOutput); + + // Pretty print the output XML. + System.out.println("\nBucket listing for " + bucketName + ":\n"); + System.out.println(xmlOutput.getWriter().toString()); + System.exit(0); + + } catch (IOException e) { + System.err.println(e.getMessage()); + } catch (Throwable t) { + t.printStackTrace(); + } + System.exit(1); + } +} diff --git a/cloud-storage/xml-api/serviceaccount-appengine-sample/README.md b/cloud-storage/xml-api/serviceaccount-appengine-sample/README.md new file mode 100644 index 00000000000..d27cd28ca29 --- /dev/null +++ b/cloud-storage/xml-api/serviceaccount-appengine-sample/README.md @@ -0,0 +1,54 @@ +Using the Service Account App Engine Sample +============================================== + +Browse Online +------------- + +The main code file is [StorageSample.java](https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/cloud-storage/xml-api/serviceaccount-appengine-sample/src/main/java/StorageServiceAccountSample.java). + +Add Your App Engine Service Account Name to the Project Team +------------------------------------------------------------ + +See the instructions at https://developers.google.com/storage/docs/xml-api-java-samples +for getting your App Engine Service Account Name and adding it to your project team. + +Checkout Instructions +--------------------- + +**Prerequisites:** install the latest version of [Java](http//java.com) and [Maven](http://maven.apache.org/download.html). You may need to set your `JAVA_HOME`. + +You must also be able to work with a GitHub repository (see e.g., +https://help.github.com/articles/set-up-git). + + cd [someDirectory] + git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git + cd java-docs-samples/cloud-storage/xml-api/serviceaccount-appengine-sample + mvn clean package + +To enable logging of HTTP requests and responses (highly recommended when +developing), please take a look at logging.properties. + +Running and Deploying Your Application from the Command Line +------------------------------------------------------------ + +To run your application locally on a development server: + + mvn appengine:devserver + +To deploy your application to appspot.com: + +If this is the first time you are deploying your application to appspot.com, you will to perform the following steps first. + +- Go to https://appengine.google.com and create an application. +- Edit src/main/webapp/WEB-INF/appengine-web.xml, and enter the unique application identifier (you chose it in the prior step) between the tags. + +If you've done the above, you can deploy at any time: + + mvn appengine:update + +If this is the first time you have run "update" on the project, a browser window will open prompting you to log in. Log in with the same Google account the app is registered with. + +Set Up a Project in Eclipse +--------------------------- + +...coming soon... diff --git a/cloud-storage/xml-api/serviceaccount-appengine-sample/pom.xml b/cloud-storage/xml-api/serviceaccount-appengine-sample/pom.xml new file mode 100644 index 00000000000..fd166e17b71 --- /dev/null +++ b/cloud-storage/xml-api/serviceaccount-appengine-sample/pom.xml @@ -0,0 +1,113 @@ + + 4.0.0 + + + doc-samples + com.google.cloud + 1.0.0 + ../../.. + + + com.google.apis-samples + serviceaccounts-appengine-sample + 1.0.0 + Example for Google Cloud Storage using OAuth 2.0 Service Accounts on Google App Engine + war + + + 1.18.0-rc + 1.8.3 + ${project.build.directory}/${project.build.finalName} + + UTF-8 + + + + war + ${webappDirectory}/WEB-INF/classes + + + + maven-compiler-plugin + 2.3.2 + + 1.6 + 1.6 + + + + + org.apache.maven.plugins + maven-war-plugin + 2.1.1 + + + compile + + exploded + + + + + ${webappDirectory} + + + + org.codehaus.mojo + findbugs-maven-plugin + 2.3.2 + + false + + + + + check + + + + + + + + com.google.appengine + appengine-maven-plugin + ${project.appengine.version} + + 8888 + + + + + + maven-release-plugin + + gae:deploy + + + + + + + + + com.google.appengine + appengine-api-1.0-sdk + ${project.appengine.version} + + + + com.google.api-client + google-api-client-appengine + ${google-api-client.version} + + + + diff --git a/cloud-storage/xml-api/serviceaccount-appengine-sample/src/main/java/com/google/api/client/sample/storage/appengine/serviceaccount/StorageSample.java b/cloud-storage/xml-api/serviceaccount-appengine-sample/src/main/java/com/google/api/client/sample/storage/appengine/serviceaccount/StorageSample.java new file mode 100644 index 00000000000..78604c3f9bd --- /dev/null +++ b/cloud-storage/xml-api/serviceaccount-appengine-sample/src/main/java/com/google/api/client/sample/storage/appengine/serviceaccount/StorageSample.java @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2012 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.api.client.sample.storage.appengine.serviceaccount; + +import com.google.api.client.googleapis.extensions.appengine.auth.oauth2.AppIdentityCredential; // SUPPRESS CHECKSTYLE LineLength +import com.google.api.client.http.GenericUrl; +import com.google.api.client.http.HttpRequest; +import com.google.api.client.http.HttpRequestFactory; +import com.google.api.client.http.HttpResponse; +import com.google.api.client.http.HttpTransport; +import com.google.api.client.http.javanet.NetHttpTransport; + +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.util.Arrays; + +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * Google Cloud Storage Service Account App Engine sample. + * + * @author Marc Cohen + */ +public class StorageSample extends HttpServlet { + + /** HTTP status code for a resource that wasn't found. */ + private static final int HTTP_NOT_FOUND = 404; + /** HTTP status code for a resource that was found. */ + private static final int HTTP_OK = 200; + + /** The base endpoint for Google Cloud Storage api calls. */ + private static final String GCS_URI = + "http://commondatastorage.googleapis.com"; + + /** Global configuration of Google Cloud Storage OAuth 2.0 scope. */ + private static final String STORAGE_SCOPE = + "https://www.googleapis.com/auth/devstorage.read_write"; + + /** Global instance of the HTTP transport. */ + private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport(); + + /** Global instance of HTML reference to XSL style sheet. */ + private static final String XSL = + "\n\n"; + + @Override + protected void doGet( + final HttpServletRequest req, final HttpServletResponse resp) + throws IOException { + + try { + AppIdentityCredential credential = new AppIdentityCredential( + Arrays.asList(STORAGE_SCOPE)); + + // Set up and execute Google Cloud Storage request. + String bucketName = req.getRequestURI(); + if (bucketName.equals("/")) { + resp.sendError( + HTTP_NOT_FOUND, + "No bucket specified - append /bucket-name to the URL and retry."); + return; + } + // Remove any trailing slashes, if found. + //[START snippet] + String cleanBucketName = bucketName.replaceAll("/$", ""); + String uri = GCS_URI + cleanBucketName; + HttpRequestFactory requestFactory = + HTTP_TRANSPORT.createRequestFactory(credential); + GenericUrl url = new GenericUrl(uri); + HttpRequest request = requestFactory.buildGetRequest(url); + HttpResponse response = request.execute(); + String content = response.parseAsString(); + //[END snippet] + + // Display the output XML. + resp.setContentType("text/xml"); + BufferedWriter writer = new BufferedWriter( + new OutputStreamWriter(resp.getOutputStream())); + String formattedContent = content.replaceAll( + "( + + bucket-list-java + 1 + true + + + + + + + + diff --git a/cloud-storage/xml-api/serviceaccount-appengine-sample/src/main/webapp/WEB-INF/logging.properties b/cloud-storage/xml-api/serviceaccount-appengine-sample/src/main/webapp/WEB-INF/logging.properties new file mode 100644 index 00000000000..519738e8a0d --- /dev/null +++ b/cloud-storage/xml-api/serviceaccount-appengine-sample/src/main/webapp/WEB-INF/logging.properties @@ -0,0 +1,17 @@ +# A default java.util.logging configuration. +# (All App Engine logging is through java.util.logging by default). +# +# To use this configuration, copy it into your application's WEB-INF +# folder and add the following to your appengine-web.xml: +# +# +# +# +# + +# Set the default logging level for all loggers to WARNING +.level = WARNING + +# Set the logging level for the Google APIs Java Client +# Uncomment this to debug the Google API Client Library for Java +#com.google.api.client.level = CONFIG diff --git a/cloud-storage/xml-api/serviceaccount-appengine-sample/src/main/webapp/WEB-INF/web.xml b/cloud-storage/xml-api/serviceaccount-appengine-sample/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 00000000000..dea3c4ca7ec --- /dev/null +++ b/cloud-storage/xml-api/serviceaccount-appengine-sample/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,23 @@ + + + + + app + com.google.api.client.sample.storage.appengine.serviceaccount.StorageSample + + + + app + / + + + + xsl + text/xsl + + + diff --git a/cloud-storage/xml-api/serviceaccount-appengine-sample/src/main/webapp/xsl/listing.xsl b/cloud-storage/xml-api/serviceaccount-appengine-sample/src/main/webapp/xsl/listing.xsl new file mode 100644 index 00000000000..ba12ba4f721 --- /dev/null +++ b/cloud-storage/xml-api/serviceaccount-appengine-sample/src/main/webapp/xsl/listing.xsl @@ -0,0 +1,32 @@ + + + + + + +

Google Cloud Storage Content Listing for Bucket +

+ + + + + + + + + + + + + + + + + +
Object NameModification TimeETagSizeStorage Class
+ + +
+
diff --git a/pom.xml b/pom.xml index f55c41f4af7..e9b6e20e981 100644 --- a/pom.xml +++ b/pom.xml @@ -22,8 +22,32 @@ taskqueue/deferred unittests bigquery + cloud-storage/xml-api/cmdline-sample + cloud-storage/xml-api/serviceaccount-appengine-sample + + + + org.apache.maven.plugins + maven-checkstyle-plugin + 2.15 + + checkstyle-checker.xml + true + true + + + + + check + + + + + + + diff --git a/taskqueue/deferred/pom.xml b/taskqueue/deferred/pom.xml index d985640314a..42bf08f3551 100644 --- a/taskqueue/deferred/pom.xml +++ b/taskqueue/deferred/pom.xml @@ -20,11 +20,7 @@ 1 UTF-8 - - - 3.1.0 - - + diff --git a/taskqueue/deferred/src/main/java/com/google/cloud/taskqueue/samples/DeferSampleServlet.java b/taskqueue/deferred/src/main/java/com/google/cloud/taskqueue/samples/DeferSampleServlet.java index a70a059ea63..d96690baa66 100644 --- a/taskqueue/deferred/src/main/java/com/google/cloud/taskqueue/samples/DeferSampleServlet.java +++ b/taskqueue/deferred/src/main/java/com/google/cloud/taskqueue/samples/DeferSampleServlet.java @@ -14,6 +14,9 @@ * limitations under the License. */ +/** + * This package demonstrates how to use the task queue with Java. + */ package com.google.cloud.taskqueue.samples; import javax.servlet.http.HttpServlet; @@ -33,7 +36,15 @@ */ public class DeferSampleServlet extends HttpServlet { + /** + * Number of ms long we will arbitrarily delay. + */ + static final int DELAY_MS = 5000; + //[START defer] + /** + * A hypothetical expensive operation we want to defer on a background task. + */ public static class ExpensiveOperation implements DeferredTask { @Override public void run() { @@ -42,15 +53,20 @@ public void run() { } } + /** + * Basic demonstration of adding a deferred task. + * @param request servlet request + * @param resp servlet response + */ @Override - public void doGet(HttpServletRequest request, HttpServletResponse - resp) throws IOException { + public void doGet(final HttpServletRequest request, + final HttpServletResponse resp) throws IOException { // Add the task to the default queue. Queue queue = QueueFactory.getDefaultQueue(); // Wait 5 seconds to run for demonstration purposes queue.add(TaskOptions.Builder.withPayload(new ExpensiveOperation()) - .etaMillis(System.currentTimeMillis() + 5000)); + .etaMillis(System.currentTimeMillis() + DELAY_MS)); resp.setContentType("text/plain"); resp.getWriter().println("Task is backgrounded on queue!");