use of com.google.cloud.bigquery.TableInfo in project google-cloud-java by GoogleCloudPlatform.
the class BigQuerySnippets method updateTable.
/**
* Example of updating a table by changing its friendly name.
*/
// [TARGET update(TableInfo, TableOption...)]
// [VARIABLE "my_dataset_name"]
// [VARIABLE "my_table_name"]
// [VARIABLE "new_friendly_name"]
public Table updateTable(String datasetName, String tableName, String newFriendlyName) {
// [START updateTable]
Table oldTable = bigquery.getTable(datasetName, tableName);
TableInfo tableInfo = oldTable.toBuilder().setFriendlyName(newFriendlyName).build();
Table newTable = bigquery.update(tableInfo);
// [END updateTable]
return newTable;
}
use of com.google.cloud.bigquery.TableInfo in project presto by prestodb.
the class BigQueryMetadata method getTableMetadata.
@Override
public ConnectorTableMetadata getTableMetadata(ConnectorSession session, ConnectorTableHandle tableHandle) {
log.debug("getTableMetadata(session=%s, tableHandle=%s)", session, tableHandle);
TableInfo table = bigQueryClient.getTable(((BigQueryTableHandle) tableHandle).getTableId());
SchemaTableName schemaTableName = new SchemaTableName(table.getTableId().getDataset(), table.getTableId().getTable());
Schema schema = table.getDefinition().getSchema();
List<ColumnMetadata> columns = schema == null ? ImmutableList.of() : schema.getFields().stream().map(Conversions::toColumnMetadata).collect(toImmutableList());
return new ConnectorTableMetadata(schemaTableName, columns);
}
use of com.google.cloud.bigquery.TableInfo in project presto by prestodb.
the class ReadSessionCreator method create.
public Storage.ReadSession create(TableId table, ImmutableList<String> selectedFields, Optional<String> filter, int parallelism) {
TableInfo tableDetails = bigQueryClient.getTable(table);
TableInfo actualTable = getActualTable(tableDetails, selectedFields, new String[] {});
try (BigQueryStorageClient bigQueryStorageClient = bigQueryStorageClientFactory.createBigQueryStorageClient()) {
ReadOptions.TableReadOptions.Builder readOptions = ReadOptions.TableReadOptions.newBuilder().addAllSelectedFields(selectedFields);
filter.ifPresent(readOptions::setRowRestriction);
TableReferenceProto.TableReference tableReference = toTableReference(actualTable.getTableId());
Storage.ReadSession readSession = bigQueryStorageClient.createReadSession(Storage.CreateReadSessionRequest.newBuilder().setParent("projects/" + bigQueryClient.getProjectId()).setFormat(Storage.DataFormat.AVRO).setRequestedStreams(parallelism).setReadOptions(readOptions).setTableReference(tableReference).setShardingStrategy(Storage.ShardingStrategy.BALANCED).build());
return readSession;
}
}
use of com.google.cloud.bigquery.TableInfo in project google-cloud-java by GoogleCloudPlatform.
the class ITBigQueryTest method testInsertAllWithSuffix.
@Test
public void testInsertAllWithSuffix() throws InterruptedException {
String tableName = "test_insert_all_with_suffix_table";
StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA);
TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableDefinition);
assertNotNull(bigquery.create(tableInfo));
ImmutableMap.Builder<String, Object> builder1 = ImmutableMap.builder();
builder1.put("TimestampField", "2014-08-19 07:41:35.220 -05:00");
builder1.put("StringField", "stringValue");
builder1.put("IntegerArrayField", ImmutableList.of(0, 1));
builder1.put("BooleanField", false);
builder1.put("BytesField", BYTES_BASE64);
builder1.put("RecordField", ImmutableMap.of("TimestampField", "1969-07-20 20:18:04 UTC", "IntegerArrayField", ImmutableList.of(1, 0), "BooleanField", true, "BytesField", BYTES_BASE64));
builder1.put("IntegerField", 5);
builder1.put("FloatField", 1.2);
ImmutableMap.Builder<String, Object> builder2 = ImmutableMap.builder();
builder2.put("TimestampField", "2014-08-19 07:41:35.220 -05:00");
builder2.put("StringField", "stringValue");
builder2.put("IntegerArrayField", ImmutableList.of(0, 1));
builder2.put("BooleanField", false);
builder2.put("BytesField", BYTES_BASE64);
builder2.put("RecordField", ImmutableMap.of("TimestampField", "1969-07-20 20:18:04 UTC", "IntegerArrayField", ImmutableList.of(1, 0), "BooleanField", true, "BytesField", BYTES_BASE64));
builder2.put("IntegerField", 5);
builder2.put("FloatField", 1.2);
InsertAllRequest request = InsertAllRequest.newBuilder(tableInfo.getTableId()).addRow(builder1.build()).addRow(builder2.build()).setTemplateSuffix("_suffix").build();
InsertAllResponse response = bigquery.insertAll(request);
assertFalse(response.hasErrors());
assertEquals(0, response.getInsertErrors().size());
String newTableName = tableName + "_suffix";
Table suffixTable = bigquery.getTable(DATASET, newTableName, TableOption.fields());
// wait until the new table is created. If the table is never created the test will time-out
while (suffixTable == null) {
Thread.sleep(1000L);
suffixTable = bigquery.getTable(DATASET, newTableName, TableOption.fields());
}
assertTrue(bigquery.delete(TableId.of(DATASET, tableName)));
assertTrue(suffixTable.delete());
}
use of com.google.cloud.bigquery.TableInfo in project google-cloud-java by GoogleCloudPlatform.
the class ITBigQueryTest method testInsertAll.
@Test
public void testInsertAll() throws IOException {
String tableName = "test_insert_all_table";
StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA);
TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableDefinition);
assertNotNull(bigquery.create(tableInfo));
ImmutableMap.Builder<String, Object> builder1 = ImmutableMap.builder();
builder1.put("TimestampField", "2014-08-19 07:41:35.220 -05:00");
builder1.put("StringField", "stringValue");
builder1.put("IntegerArrayField", ImmutableList.of(0, 1));
builder1.put("BooleanField", false);
builder1.put("BytesField", BYTES_BASE64);
builder1.put("RecordField", ImmutableMap.of("TimestampField", "1969-07-20 20:18:04 UTC", "IntegerArrayField", ImmutableList.of(1, 0), "BooleanField", true, "BytesField", BYTES_BASE64));
builder1.put("IntegerField", 5);
builder1.put("FloatField", 1.2);
ImmutableMap.Builder<String, Object> builder2 = ImmutableMap.builder();
builder2.put("TimestampField", "2014-08-19 07:41:35.220 -05:00");
builder2.put("StringField", "stringValue");
builder2.put("IntegerArrayField", ImmutableList.of(0, 1));
builder2.put("BooleanField", false);
builder2.put("BytesField", BYTES_BASE64);
builder2.put("RecordField", ImmutableMap.of("TimestampField", "1969-07-20 20:18:04 UTC", "IntegerArrayField", ImmutableList.of(1, 0), "BooleanField", true, "BytesField", BYTES_BASE64));
builder2.put("IntegerField", 5);
builder2.put("FloatField", 1.2);
InsertAllRequest request = InsertAllRequest.newBuilder(tableInfo.getTableId()).addRow(builder1.build()).addRow(builder2.build()).build();
InsertAllResponse response = bigquery.insertAll(request);
assertFalse(response.hasErrors());
assertEquals(0, response.getInsertErrors().size());
assertTrue(bigquery.delete(TableId.of(DATASET, tableName)));
}
Aggregations