Java源码示例:com.google.api.services.bigquery.Bigquery.Datasets
示例1
private void setupBigQueryTable(
String projectId, String datasetId, String tableId, TableSchema schema) throws IOException {
if (bigQueryClient == null) {
bigQueryClient = newBigQueryClient(options.as(BigQueryOptions.class)).build();
}
Datasets datasetService = bigQueryClient.datasets();
if (executeNullIfNotFound(datasetService.get(projectId, datasetId)) == null) {
Dataset newDataset =
new Dataset()
.setDatasetReference(
new DatasetReference().setProjectId(projectId).setDatasetId(datasetId));
datasetService.insert(projectId, newDataset).execute();
}
Tables tableService = bigQueryClient.tables();
Table table = executeNullIfNotFound(tableService.get(projectId, datasetId, tableId));
if (table == null) {
Table newTable =
new Table()
.setSchema(schema)
.setTableReference(
new TableReference()
.setProjectId(projectId)
.setDatasetId(datasetId)
.setTableId(tableId));
tableService.insert(projectId, datasetId, newTable).execute();
} else if (!table.getSchema().equals(schema)) {
throw new RuntimeException(
"Table exists and schemas do not match, expecting: "
+ schema.toPrettyString()
+ ", actual: "
+ table.getSchema().toPrettyString());
}
}
示例2
private void setupBigQueryTable(
String projectId, String datasetId, String tableId, TableSchema schema) throws IOException {
if (bigQueryClient == null) {
bigQueryClient = newBigQueryClient(options.as(BigQueryOptions.class)).build();
}
Datasets datasetService = bigQueryClient.datasets();
if (executeNullIfNotFound(datasetService.get(projectId, datasetId)) == null) {
Dataset newDataset =
new Dataset()
.setDatasetReference(
new DatasetReference().setProjectId(projectId).setDatasetId(datasetId));
datasetService.insert(projectId, newDataset).execute();
}
Tables tableService = bigQueryClient.tables();
Table table = executeNullIfNotFound(tableService.get(projectId, datasetId, tableId));
if (table == null) {
Table newTable =
new Table()
.setSchema(schema)
.setTableReference(
new TableReference()
.setProjectId(projectId)
.setDatasetId(datasetId)
.setTableId(tableId));
tableService.insert(projectId, datasetId, newTable).execute();
} else if (!table.getSchema().equals(schema)) {
throw new RuntimeException(
"Table exists and schemas do not match, expecting: "
+ schema.toPrettyString()
+ ", actual: "
+ table.getSchema().toPrettyString());
}
}
示例3
@Before
public void setUp() throws IOException {
MockitoAnnotations.initMocks(this);
LoggerConfig.getConfig(GsonBigQueryInputFormat.class).setLevel(Level.FINE);
// Create fake job reference.
JobReference fakeJobReference = new JobReference().setProjectId(jobProjectId).setJobId(jobId);
// Create the job result.
jobStatus = new JobStatus();
jobStatus.setState("DONE");
jobStatus.setErrorResult(null);
jobHandle = new Job();
jobHandle.setStatus(jobStatus);
jobHandle.setJobReference(fakeJobReference);
// Mocks for Bigquery jobs.
when(mockBigquery.jobs()).thenReturn(mockBigqueryJobs);
// Mock getting Bigquery job.
when(mockBigqueryJobs.get(any(String.class), any(String.class)))
.thenReturn(mockBigqueryJobsGet);
when(mockBigqueryJobsGet.setLocation(any(String.class))).thenReturn(mockBigqueryJobsGet);
// Mock inserting Bigquery job.
when(mockBigqueryJobs.insert(any(String.class), any(Job.class)))
.thenReturn(mockBigqueryJobsInsert);
// Fake table.
fakeTableSchema = new TableSchema();
fakeTable = new Table().setSchema(fakeTableSchema).setLocation("test_location");
// Mocks for Bigquery tables.
when(mockBigquery.tables()).thenReturn(mockBigqueryTables);
when(mockBigqueryTables.get(any(String.class), any(String.class), any(String.class)))
.thenReturn(mockBigqueryTablesGet);
Datasets datasets = Mockito.mock(Datasets.class);
Datasets.Get datasetsGet = Mockito.mock(Datasets.Get.class);
Dataset dataset = new Dataset().setLocation("test_location");
when(mockBigquery.datasets()).thenReturn(datasets);
when(datasets.get(any(String.class), any(String.class))).thenReturn(datasetsGet);
when(datasetsGet.execute()).thenReturn(dataset);
// Create table reference.
tableRef = new TableReference();
tableRef.setProjectId(projectId);
tableRef.setDatasetId(datasetId);
tableRef.setTableId(tableId);
helper = new BigQueryHelper(mockBigquery);
helper.setErrorExtractor(mockErrorExtractor);
}