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 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(); }
@Test public void testUpdateTableWithoutProject() { TableInfo tableInfo = TABLE_INFO.setProjectId(PROJECT); TableId tableId = TableId.of("", TABLE_ID.getDataset(), TABLE_ID.getTable()); tableInfo.toBuilder().setTableId(tableId); EasyMock.expect(bigqueryRpcMock.patch(tableInfo.toPb(), EMPTY_RPC_OPTIONS)) .andReturn(tableInfo.toPb()); EasyMock.replay(bigqueryRpcMock); BigQueryOptions bigQueryOptions = createBigQueryOptionsForProject(PROJECT, rpcFactoryMock); bigquery = bigQueryOptions.getService(); Table table = bigquery.update(tableInfo); assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(tableInfo)), table); }
.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(); } }
/** * Returns a builder for 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 Builder newBuilder(TableId tableId, TableDefinition definition) { return new BuilderImpl().setTableId(tableId).setDefinition(definition); }
/** * 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(); }
/** * 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 builder(tableId, definition).build(); }
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(); }
Builder(BigQuery bigquery, TableId tableId, TableDefinition defintion) { this.bigquery = bigquery; this.infoBuilder = new TableInfo.BuilderImpl(); this.infoBuilder.tableId(tableId).definition(defintion); }
private void createTable(TableId tableId, Schema schema) { TableInfo tableInfo = TableInfo.newBuilder(tableId, StandardTableDefinition.of(schema)) .setFriendlyName(tableId.getTable()) .build(); client.create(tableInfo, FIELD_OPTIONS); }
/** * Returns a builder for 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 Builder builder(TableId tableId, TableDefinition definition) { return new BuilderImpl().tableId(tableId).definition(definition); }
Builder(BigQuery bigquery, TableId tableId, TableDefinition defintion) { this.bigquery = bigquery; this.infoBuilder = new TableInfo.BuilderImpl(); this.infoBuilder.setTableId(tableId).setDefinition(defintion); }
TableInfo setProjectId(String projectId) { if (Strings.isNullOrEmpty(getTableId().getProject())) { return toBuilder().setTableId(getTableId().setProjectId(projectId)).build(); } return this; }
@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()); }
@Test public void testUpdateTableWithSelectedFields() { Capture<Map<BigQueryRpc.Option, Object>> capturedOptions = Capture.newInstance(); TableInfo updatedTableInfo = TABLE_INFO.toBuilder().setDescription("newDescription").build(); TableInfo updatedTableInfoWithProject = TABLE_INFO_WITH_PROJECT.toBuilder().setDescription("newDescription").build(); EasyMock.expect( bigqueryRpcMock.patch(eq(updatedTableInfoWithProject.toPb()), capture(capturedOptions))) .andReturn(updatedTableInfoWithProject.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.getService(); Table table = bigquery.update(updatedTableInfo, TABLE_OPTION_FIELDS); String selector = (String) capturedOptions.getValue().get(TABLE_OPTION_FIELDS.getRpcOption()); assertTrue(selector.contains("tableReference")); assertTrue(selector.contains("schema")); assertTrue(selector.contains("etag")); assertEquals(31, selector.length()); assertEquals( new Table(bigquery, new TableInfo.BuilderImpl(updatedTableInfoWithProject)), table); }
@Test public void testUpdateTableWithSelectedFields() { String tableName = "test_update_with_selected_fields_table"; StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA); TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableDefinition); Table createdTable = bigquery.create(tableInfo); assertNotNull(createdTable); Table updatedTable = bigquery.update( tableInfo.toBuilder().setDescription("newDescr").build(), TableOption.fields(TableField.DESCRIPTION)); assertTrue(updatedTable.getDefinition() instanceof StandardTableDefinition); assertEquals(DATASET, updatedTable.getTableId().getDataset()); assertEquals(tableName, updatedTable.getTableId().getTable()); assertEquals("newDescr", updatedTable.getDescription()); assertNull(updatedTable.getDefinition().getSchema()); assertNull(updatedTable.getLastModifiedTime()); assertNull(updatedTable.<StandardTableDefinition>getDefinition().getNumBytes()); assertNull(updatedTable.<StandardTableDefinition>getDefinition().getNumRows()); assertTrue(createdTable.delete()); }
/** 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 testReload() throws Exception { initializeExpectedTable(4); TableInfo updatedInfo = TABLE_INFO.toBuilder().setDescription("Description").build(); Table expectedTable = new Table(serviceMockReturnsOptions, new TableInfo.BuilderImpl(updatedInfo)); expect(bigquery.getOptions()).andReturn(mockOptions); expect(bigquery.getTable(TABLE_INFO.getTableId())).andReturn(expectedTable); replay(bigquery); initializeTable(); Table updatedTable = table.reload(); compareTable(expectedTable, updatedTable); }