Skip to content

Commit

Permalink
Merge pull request #609 from mziccard/bigquery-hierachies
Browse files Browse the repository at this point in the history
Make bigquery functional classes extend info classes
  • Loading branch information
aozarov committed Feb 4, 2016
2 parents 2bb03eb + fbcbfb4 commit 4bf3796
Show file tree
Hide file tree
Showing 26 changed files with 1,637 additions and 1,064 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,30 +128,30 @@ must [supply credentials](#authentication) and a project ID if running this snip
import com.google.gcloud.bigquery.BigQuery;
import com.google.gcloud.bigquery.BigQueryOptions;
import com.google.gcloud.bigquery.Field;
import com.google.gcloud.bigquery.Job;
import com.google.gcloud.bigquery.JobStatus;
import com.google.gcloud.bigquery.JobInfo;
import com.google.gcloud.bigquery.LoadJobConfiguration;
import com.google.gcloud.bigquery.Schema;
import com.google.gcloud.bigquery.StandardTableDefinition;
import com.google.gcloud.bigquery.Table;
import com.google.gcloud.bigquery.TableId;
import com.google.gcloud.bigquery.TableInfo;
BigQuery bigquery = BigQueryOptions.defaultInstance().service();
TableId tableId = TableId.of("dataset", "table");
TableInfo info = bigquery.getTable(tableId);
if (info == null) {
Table table = bigquery.getTable(tableId);
if (table == null) {
System.out.println("Creating table " + tableId);
Field integerField = Field.of("fieldName", Field.Type.integer());
Schema schema = Schema.of(integerField);
bigquery.create(TableInfo.of(tableId, StandardTableDefinition.of(schema)));
} else {
System.out.println("Loading data into table " + tableId);
LoadJobConfiguration configuration = LoadJobConfiguration.of(tableId, "gs://bucket/path");
JobInfo loadJob = JobInfo.of(configuration);
loadJob = bigquery.create(loadJob);
while (loadJob.status().state() != JobStatus.State.DONE) {
Job loadJob = bigquery.create(JobInfo.of(configuration));
while (!loadJob.isDone()) {
Thread.sleep(1000L);
loadJob = bigquery.getJob(loadJob.jobId());
}
if (loadJob.status().error() != null) {
System.out.println("Job completed with errors");
Expand Down
6 changes: 4 additions & 2 deletions gcloud-java-bigquery/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ with only one string field. Add the following imports at the top of your file:
import com.google.gcloud.bigquery.Field;
import com.google.gcloud.bigquery.Schema;
import com.google.gcloud.bigquery.StandardTableDefinition;
import com.google.gcloud.bigquery.Table;
import com.google.gcloud.bigquery.TableId;
import com.google.gcloud.bigquery.TableInfo;
```
Expand All @@ -127,7 +128,7 @@ Field stringField = Field.of("StringField", Field.Type.string());
Schema schema = Schema.of(stringField);
// Create a table
StandardTableDefinition tableDefinition = StandardTableDefinition.of(schema);
TableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, tableDefinition));
Table createdTable = bigquery.create(TableInfo.of(tableId, tableDefinition));
```

#### Loading data into a table
Expand Down Expand Up @@ -216,6 +217,7 @@ import com.google.gcloud.bigquery.QueryRequest;
import com.google.gcloud.bigquery.QueryResponse;
import com.google.gcloud.bigquery.Schema;
import com.google.gcloud.bigquery.StandardTableDefinition;
import com.google.gcloud.bigquery.Table;
import com.google.gcloud.bigquery.TableId;
import com.google.gcloud.bigquery.TableInfo;

Expand All @@ -242,7 +244,7 @@ public class GcloudBigQueryExample {
Schema schema = Schema.of(stringField);
// Create a table
StandardTableDefinition tableDefinition = StandardTableDefinition.of(schema);
TableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, tableDefinition));
Table createdTable = bigquery.create(TableInfo.of(tableId, tableDefinition));

// Define rows to insert
Map<String, Object> firstRow = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ Access toPb() {
*/
public static final class View extends Entity {

private static final long serialVersionUID = -6851072781269419383L;

private final TableId id;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ private DatasetOption(BigQueryRpc.Option option, Object value) {
/**
* Returns an option to specify the dataset's fields to be returned by the RPC call. If this
* option is not provided all dataset's fields are returned. {@code DatasetOption.fields} can
* be used to specify only the fields of interest. {@link DatasetInfo#datasetId()} is always
* be used to specify only the fields of interest. {@link Dataset#datasetId()} is always
* returned, even if not specified.
*/
public static DatasetOption fields(DatasetField... fields) {
Expand Down Expand Up @@ -275,8 +275,8 @@ private TableOption(BigQueryRpc.Option option, Object value) {
/**
* Returns an option to specify the table's fields to be returned by the RPC call. If this
* option is not provided all table's fields are returned. {@code TableOption.fields} can be
* used to specify only the fields of interest. {@link TableInfo#tableId()} and type (which is
* part of {@link TableInfo#definition()}) are always returned, even if not specified.
* used to specify only the fields of interest. {@link Table#tableId()} and type (which is part
* of {@link Table#definition()}) are always returned, even if not specified.
*/
public static TableOption fields(TableField... fields) {
return new TableOption(BigQueryRpc.Option.FIELDS, TableField.selector(fields));
Expand Down Expand Up @@ -369,7 +369,7 @@ public static JobListOption startPageToken(String pageToken) {
/**
* Returns an option to specify the job's fields to be returned by the RPC call. If this option
* is not provided all job's fields are returned. {@code JobOption.fields()} can be used to
* specify only the fields of interest. {@link JobInfo#jobId()}, {@link JobStatus#state()},
* specify only the fields of interest. {@link Job#jobId()}, {@link JobStatus#state()},
* {@link JobStatus#error()} as well as type-specific configuration (e.g.
* {@link QueryJobConfiguration#query()} for Query Jobs) are always returned, even if not
* specified. {@link JobField#SELF_LINK} and {@link JobField#ETAG} can not be selected when
Expand Down Expand Up @@ -397,7 +397,7 @@ private JobOption(BigQueryRpc.Option option, Object value) {
/**
* Returns an option to specify the job's fields to be returned by the RPC call. If this option
* is not provided all job's fields are returned. {@code JobOption.fields()} can be used to
* specify only the fields of interest. {@link JobInfo#jobId()} as well as type-specific
* specify only the fields of interest. {@link Job#jobId()} as well as type-specific
* configuration (e.g. {@link QueryJobConfiguration#query()} for Query Jobs) are always
* returned, even if not specified.
*/
Expand Down Expand Up @@ -457,46 +457,45 @@ public static QueryResultsOption maxWaitTime(long maxWaitTime) {
*
* @throws BigQueryException upon failure
*/
DatasetInfo create(DatasetInfo dataset, DatasetOption... options) throws BigQueryException;
Dataset create(DatasetInfo dataset, DatasetOption... options) throws BigQueryException;

/**
* Creates a new table.
*
* @throws BigQueryException upon failure
*/
TableInfo create(TableInfo table, TableOption... options) throws BigQueryException;
Table create(TableInfo table, TableOption... options) throws BigQueryException;

/**
* Creates a new job.
*
* @throws BigQueryException upon failure
*/
JobInfo create(JobInfo job, JobOption... options) throws BigQueryException;
Job create(JobInfo job, JobOption... options) throws BigQueryException;

/**
* Returns the requested dataset or {@code null} if not found.
*
* @throws BigQueryException upon failure
*/
DatasetInfo getDataset(String datasetId, DatasetOption... options) throws BigQueryException;
Dataset getDataset(String datasetId, DatasetOption... options) throws BigQueryException;

/**
* Returns the requested dataset or {@code null} if not found.
*
* @throws BigQueryException upon failure
*/
DatasetInfo getDataset(DatasetId datasetId, DatasetOption... options) throws BigQueryException;
Dataset getDataset(DatasetId datasetId, DatasetOption... options) throws BigQueryException;

/**
* Lists the project's datasets. This method returns partial information on each dataset
* ({@link DatasetInfo#datasetId()}, {@link DatasetInfo#friendlyName()} and
* {@link DatasetInfo#id()}). To get complete information use either
* {@link #getDataset(String, DatasetOption...)} or
* ({@link Dataset#datasetId()}, {@link Dataset#friendlyName()} and {@link Dataset#id()}). To get
* complete information use either {@link #getDataset(String, DatasetOption...)} or
* {@link #getDataset(DatasetId, DatasetOption...)}.
*
* @throws BigQueryException upon failure
*/
Page<DatasetInfo> listDatasets(DatasetListOption... options) throws BigQueryException;
Page<Dataset> listDatasets(DatasetListOption... options) throws BigQueryException;

/**
* Deletes the requested dataset.
Expand Down Expand Up @@ -535,54 +534,50 @@ public static QueryResultsOption maxWaitTime(long maxWaitTime) {
*
* @throws BigQueryException upon failure
*/
DatasetInfo update(DatasetInfo dataset, DatasetOption... options) throws BigQueryException;
Dataset update(DatasetInfo dataset, DatasetOption... options) throws BigQueryException;

/**
* Updates table information.
*
* @throws BigQueryException upon failure
*/
TableInfo update(TableInfo table, TableOption... options) throws BigQueryException;
Table update(TableInfo table, TableOption... options) throws BigQueryException;

/**
* Returns the requested table or {@code null} if not found.
*
* @throws BigQueryException upon failure
*/
TableInfo getTable(String datasetId, String tableId, TableOption... options)
throws BigQueryException;
Table getTable(String datasetId, String tableId, TableOption... options) throws BigQueryException;

/**
* Returns the requested table or {@code null} if not found.
*
* @throws BigQueryException upon failure
*/
TableInfo getTable(TableId tableId, TableOption... options)
throws BigQueryException;
Table getTable(TableId tableId, TableOption... options) throws BigQueryException;

/**
* Lists the tables in the dataset. This method returns partial information on each table
* ({@link TableInfo#tableId()}, {@link TableInfo#friendlyName()}, {@link TableInfo#id()} and
* type, which is part of {@link TableInfo#definition()}). To get complete information use either
* ({@link Table#tableId()}, {@link Table#friendlyName()}, {@link Table#id()} and type, which
* is part of {@link Table#definition()}). To get complete information use either
* {@link #getTable(TableId, TableOption...)} or
* {@link #getTable(String, String, TableOption...)}.
*
* @throws BigQueryException upon failure
*/
Page<TableInfo> listTables(String datasetId, TableListOption... options)
throws BigQueryException;
Page<Table> listTables(String datasetId, TableListOption... options) throws BigQueryException;

/**
* Lists the tables in the dataset. This method returns partial information on each table
* ({@link TableInfo#tableId()}, {@link TableInfo#friendlyName()}, {@link TableInfo#id()} and
* type, which is part of {@link TableInfo#definition()}). To get complete information use either
* ({@link Table#tableId()}, {@link Table#friendlyName()}, {@link Table#id()} and type, which
* is part of {@link Table#definition()}). To get complete information use either
* {@link #getTable(TableId, TableOption...)} or
* {@link #getTable(String, String, TableOption...)}.
*
* @throws BigQueryException upon failure
*/
Page<TableInfo> listTables(DatasetId datasetId, TableListOption... options)
throws BigQueryException;
Page<Table> listTables(DatasetId datasetId, TableListOption... options) throws BigQueryException;

/**
* Sends an insert all request.
Expand Down Expand Up @@ -612,21 +607,21 @@ Page<List<FieldValue>> listTableData(TableId tableId, TableDataListOption... opt
*
* @throws BigQueryException upon failure
*/
JobInfo getJob(String jobId, JobOption... options) throws BigQueryException;
Job getJob(String jobId, JobOption... options) throws BigQueryException;

/**
* Returns the requested job or {@code null} if not found.
*
* @throws BigQueryException upon failure
*/
JobInfo getJob(JobId jobId, JobOption... options) throws BigQueryException;
Job getJob(JobId jobId, JobOption... options) throws BigQueryException;

/**
* Lists the jobs.
*
* @throws BigQueryException upon failure
*/
Page<JobInfo> listJobs(JobListOption... options) throws BigQueryException;
Page<Job> listJobs(JobListOption... options) throws BigQueryException;

/**
* Sends a job cancel request. This call will return immediately. The job status can then be
Expand Down
Loading

0 comments on commit 4bf3796

Please sign in to comment.