/** Example of getting a table. */ // [TARGET getTable(String, String, TableOption...)] // [VARIABLE "my_dataset_name"] // [VARIABLE "my_table_name"] public Table getTable(String datasetName, String tableName) { // [START ] Table table = bigquery.getTable(datasetName, tableName); // [END ] return table; }
@Override public void run(BigQuery bigquery, TableId tableId) { System.out.println("Table info: " + bigquery.getTable(tableId)); } }
/** Example of getting a table. */ // [TARGET getTable(TableId, TableOption...)] // [VARIABLE "my_project_id"] // [VARIABLE "my_dataset_name"] // [VARIABLE "my_table_name"] public Table getTableFromId(String projectId, String datasetName, String tableName) { // [START bigquery_get_table] TableId tableId = TableId.of(projectId, datasetName, tableName); Table table = bigquery.getTable(tableId); // [END bigquery_get_table] return table; }
/** 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; }
/** * Returns the requested table in this dataset or {@code null} if not found. * * <p>Example of getting a table in the dataset. * * <pre>{@code * String tableName = “my_table”; * Table table = dataset.get(tableName); * }</pre> * * @param tableId user-defined id of the requested table * @param options table options * @throws BigQueryException upon failure */ public Table get(String tableId, TableOption... options) { return bigquery.getTable(TableId.of(getDatasetId().getDataset(), tableId), options); }
/** 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 testGetNonExistingTable() { assertNull(bigquery.getTable(DATASET, "test_get_non_existing_table")); }
@Test public void testReloadNull() throws Exception { initializeExpectedTable(1); expect(bigquery.getOptions()).andReturn(mockOptions); expect(bigquery.getTable(TABLE_INFO.getTableId())).andReturn(null); replay(bigquery); initializeTable(); assertNull(table.reload()); }
@Test public void testExists_True() throws Exception { initializeExpectedTable(1); BigQuery.TableOption[] expectedOptions = {BigQuery.TableOption.fields()}; expect(bigquery.getOptions()).andReturn(mockOptions); expect(bigquery.getTable(TABLE_INFO.getTableId(), expectedOptions)).andReturn(expectedTable); replay(bigquery); initializeTable(); assertTrue(table.exists()); }
@Test public void testExists_False() throws Exception { initializeExpectedTable(1); BigQuery.TableOption[] expectedOptions = {BigQuery.TableOption.fields()}; expect(bigquery.getOptions()).andReturn(mockOptions); expect(bigquery.getTable(TABLE_INFO.getTableId(), expectedOptions)).andReturn(null); replay(bigquery); initializeTable(); assertFalse(table.exists()); }
@Test public void testGetNull() throws Exception { initializeExpectedDataset(1); expect(bigquery.getOptions()).andReturn(mockOptions); expect(bigquery.getTable(TABLE_INFO1.getTableId())).andReturn(null); replay(bigquery); initializeDataset(); assertNull(dataset.get(TABLE_INFO1.getTableId().getTable())); }
@Test public void testGetTable() { EasyMock.expect(bigqueryRpcMock.getTable(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) .andReturn(TABLE_INFO_WITH_PROJECT.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.getService(); Table table = bigquery.getTable(DATASET, TABLE); assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO_WITH_PROJECT)), table); }
@Test public void testGetTableFromTableId() { EasyMock.expect(bigqueryRpcMock.getTable(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) .andReturn(TABLE_INFO_WITH_PROJECT.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.getService(); Table table = bigquery.getTable(TABLE_ID); assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO_WITH_PROJECT)), 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); }
@Test public void testGet() throws Exception { initializeExpectedDataset(2); Table expectedTable = new Table(serviceMockReturnsOptions, new TableInfo.BuilderImpl(TABLE_INFO1)); expect(bigquery.getOptions()).andReturn(mockOptions); expect(bigquery.getTable(TABLE_INFO1.getTableId())).andReturn(expectedTable); replay(bigquery); initializeDataset(); Table table = dataset.get(TABLE_INFO1.getTableId().getTable()); assertNotNull(table); assertEquals(expectedTable, table); }
@Test public void testGetTableFromTableIdWithProject() { TableInfo tableInfo = TABLE_INFO.setProjectId(OTHER_PROJECT); TableId tableId = TABLE_ID.setProjectId(OTHER_PROJECT); EasyMock.expect(bigqueryRpcMock.getTable(OTHER_PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) .andReturn(tableInfo.toPb()); EasyMock.replay(bigqueryRpcMock); BigQueryOptions bigQueryOptions = createBigQueryOptionsForProject(OTHER_PROJECT, rpcFactoryMock); bigquery = bigQueryOptions.getService(); Table table = bigquery.getTable(tableId); assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(tableInfo)), table); }
@Test public void testGetWithOptions() throws Exception { initializeExpectedDataset(2); Table expectedTable = new Table(serviceMockReturnsOptions, new TableInfo.BuilderImpl(TABLE_INFO1)); expect(bigquery.getOptions()).andReturn(mockOptions); expect(bigquery.getTable(TABLE_INFO1.getTableId(), BigQuery.TableOption.fields())) .andReturn(expectedTable); replay(bigquery); initializeDataset(); Table table = dataset.get(TABLE_INFO1.getTableId().getTable(), BigQuery.TableOption.fields()); assertNotNull(table); assertEquals(expectedTable, table); }
@Test public void testReloadWithOptions() 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(), BigQuery.TableOption.fields())) .andReturn(expectedTable); replay(bigquery); initializeTable(); Table updatedTable = table.reload(BigQuery.TableOption.fields()); compareTable(expectedTable, updatedTable); }
@Test public void testGetTableWithSelectedFields() { Capture<Map<BigQueryRpc.Option, Object>> capturedOptions = Capture.newInstance(); EasyMock.expect( bigqueryRpcMock.getTable(eq(PROJECT), eq(DATASET), eq(TABLE), capture(capturedOptions))) .andReturn(TABLE_INFO_WITH_PROJECT.toPb()); EasyMock.replay(bigqueryRpcMock); bigquery = options.getService(); Table table = bigquery.getTable(TABLE_ID, 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(TABLE_INFO_WITH_PROJECT)), table); }
@Test public void testGetTableFromTableIdWithoutProject() { TableInfo tableInfo = TABLE_INFO.setProjectId(PROJECT); TableId tableId = TableId.of("", TABLE_ID.getDataset(), TABLE_ID.getTable()); EasyMock.expect(bigqueryRpcMock.getTable(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) .andReturn(tableInfo.toPb()); EasyMock.replay(bigqueryRpcMock); BigQueryOptions bigQueryOptions = createBigQueryOptionsForProject(PROJECT, rpcFactoryMock); bigquery = bigQueryOptions.getService(); Table table = bigquery.getTable(tableId); assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(tableInfo)), table); }