/** * Returns a {@code TableInfo} object given table identity and definition. Use {@link * StandardTableDefinition} to create simple BigQuery table. Use {@link ViewDefinition} to create * a BigQuery view. Use {@link ExternalTableDefinition} to create a BigQuery a table backed by * external data. */ public static TableInfo of(TableId tableId, TableDefinition definition) { return newBuilder(tableId, definition).build(); }
/** Example of creating a table. */ // [TARGET create(TableInfo, TableOption...)] // [VARIABLE "my_dataset_name"] // [VARIABLE "my_table_name"] // [VARIABLE "string_field"] public Table createTable(String datasetName, String tableName, String fieldName) { // [START bigquery_create_table] TableId tableId = TableId.of(datasetName, tableName); // Table field definition Field field = Field.of(fieldName, LegacySQLTypeName.STRING); // Table schema definition Schema schema = Schema.of(field); TableDefinition tableDefinition = StandardTableDefinition.of(schema); TableInfo tableInfo = TableInfo.newBuilder(tableId, tableDefinition).build(); Table table = bigquery.create(tableInfo); // [END bigquery_create_table] return table; }
@Test public void testUpdateTable() { String tableName = "test_update_table"; StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA); TableInfo tableInfo = TableInfo.newBuilder(TableId.of(DATASET, tableName), tableDefinition) .setDescription("Some Description") .setLabels(Collections.singletonMap("a", "b")) .build(); Table createdTable = bigquery.create(tableInfo); assertThat(createdTable.getDescription()).isEqualTo("Some Description"); assertThat(createdTable.getLabels()).containsExactly("a", "b"); Map<String, String> updateLabels = new HashMap<>(); updateLabels.put("x", "y"); updateLabels.put("a", null); Table updatedTable = bigquery.update( createdTable .toBuilder() .setDescription("Updated Description") .setLabels(updateLabels) .build()); assertThat(updatedTable.getDescription()).isEqualTo("Updated Description"); assertThat(updatedTable.getLabels()).containsExactly("x", "y"); updatedTable = bigquery.update(updatedTable.toBuilder().setLabels(null).build()); assertThat(updatedTable.getLabels()).isEmpty(); assertThat(createdTable.delete()).isTrue(); }
Schema schema = Schema.of(Field.of("name", LegacySQLTypeName.STRING)); TableDefinition tableDef = StandardTableDefinition.of(schema); Table table = bigquery.create(TableInfo.newBuilder(tableId, tableDef).build());
@Test public void testCreateAndGetTableWithSelectedField() { String tableName = "test_create_and_get_selected_fields_table"; TableId tableId = TableId.of(DATASET, tableName); StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA); Table createdTable = bigquery.create( TableInfo.newBuilder(tableId, tableDefinition) .setLabels(Collections.singletonMap("a", "b")) .build()); assertNotNull(createdTable); assertEquals(DATASET, createdTable.getTableId().getDataset()); assertEquals(tableName, createdTable.getTableId().getTable()); Table remoteTable = bigquery.getTable( DATASET, tableName, TableOption.fields(TableField.CREATION_TIME, TableField.LABELS)); assertNotNull(remoteTable); assertTrue(remoteTable.getDefinition() instanceof StandardTableDefinition); assertEquals(createdTable.getTableId(), remoteTable.getTableId()); assertEquals(TableDefinition.Type.TABLE, remoteTable.getDefinition().getType()); assertThat(remoteTable.getLabels()).containsExactly("a", "b"); assertNotNull(remoteTable.getCreationTime()); assertNull(remoteTable.getDefinition().getSchema()); assertNull(remoteTable.getLastModifiedTime()); assertNull(remoteTable.<StandardTableDefinition>getDefinition().getNumBytes()); assertNull(remoteTable.<StandardTableDefinition>getDefinition().getNumRows()); assertNull(remoteTable.<StandardTableDefinition>getDefinition().getTimePartitioning()); assertNull(remoteTable.<StandardTableDefinition>getDefinition().getClustering()); assertTrue(remoteTable.delete()); }
/** * Returns a {@code TableInfo} object given table identity and definition. Use {@link * StandardTableDefinition} to create simple BigQuery table. Use {@link ViewDefinition} to create * a BigQuery view. Use {@link ExternalTableDefinition} to create a BigQuery a table backed by * external data. */ public static TableInfo of(TableId tableId, TableDefinition definition) { return newBuilder(tableId, definition).build(); }
private void createTable(TableId tableId, Schema schema) { TableInfo tableInfo = TableInfo.newBuilder(tableId, StandardTableDefinition.of(schema)) .setFriendlyName(tableId.getTable()) .build(); client.create(tableInfo, FIELD_OPTIONS); }
public static TableInfo createTableInfo(com.google.api.services.bigquery.model.Table model) { String type = model.getType(); if (type == null) { model.setType(TableDefinition.Type.TABLE.name()); } TableId tableId = TableId.fromPb(model.getTableReference()); TableDefinition definition = tableDefinition(model); return TableInfo.newBuilder(tableId, definition).build(); }
.setTimePartitioning(TimePartitioning.of(Type.DAY)) .build(); TableInfo tableInfo = TableInfo.newBuilder(tableId, tableDefinition).build(); checkTableCreation(bigQuery.create(tableInfo), featureSpec); Schema newSchema = Schema.of(fields); TableDefinition tableDefinition = StandardTableDefinition.of(newSchema); TableInfo tableInfo = TableInfo.newBuilder(tableId, tableDefinition).build(); checkTableCreation(bigQuery.update(tableInfo), featureSpec); createOrUpdateView(
TableInfo constructTableInfo(TableId table, Schema kafkaConnectSchema) { com.google.cloud.bigquery.Schema bigQuerySchema = schemaConverter.convertSchema(kafkaConnectSchema); StandardTableDefinition tableDefinition = StandardTableDefinition.newBuilder() .setSchema(bigQuerySchema) .setTimePartitioning(TimePartitioning.of(TimePartitioning.Type.DAY)) .build(); TableInfo.Builder tableInfoBuilder = TableInfo.newBuilder(table, tableDefinition); if (kafkaConnectSchema.doc() != null) { tableInfoBuilder.setDescription(kafkaConnectSchema.doc()); } return tableInfoBuilder.build(); } }