Skip to content

Commit

Permalink
Merge pull request #500 from mziccard/bigquery
Browse files Browse the repository at this point in the history
Make BigQuery get/create/updated Table and Job methods generic
  • Loading branch information
aozarov committed Dec 22, 2015
2 parents 75a0ece + 862905f commit a0f5f1b
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 85 deletions.
4 changes: 2 additions & 2 deletions gcloud-java-bigquery/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ Field stringField = Field.of("StringField", Field.Type.string());
// Table schema definition
Schema schema = Schema.of(stringField);
// Create a table
BaseTableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, schema));
TableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, schema));
```

#### Loading data into a table
Expand Down Expand Up @@ -232,7 +232,7 @@ public class GcloudBigQueryExample {
// Table schema definition
Schema schema = Schema.of(stringField);
// Create a table
BaseTableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, schema));
TableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, schema));

// Define rows to insert
Map<String, Object> firstRow = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,14 +463,14 @@ public static QueryResultsOption maxWaitTime(long maxWaitTime) {
*
* @throws BigQueryException upon failure
*/
BaseTableInfo create(BaseTableInfo table, TableOption... options) throws BigQueryException;
<T extends BaseTableInfo> T create(T table, TableOption... options) throws BigQueryException;

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

/**
* Returns the requested dataset or {@code null} if not found.
Expand Down Expand Up @@ -541,22 +541,23 @@ public static QueryResultsOption maxWaitTime(long maxWaitTime) {
*
* @throws BigQueryException upon failure
*/
BaseTableInfo update(BaseTableInfo table, TableOption... options) throws BigQueryException;
<T extends BaseTableInfo> T update(T table, TableOption... options) throws BigQueryException;

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

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

/**
* Lists the tables in the dataset. This method returns partial information on each table
Expand Down Expand Up @@ -610,14 +611,14 @@ Page<List<FieldValue>> listTableData(TableId tableId, TableDataListOption... opt
*
* @throws BigQueryException upon failure
*/
JobInfo getJob(String jobId, JobOption... options) throws BigQueryException;
<T extends JobInfo> T 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;
<T extends JobInfo> T getJob(JobId jobId, JobOption... options) throws BigQueryException;

/**
* Lists the jobs.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public Dataset call() {
}

@Override
public BaseTableInfo create(BaseTableInfo table, TableOption... options)
public <T extends BaseTableInfo> T create(T table, TableOption... options)
throws BigQueryException {
final Table tablePb = setProjectId(table).toPb();
final Map<BigQueryRpc.Option, ?> optionsMap = optionMap(options);
Expand All @@ -216,7 +216,7 @@ public Table call() {
}

@Override
public JobInfo create(JobInfo job, JobOption... options) throws BigQueryException {
public <T extends JobInfo> T create(T job, JobOption... options) throws BigQueryException {
final Job jobPb = setProjectId(job).toPb();
final Map<BigQueryRpc.Option, ?> optionsMap = optionMap(options);
try {
Expand Down Expand Up @@ -335,7 +335,7 @@ public Dataset call() {
}

@Override
public BaseTableInfo update(BaseTableInfo table, TableOption... options)
public <T extends BaseTableInfo> T update(T table, TableOption... options)
throws BigQueryException {
final Table tablePb = setProjectId(table).toPb();
final Map<BigQueryRpc.Option, ?> optionsMap = optionMap(options);
Expand All @@ -352,13 +352,13 @@ public Table call() {
}

@Override
public BaseTableInfo getTable(final String datasetId, final String tableId,
public <T extends BaseTableInfo> T getTable(final String datasetId, final String tableId,
TableOption... options) throws BigQueryException {
return getTable(TableId.of(datasetId, tableId), options);
}

@Override
public BaseTableInfo getTable(final TableId tableId, TableOption... options)
public <T extends BaseTableInfo> T getTable(final TableId tableId, TableOption... options)
throws BigQueryException {
final Map<BigQueryRpc.Option, ?> optionsMap = optionMap(options);
try {
Expand All @@ -368,7 +368,7 @@ public Table call() {
return bigQueryRpc.getTable(tableId.dataset(), tableId.table(), optionsMap);
}
}, options().retryParams(), EXCEPTION_HANDLER);
return answer == null ? null : BaseTableInfo.fromPb(answer);
return answer == null ? null : BaseTableInfo.<T>fromPb(answer);
} catch (RetryHelper.RetryHelperException e) {
throw BigQueryException.translateAndThrow(e);
}
Expand Down Expand Up @@ -466,12 +466,13 @@ public List<FieldValue> apply(TableRow rowPb) {
}

@Override
public JobInfo getJob(String jobId, JobOption... options) throws BigQueryException {
public <T extends JobInfo> T getJob(String jobId, JobOption... options) throws BigQueryException {
return getJob(JobId.of(jobId), options);
}

@Override
public JobInfo getJob(final JobId jobId, JobOption... options) throws BigQueryException {
public <T extends JobInfo> T getJob(final JobId jobId, JobOption... options)
throws BigQueryException {
final Map<BigQueryRpc.Option, ?> optionsMap = optionMap(options);
try {
Job answer = runWithRetries(new Callable<Job>() {
Expand All @@ -480,7 +481,7 @@ public Job call() {
return bigQueryRpc.getJob(jobId.job(), optionsMap);
}
}, options().retryParams(), EXCEPTION_HANDLER);
return answer == null ? null : JobInfo.fromPb(answer);
return answer == null ? null : JobInfo.<T>fromPb(answer);
} catch (RetryHelper.RetryHelperException e) {
throw BigQueryException.translateAndThrow(e);
}
Expand Down
Loading

0 comments on commit a0f5f1b

Please sign in to comment.