use of com.google.cloud.bigquery.TableInfo in project google-cloud-java by GoogleCloudPlatform.
the class ITBigQueryTest method testCreateAndGetJobWithSelectedFields.
@Test
public void testCreateAndGetJobWithSelectedFields() throws InterruptedException, TimeoutException {
String sourceTableName = "test_create_and_get_job_with_selected_fields_source_table";
String destinationTableName = "test_create_and_get_job_with_selected_fields_destination_table";
TableId sourceTable = TableId.of(DATASET, sourceTableName);
StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA);
TableInfo tableInfo = TableInfo.of(sourceTable, tableDefinition);
Table createdTable = bigquery.create(tableInfo);
assertNotNull(createdTable);
assertEquals(DATASET, createdTable.getTableId().getDataset());
assertEquals(sourceTableName, createdTable.getTableId().getTable());
TableId destinationTable = TableId.of(DATASET, destinationTableName);
CopyJobConfiguration configuration = CopyJobConfiguration.of(destinationTable, sourceTable);
Job createdJob = bigquery.create(JobInfo.of(configuration), JobOption.fields(JobField.ETAG));
CopyJobConfiguration createdConfiguration = createdJob.getConfiguration();
assertNotNull(createdJob.getJobId());
assertNotNull(createdConfiguration.getSourceTables());
assertNotNull(createdConfiguration.getDestinationTable());
assertNotNull(createdJob.getEtag());
assertNull(createdJob.getStatistics());
assertNull(createdJob.getStatus());
assertNull(createdJob.getSelfLink());
assertNull(createdJob.getUserEmail());
Job remoteJob = bigquery.getJob(createdJob.getJobId(), JobOption.fields(JobField.ETAG));
CopyJobConfiguration remoteConfiguration = remoteJob.getConfiguration();
assertEquals(createdJob.getJobId(), remoteJob.getJobId());
assertEquals(createdConfiguration.getSourceTables(), remoteConfiguration.getSourceTables());
assertEquals(createdConfiguration.getDestinationTable(), remoteConfiguration.getDestinationTable());
assertEquals(createdConfiguration.getCreateDisposition(), remoteConfiguration.getCreateDisposition());
assertEquals(createdConfiguration.getWriteDisposition(), remoteConfiguration.getWriteDisposition());
assertNotNull(remoteJob.getEtag());
assertNull(remoteJob.getStatistics());
assertNull(remoteJob.getStatus());
assertNull(remoteJob.getSelfLink());
assertNull(remoteJob.getUserEmail());
assertTrue(createdTable.delete());
Job completedJob = remoteJob.waitFor(WaitForOption.checkEvery(1, TimeUnit.SECONDS), WaitForOption.timeout(1, TimeUnit.MINUTES));
assertNotNull(completedJob);
assertNull(completedJob.getStatus().getError());
assertTrue(bigquery.delete(DATASET, destinationTableName));
}
use of com.google.cloud.bigquery.TableInfo in project google-cloud-java by GoogleCloudPlatform.
the class ITBigQueryTest method testCreateExternalTable.
@Test
public void testCreateExternalTable() throws InterruptedException {
String tableName = "test_create_external_table";
TableId tableId = TableId.of(DATASET, tableName);
ExternalTableDefinition externalTableDefinition = ExternalTableDefinition.of("gs://" + BUCKET + "/" + JSON_LOAD_FILE, TABLE_SCHEMA, FormatOptions.json());
TableInfo tableInfo = TableInfo.of(tableId, externalTableDefinition);
Table createdTable = bigquery.create(tableInfo);
assertNotNull(createdTable);
assertEquals(DATASET, createdTable.getTableId().getDataset());
assertEquals(tableName, createdTable.getTableId().getTable());
Table remoteTable = bigquery.getTable(DATASET, tableName);
assertNotNull(remoteTable);
assertTrue(remoteTable.getDefinition() instanceof ExternalTableDefinition);
assertEquals(createdTable.getTableId(), remoteTable.getTableId());
assertEquals(TABLE_SCHEMA, remoteTable.getDefinition().getSchema());
QueryRequest request = QueryRequest.newBuilder("SELECT TimestampField, StringField, IntegerArrayField, BooleanField FROM " + DATASET + "." + tableName).setDefaultDataset(DatasetId.of(DATASET)).setMaxWaitTime(60000L).setPageSize(1000L).build();
QueryResponse response = bigquery.query(request);
while (!response.jobCompleted()) {
response = bigquery.getQueryResults(response.getJobId());
Thread.sleep(1000);
}
long integerValue = 0;
int rowCount = 0;
for (List<FieldValue> row : response.getResult().getValues()) {
FieldValue timestampCell = row.get(0);
FieldValue stringCell = row.get(1);
FieldValue integerCell = row.get(2);
FieldValue booleanCell = row.get(3);
assertEquals(FieldValue.Attribute.PRIMITIVE, timestampCell.getAttribute());
assertEquals(FieldValue.Attribute.PRIMITIVE, stringCell.getAttribute());
assertEquals(FieldValue.Attribute.PRIMITIVE, integerCell.getAttribute());
assertEquals(FieldValue.Attribute.PRIMITIVE, booleanCell.getAttribute());
assertEquals(1408452095220000L, timestampCell.getTimestampValue());
assertEquals("stringValue", stringCell.getStringValue());
assertEquals(integerValue, integerCell.getLongValue());
assertEquals(false, booleanCell.getBooleanValue());
integerValue = ~integerValue & 0x1;
rowCount++;
}
assertEquals(4, rowCount);
assertTrue(remoteTable.delete());
}
use of com.google.cloud.bigquery.TableInfo in project presto by prestodb.
the class BigQueryMetadata method listTables.
private List<SchemaTableName> listTables(ConnectorSession session, SchemaTablePrefix prefix) {
if (prefix.getTableName() == null) {
return listTables(session, Optional.ofNullable(prefix.getSchemaName()));
}
SchemaTableName tableName = prefix.toSchemaTableName();
Optional<TableInfo> tableInfo = getBigQueryTable(tableName);
return tableInfo.isPresent() ? ImmutableList.of(tableName) : // table does not exist
ImmutableList.of();
}
use of com.google.cloud.bigquery.TableInfo in project presto by prestodb.
the class BigQueryMetadata method getColumnHandles.
@Override
public Map<String, ColumnHandle> getColumnHandles(ConnectorSession session, ConnectorTableHandle tableHandle) {
log.debug("getColumnHandles(session=%s, tableHandle=%s)", session, tableHandle);
TableInfo table = bigQueryClient.getTable(((BigQueryTableHandle) tableHandle).getTableId());
Schema schema = table.getDefinition().getSchema();
return schema == null ? ImmutableMap.of() : schema.getFields().stream().collect(toMap(Field::getName, Conversions::toColumnHandle));
}
use of com.google.cloud.bigquery.TableInfo in project beam by apache.
the class BigQueryClient method createTable.
private void createTable(TableId tableId, Schema schema) {
TableInfo tableInfo = TableInfo.newBuilder(tableId, StandardTableDefinition.of(schema)).setFriendlyName(tableId.getTable()).build();
client.create(tableInfo, FIELD_OPTIONS);
}
Aggregations