@Override TableId parse(String... args) throws Exception { String message; if (args.length == 2) { return TableId.of(args[0], args[1]); } else if (args.length < 2) { message = "Missing required dataset and table id."; } else { message = "Too many arguments."; } throw new IllegalArgumentException(message); }
@Override public TableResult listTableData( String datasetId, String tableId, TableDataListOption... options) { return listTableData(TableId.of(datasetId, tableId), options); }
@Override public TableResult listTableData( String datasetId, String tableId, Schema schema, TableDataListOption... options) { return listTableData(TableId.of(datasetId, tableId), schema, options); }
/** * Returns a builder for an {@code InsertAllRequest} object given the destination table and the * rows to insert. */ public static Builder newBuilder(String datasetId, String tableId, RowToInsert... rows) { return newBuilder(TableId.of(datasetId, tableId), rows); }
/** 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; }
/** * Returns a builder for an {@code InsertAllRequest} object given the destination table and the * rows to insert. */ public static Builder newBuilder(String datasetId, String tableId, Iterable<RowToInsert> rows) { return newBuilder(TableId.of(datasetId, tableId), rows); }
@Override public boolean delete(String datasetId, String tableId) { return delete(TableId.of(datasetId, tableId)); }
TableId setProjectId(String projectId) { Preconditions.checkArgument( !Strings.isNullOrEmpty(projectId), "Provided projectId is null or empty"); return TableId.of(projectId, getDataset(), getTable()); }
/** Returns a builder for an {@code InsertAllRequest} object given the destination table. */ public static Builder newBuilder(String datasetId, String tableId) { return new Builder().setTable(TableId.of(datasetId, tableId)); }
@Override TableInfo parse(String... args) throws Exception { if (args.length >= 3) { String dataset = args[0]; String table = args[1]; TableId tableId = TableId.of(dataset, table); return TableInfo.of(tableId, StandardTableDefinition.of(parseSchema(args, 2, args.length))); } throw new IllegalArgumentException("Missing required arguments."); }
@Test public void testSetProjectId() { TableId differentProjectTable = TableId.of("differentProject", "dataset", "table"); assertEquals(differentProjectTable, TABLE.setProjectId("differentProject")); }
@Test public void testListTableDataFromTableId() { EasyMock.expect(bigqueryRpcMock.listTableData(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS)) .andReturn(TABLE_DATA_PB); EasyMock.replay(bigqueryRpcMock); bigquery = options.getService(); Page<FieldValueList> page = bigquery.listTableData(TableId.of(DATASET, TABLE)); assertEquals(CURSOR, page.getNextPageToken()); assertArrayEquals(TABLE_DATA.toArray(), Iterables.toArray(page.getValues(), List.class)); }
@Test public void testDeleteTableFromTableIdWithoutProject() { TableId tableId = TableId.of("", TABLE_ID.getDataset(), TABLE_ID.getTable()); EasyMock.expect(bigqueryRpcMock.deleteTable(PROJECT, DATASET, TABLE)).andReturn(true); EasyMock.replay(bigqueryRpcMock); BigQueryOptions bigQueryOptions = createBigQueryOptionsForProject(PROJECT, rpcFactoryMock); bigquery = bigQueryOptions.getService(); assertTrue(bigquery.delete(tableId)); }
@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); }
@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); }
@Test public void testViewEntity() { TableId viewId = TableId.of("project", "dataset", "view"); View entity = new View(viewId); assertEquals(viewId, entity.getId()); assertEquals(Type.VIEW, entity.getType()); Dataset.Access pb = entity.toPb(); assertEquals(entity, Entity.fromPb(pb)); }
@Test public void testToBuilder() { compareExtractJobConfiguration( EXTRACT_CONFIGURATION, EXTRACT_CONFIGURATION.toBuilder().build()); ExtractJobConfiguration job = EXTRACT_CONFIGURATION.toBuilder().setSourceTable(TableId.of("dataset", "newTable")).build(); assertEquals("newTable", job.getSourceTable().getTable()); job = job.toBuilder().setSourceTable(TABLE_ID).build(); compareExtractJobConfiguration(EXTRACT_CONFIGURATION, job); }
@Test public void testToBuilder() { compareLoadConfiguration(LOAD_CONFIGURATION_CSV, LOAD_CONFIGURATION_CSV.toBuilder().build()); WriteChannelConfiguration configuration = LOAD_CONFIGURATION_CSV .toBuilder() .setDestinationTable(TableId.of("dataset", "newTable")) .build(); assertEquals("newTable", configuration.getDestinationTable().getTable()); configuration = configuration.toBuilder().setDestinationTable(TABLE_ID).build(); compareLoadConfiguration(LOAD_CONFIGURATION_CSV, configuration); }
@Test public void testOf() { Acl acl = Acl.of(Group.ofAllAuthenticatedUsers(), Role.READER); assertEquals(Group.ofAllAuthenticatedUsers(), acl.getEntity()); assertEquals(Role.READER, acl.getRole()); Dataset.Access pb = acl.toPb(); assertEquals(acl, Acl.fromPb(pb)); View view = new View(TableId.of("project", "dataset", "view")); acl = Acl.of(view); assertEquals(view, acl.getEntity()); assertEquals(null, acl.getRole()); } }