/** Example of updating a table by changing its expiration. */ // [TARGET update(TableInfo, TableOption...)] // [VARIABLE "my_dataset_name"] // [VARIABLE "my_table_name"] public Table updateTableExpiration(String datasetName, String tableName) { // [START bigquery_update_table_expiration] Table beforeTable = bigquery.getTable(datasetName, tableName); // Set table to expire 5 days from now. long expirationMillis = DateTime.now().plusDays(5).getMillis(); TableInfo tableInfo = beforeTable.toBuilder().setExpirationTime(expirationMillis).build(); Table afterTable = bigquery.update(tableInfo); // [END bigquery_update_table_expiration] return afterTable; }
/** Example of updating the table's information. */ // [TARGET update(TableOption...)] public Table update() { // [START ] Table updatedTable = table.toBuilder().setDescription("new description").build().update(); // [END ] return updatedTable; }
/** Example of updating a table by changing its description. */ public Table updateTableDescription(String datasetName, String tableName, String newDescription) { // [START bigquery_update_table_description] // String datasetName = "my_dataset_name"; // String tableName = "my_table_name"; // String newDescription = "new_description"; Table beforeTable = bigquery.getTable(datasetName, tableName); TableInfo tableInfo = beforeTable.toBuilder().setDescription(newDescription).build(); Table afterTable = bigquery.update(tableInfo); // [END bigquery_update_table_description] return afterTable; }
@Test public void testToBuilder() { initializeExpectedTable(4); replay(bigquery); compareTable(expectedTable, expectedTable.toBuilder().build()); }
@Test public void testUpdate() { initializeExpectedTable(4); Table expectedUpdatedTable = expectedTable.toBuilder().setDescription("Description").build(); expect(bigquery.getOptions()).andReturn(mockOptions); expect(bigquery.update(eq(expectedTable))).andReturn(expectedUpdatedTable); replay(bigquery); initializeTable(); Table actualUpdatedTable = table.update(); compareTable(expectedUpdatedTable, actualUpdatedTable); }
@Test public void testUpdateWithOptions() { initializeExpectedTable(4); Table expectedUpdatedTable = expectedTable.toBuilder().setDescription("Description").build(); expect(bigquery.getOptions()).andReturn(mockOptions); expect(bigquery.update(eq(expectedTable), eq(BigQuery.TableOption.fields()))) .andReturn(expectedUpdatedTable); replay(bigquery); initializeTable(); Table actualUpdatedTable = table.update(BigQuery.TableOption.fields()); compareTable(expectedUpdatedTable, actualUpdatedTable); }
@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(); }