diff --git a/bigquery/pom.xml b/bigquery/pom.xml
new file mode 100644
index 00000000000..2ba21ae348b
--- /dev/null
+++ b/bigquery/pom.xml
@@ -0,0 +1,81 @@
+
+ 4.0.0
+ com.google.cloud.bigquery.samples
+ bigquery-samples
+ jar
+
+
+ doc-samples
+ com.google.cloud
+ 1.0.0
+ ..
+
+
+
+
+
+ googleapis
+ https://google-api-client-libraries.appspot.com/mavenrepo
+
+
+
+
+
+ com.google.apis
+ google-api-services-bigquery
+ v2-rev158-1.19.0
+
+
+ com.google.oauth-client
+ google-oauth-client
+ ${project.oauth.version}
+
+
+ com.google.http-client
+ google-http-client-jackson2
+ ${project.http.version}
+
+
+ com.google.oauth-client
+ google-oauth-client-jetty
+ ${project.oauth.version}
+
+
+ junit
+ junit
+
+
+ com.google.code.gson
+ gson
+ 2.3.1
+
+
+
+
+ 1.19.0
+ 1.19.0
+ UTF-8
+
+
+
+ src/main/java
+
+
+ src/main/resources
+
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ 3.2
+
+ 5
+ 5
+
+
+
+
+
+
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
new file mode 100644
index 00000000000..774f7834ba4
--- /dev/null
+++ b/bigquery/src/main/java/com/google/cloud/bigquery/samples/AsyncQuerySample.java
@@ -0,0 +1,116 @@
+/*
+ * 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
+ *
+ * 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;
+
+
+import com.google.api.services.bigquery.Bigquery;
+import com.google.api.services.bigquery.Bigquery.Jobs.GetQueryResults;
+import com.google.api.services.bigquery.model.GetQueryResultsResponse;
+import com.google.api.services.bigquery.model.Job;
+import com.google.api.services.bigquery.model.JobConfiguration;
+import com.google.api.services.bigquery.model.JobConfigurationQuery;
+
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.Scanner;
+
+
+/**
+ * Example of authorizing with BigQuery and reading from a public dataset.
+ */
+public class AsyncQuerySample extends BigqueryUtils{
+
+
+ // [START main]
+ /**
+ * @param args
+ * @throws IOException
+ * @throws InterruptedException
+ */
+ public static void main(String[] args)
+ throws IOException, InterruptedException {
+
+ 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("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): ");
+ long waitTime = scanner.nextLong();
+ scanner.close();
+ 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{
+
+ 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,
+ //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
+ *
+ * @param bigquery an authorized BigQuery client
+ * @param projectId a String containing the project ID
+ * @param querySql the actual query string
+ * @return a reference to the inserted query job
+ * @throws IOException
+ */
+ public static Job asyncQuery(Bigquery bigquery,
+ String projectId,
+ String querySql,
+ boolean batch) throws IOException {
+
+ JobConfigurationQuery query_config = new JobConfigurationQuery()
+ .setQuery(querySql);
+
+ if(batch){
+ query_config.setPriority("BATCH");
+ }
+
+ Job job = new Job().setConfiguration(
+ new JobConfiguration().setQuery(query_config));
+
+ 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
new file mode 100644
index 00000000000..c69212f84d9
--- /dev/null
+++ b/bigquery/src/main/java/com/google/cloud/bigquery/samples/BigqueryServiceFactory.java
@@ -0,0 +1,57 @@
+/*
+ * 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
+ *
+ * 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;
+
+import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
+import com.google.api.client.http.HttpTransport;
+import com.google.api.client.http.javanet.NetHttpTransport;
+import com.google.api.client.json.JsonFactory;
+import com.google.api.client.json.jackson2.JacksonFactory;
+import com.google.api.services.bigquery.Bigquery;
+import com.google.api.services.bigquery.BigqueryScopes;
+
+import java.io.IOException;
+import java.util.Collection;
+
+public class BigqueryServiceFactory {
+
+ 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();
+ }
+ }
+ }
+ return service;
+ }
+
+ // [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);
+ }
+ return new Bigquery.Builder(TRANSPORT, JSON_FACTORY, 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
new file mode 100644
index 00000000000..a5e926d4fe3
--- /dev/null
+++ b/bigquery/src/main/java/com/google/cloud/bigquery/samples/BigqueryUtils.java
@@ -0,0 +1,147 @@
+/*
+ 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;
+
+import com.google.api.client.json.GenericJson;
+import com.google.api.services.bigquery.Bigquery;
+import com.google.api.services.bigquery.Bigquery.Datasets;
+import com.google.api.services.bigquery.BigqueryRequest;
+import com.google.api.services.bigquery.model.DatasetList;
+import com.google.api.services.bigquery.model.Job;
+import com.google.api.services.bigquery.model.TableCell;
+import com.google.api.services.bigquery.model.TableFieldSchema;
+import com.google.api.services.bigquery.model.TableRow;
+import com.google.api.services.bigquery.model.TableSchema;
+import com.google.gson.Gson;
+
+import java.io.IOException;
+import java.io.PrintStream;
+import java.io.Reader;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+
+/**
+ * TODO: Insert description here. (generated by elibixby)
+ */
+public class BigqueryUtils {
+
+ // [START print_rows]
+ public static void printRows(List rows, PrintStream out){
+ for (TableRow row : rows) {
+ for (TableCell field : row.getF()) {
+ out.printf("%-50s", field.getV());
+ }
+ out.println();
+ }
+ }
+ // [END print_rows]
+
+ // [START poll_job]
+ public static Job pollJob(Bigquery.Jobs.Get request, long interval)
+ throws IOException, InterruptedException{
+ Job job = request.execute();
+ 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]
+
+ // [START paging]
+ public static Iterator getPages(BigqueryRequest request_template){
+
+ class PageIterator implements Iterator{
+
+ BigqueryRequest request;
+ boolean hasNext = true;
+
+ public PageIterator(BigqueryRequest request_template){
+ this.request = request_template;
+ }
+
+ public boolean hasNext() {
+ return hasNext ;
+ }
+
+ public T next() {
+ if(!hasNext){
+ throw new NoSuchElementException();
+ }
+ try {
+ T response = request.execute();
+ if (response.containsKey("pageToken")) {
+ request = request.set("pageToken", response.get("pageToken"));
+ } else {
+ hasNext = false;
+ }
+ return response;
+ } catch (IOException e) {
+ e.printStackTrace();
+ return null;
+ }
+ }
+ public void remove() {
+ this.next();
+ }
+ }
+
+ return new PageIterator(request_template);
+ }
+ // [END paging]
+
+ // [START load_schema]
+ public static TableSchema loadSchema(Reader schemaSource){
+ TableSchema sourceSchema = new TableSchema();
+
+ 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
+ *
+ * @param bigquery an authorized BigQuery client
+ * @param projectId a string containing the current project ID
+ * @throws IOException
+ */
+ public static void listDatasets(Bigquery bigquery, String projectId)
+ throws IOException {
+ Datasets.List datasetRequest = bigquery.datasets().list(projectId);
+ DatasetList datasetList = datasetRequest.execute();
+ if (datasetList.getDatasets() != null) {
+ List datasets = datasetList.getDatasets();
+ System.out.println("Available datasets\n----------------");
+ System.out.println(datasets.toString());
+ for (DatasetList.Datasets dataset : datasets) {
+ System.out.format("%s\n", dataset.getDatasetReference().getDatasetId());
+ }
+ }
+ }
+ // [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
new file mode 100644
index 00000000000..9c21b2d6488
--- /dev/null
+++ b/bigquery/src/main/java/com/google/cloud/bigquery/samples/ExportDataCloudStorageSample.java
@@ -0,0 +1,102 @@
+/*
+ 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;
+
+import com.google.api.services.bigquery.Bigquery;
+import com.google.api.services.bigquery.model.Job;
+import com.google.api.services.bigquery.model.JobConfiguration;
+import com.google.api.services.bigquery.model.JobConfigurationExtract;
+import com.google.api.services.bigquery.model.TableReference;
+
+import java.io.IOException;
+import java.util.Scanner;
+
+/**
+ * TODO: Insert description here. (generated by elibixby)
+ */
+public class ExportDataCloudStorageSample extends BigqueryUtils {
+
+
+
+ // [START main]
+ public static void main(String[] args) throws IOException, InterruptedException{
+ Scanner scanner = new Scanner(System.in);
+ System.out.println("Enter your project id: ");
+ String projectId = scanner.nextLine();
+ System.out.println("Enter your dataset id: ");
+ 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: ");
+ String cloudStoragePath = scanner.nextLine();
+ 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]
+
+ // [START run]
+ public static void run(
+ String cloudStoragePath,
+ String projectId,
+ String datasetId,
+ String tableId,
+ long interval) throws IOException, InterruptedException{
+
+ Bigquery bigquery = BigqueryServiceFactory.getService();
+
+ Job extractJob = extractJob(
+ bigquery,
+ cloudStoragePath,
+ new TableReference()
+ .setProjectId(projectId)
+ .setDatasetId(datasetId)
+ .setTableId(tableId));
+
+ Bigquery.Jobs.Get get_job = bigquery.jobs().get(
+ extractJob.getJobReference().getProjectId(),
+ extractJob.getJobReference().getJobId());
+
+ pollJob(get_job, interval);
+
+ System.out.println("Export is Done!");
+
+ }
+ // [END run]
+
+
+ // [START extract_job]
+ public static Job extractJob(
+ Bigquery bigquery,
+ String cloudStoragePath,
+ TableReference table) throws IOException{
+
+ JobConfigurationExtract extract = new JobConfigurationExtract()
+ .setSourceTable(table)
+ .setDestinationUri(cloudStoragePath);
+
+ 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
new file mode 100644
index 00000000000..c328bf570ed
--- /dev/null
+++ b/bigquery/src/main/java/com/google/cloud/bigquery/samples/LoadDataCSVSample.java
@@ -0,0 +1,117 @@
+/*
+ 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;
+
+import com.google.api.services.bigquery.Bigquery;
+import com.google.api.services.bigquery.model.Job;
+import com.google.api.services.bigquery.model.JobConfiguration;
+import com.google.api.services.bigquery.model.JobConfigurationLoad;
+import com.google.api.services.bigquery.model.TableReference;
+import com.google.api.services.bigquery.model.TableSchema;
+
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.Reader;
+import java.util.Collections;
+import java.util.Scanner;
+
+/**
+ * TODO: Insert description here. (generated by elibixby)
+ */
+public class LoadDataCSVSample extends BigqueryUtils {
+
+
+ // [START main]
+ public static void main(String[] args) throws IOException, InterruptedException{
+ Scanner scanner = new Scanner(System.in);
+ System.out.println("Enter your project id: ");
+ String projectId = scanner.nextLine();
+ System.out.println("Enter your dataset id: ");
+ 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: ");
+ 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): ");
+ long interval = scanner.nextLong();
+ scanner.close();
+
+ run(cloudStoragePath,
+ projectId,
+ datasetId,
+ tableId,
+ new FileReader(new File(sourceSchemaPath)),
+ interval);
+
+ }
+ // [END main]
+
+ // [START run]
+ public static void run(
+ String cloudStoragePath,
+ String projectId,
+ String datasetId,
+ String tableId,
+ Reader schemaSource,
+ long interval) throws IOException, InterruptedException{
+
+ Bigquery bigquery = BigqueryServiceFactory.getService();
+
+
+ Job loadJob = loadJob(
+ bigquery,
+ cloudStoragePath,
+ new TableReference()
+ .setProjectId(projectId)
+ .setDatasetId(datasetId)
+ .setTableId(tableId),
+ loadSchema(schemaSource));
+
+ Bigquery.Jobs.Get get_job = bigquery.jobs().get(
+ loadJob.getJobReference().getProjectId(),
+ loadJob.getJobReference().getJobId());
+
+ pollJob(get_job, interval);
+
+ System.out.println("Load is Done!");
+
+ }
+ // [END run]
+
+ // [START load_job]
+ public static Job loadJob(
+ Bigquery bigquery,
+ String cloudStoragePath,
+ TableReference table,
+ TableSchema schema) throws IOException{
+
+ JobConfigurationLoad load = new JobConfigurationLoad()
+ .setDestinationTable(table)
+ .setSchema(schema)
+ .setSourceUris(Collections.singletonList(cloudStoragePath));
+
+ 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
new file mode 100644
index 00000000000..d914f5fa5bc
--- /dev/null
+++ b/bigquery/src/main/java/com/google/cloud/bigquery/samples/StreamingSample.java
@@ -0,0 +1,136 @@
+/*
+ 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;
+
+import com.google.api.services.bigquery.Bigquery;
+import com.google.api.services.bigquery.model.TableDataInsertAllRequest;
+import com.google.api.services.bigquery.model.TableDataInsertAllResponse;
+import com.google.gson.Gson;
+import com.google.gson.JsonSyntaxException;
+import com.google.gson.stream.JsonReader;
+
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Scanner;
+
+
+/**
+ * TODO: Insert description here. (generated by elibixby)
+ */
+public class StreamingSample extends BigqueryUtils {
+
+
+
+ // [START main]
+ public static void main(String[] args) throws IOException{
+ final Scanner scanner = new Scanner(System.in);
+ System.out.println("Enter your project id: ");
+ String projectId = scanner.nextLine();
+ System.out.println("Enter your dataset id: ");
+ String datasetId = scanner.nextLine();
+ 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()){
+ System.out.println(responses.next());
+ }
+
+ fromCLI.close();
+ }
+ // [END main]
+
+
+
+// [START run]
+ public static Iterator run(final String projectId,
+ final String datasetId,
+ final String tableId,
+ final JsonReader rows) throws IOException{
+
+
+ final Bigquery bigquery = BigqueryServiceFactory.getService();
+ final Gson gson = new Gson();
+ rows.beginArray();
+
+ return new Iterator(){
+
+ public boolean hasNext() {
+ try {
+ return rows.hasNext();
+ } catch (IOException e) {
+ // TODO(elibixby): Auto-generated catch block
+ e.printStackTrace();
+ }
+ return false;
+ }
+
+ public TableDataInsertAllResponse next() {
+ try {
+ Map rowData = gson.