Refine search
/** Example of getting a dataset. */ // [TARGET getDataset(DatasetId, DatasetOption...)] // [VARIABLE "my_project_id"] // [VARIABLE "my_dataset_name"] public Dataset getDatasetFromId(String projectId, String datasetName) { // [START bigquery_get_dataset] DatasetId datasetId = DatasetId.of(projectId, datasetName); Dataset dataset = bigquery.getDataset(datasetId); // [END bigquery_get_dataset] return dataset; }
@Test public void testReloadNull() throws Exception { initializeExpectedDataset(1); expect(bigquery.getOptions()).andReturn(mockOptions); expect(bigquery.getDataset(DATASET_INFO.getDatasetId().getDataset())).andReturn(null); replay(bigquery); initializeDataset(); assertNull(dataset.reload()); }
DatasetId setProjectId(String projectId) { return getProject() != null ? this : DatasetId.of(projectId, getDataset()); }
@Override public Boolean call() { return bigQueryRpc.deleteDataset( completeDatasetId.getProject(), completeDatasetId.getDataset(), optionsMap); } },
private void compareDatasetIds(DatasetId expected, DatasetId value) { assertEquals(expected, value); assertEquals(expected.getProject(), value.getProject()); assertEquals(expected.getDataset(), value.getDataset()); assertEquals(expected.hashCode(), value.hashCode()); } }
bigquery.create( DatasetInfo.newBuilder("locationset_" + UUID.randomUUID().toString().replace("-", "_")) .setLocation(location) .build()); try { TableId tableId = TableId.of(dataset.getDatasetId().getDataset(), "sometable"); Schema schema = Schema.of(Field.of("name", LegacySQLTypeName.STRING)); TableDefinition tableDef = StandardTableDefinition.of(schema); Table table = bigquery.create(TableInfo.newBuilder(tableId, tableDef).build()); bigquery.create( JobInfo.of( JobId.newBuilder().setLocation(location).build(),
@Test public void testUpdateDataset() { Dataset dataset = bigquery.create( DatasetInfo.newBuilder(OTHER_DATASET) .setDescription("Some Description") .setLabels(Collections.singletonMap("a", "b")) .build()); assertThat(dataset).isNotNull(); assertThat(dataset.getDatasetId().getProject()).isEqualTo(bigquery.getOptions().getProjectId()); assertThat(dataset.getDatasetId().getDataset()).isEqualTo(OTHER_DATASET); assertThat(dataset.getDescription()).isEqualTo("Some Description"); assertThat(dataset.getLabels()).containsExactly("a", "b"); Map<String, String> updateLabels = new HashMap<>(); updateLabels.put("x", "y"); updateLabels.put("a", null); Dataset updatedDataset = bigquery.update( dataset .toBuilder() .setDescription("Updated Description") .setLabels(updateLabels) .build()); assertThat(updatedDataset.getDescription()).isEqualTo("Updated Description"); assertThat(updatedDataset.getLabels()).containsExactly("x", "y"); updatedDataset = bigquery.update(updatedDataset.toBuilder().setLabels(null).build()); assertThat(updatedDataset.getLabels()).isEmpty(); assertThat(dataset.delete()).isTrue(); }
QueryJobConfiguration configuration = QueryJobConfiguration.newBuilder(query) .setDefaultDataset(DatasetId.of(DATASET)) .setDestinationTable(destinationTable) .build(); Job remoteJob = bigquery.create(JobInfo.of(configuration)); remoteJob = remoteJob.waitFor(); assertNull(remoteJob.getStatus().getError()); assertTrue(bigquery.delete(DATASET, tableName)); Job queryJob = bigquery.getJob(remoteJob.getJobId()); JobStatistics.QueryStatistics statistics = queryJob.getStatistics(); assertNotNull(statistics.getQueryPlan());
@Test public void testCancelJob() throws InterruptedException, TimeoutException { String destinationTableName = "test_cancel_query_job_table"; String query = "SELECT TimestampField, StringField, BooleanField FROM " + TABLE_ID.getTable(); TableId destinationTable = TableId.of(DATASET, destinationTableName); QueryJobConfiguration configuration = QueryJobConfiguration.newBuilder(query) .setDefaultDataset(DatasetId.of(DATASET)) .setDestinationTable(destinationTable) .build(); Job remoteJob = bigquery.create(JobInfo.of(configuration)); assertTrue(remoteJob.cancel()); remoteJob = remoteJob.waitFor(); assertNull(remoteJob.getStatus().getError()); }
/** Example of deleting a dataset, even if non-empty. */ // [TARGET delete(DatasetId, DatasetDeleteOption...)] // [VARIABLE "my_project_id"] // [VARIABLE "my_dataset_name"] public boolean deleteDatasetFromId(String projectId, String datasetName) { // [START bigquery_delete_dataset] DatasetId datasetId = DatasetId.of(projectId, datasetName); boolean deleted = bigquery.delete(datasetId, DatasetDeleteOption.deleteContents()); if (deleted) { // the dataset was deleted } else { // the dataset was not found } // [END bigquery_delete_dataset] return deleted; }
@Test public void testQuery() throws InterruptedException { String query = "SELECT TimestampField, StringField, BooleanField FROM " + TABLE_ID.getTable(); QueryJobConfiguration config = QueryJobConfiguration.newBuilder(query).setDefaultDataset(DatasetId.of(DATASET)).build(); Job job = bigquery.create(JobInfo.of(JobId.of(), config)); TableResult result = job.getQueryResults(); assertEquals(QUERY_RESULT_SCHEMA, result.getSchema()); int rowCount = 0; for (FieldValueList row : result.getValues()) { FieldValue timestampCell = row.get(0); assertEquals(timestampCell, row.get("TimestampField")); FieldValue stringCell = row.get(1); assertEquals(stringCell, row.get("StringField")); FieldValue booleanCell = row.get(2); assertEquals(booleanCell, row.get("BooleanField")); assertEquals(FieldValue.Attribute.PRIMITIVE, timestampCell.getAttribute()); assertEquals(FieldValue.Attribute.PRIMITIVE, stringCell.getAttribute()); assertEquals(FieldValue.Attribute.PRIMITIVE, booleanCell.getAttribute()); assertEquals(1408452095220000L, timestampCell.getTimestampValue()); assertEquals("stringValue", stringCell.getStringValue()); assertEquals(false, booleanCell.getBooleanValue()); rowCount++; } assertEquals(2, rowCount); Job job2 = bigquery.getJob(job.getJobId()); JobStatistics.QueryStatistics statistics = job2.getStatistics(); assertNotNull(statistics.getQueryPlan()); }
@Test public void testGetDataset() { Dataset dataset = bigquery.getDataset(DATASET); assertEquals(bigquery.getOptions().getProjectId(), dataset.getDatasetId().getProject()); assertEquals(DATASET, dataset.getDatasetId().getDataset()); assertEquals(DESCRIPTION, dataset.getDescription()); assertEquals(LABELS, dataset.getLabels()); assertNotNull(dataset.getAcl()); assertNotNull(dataset.getEtag()); assertNotNull(dataset.getGeneratedId()); assertNotNull(dataset.getLastModified()); assertNotNull(dataset.getSelfLink()); }
/** * Creates a new table in this dataset. * * <p>Example of creating a table in the dataset with schema and time partitioning. * * <pre>{@code * String tableName = “my_table”; * String fieldName = “my_field”; * Schema schema = Schema.of(Field.of(fieldName, LegacySQLTypeName.STRING)); * StandardTableDefinition definition = StandardTableDefinition.newBuilder() * .setSchema(schema) * .setTimePartitioning(TimePartitioning.of(TimePartitioning.Type.DAY)) * .build(); * Table table = dataset.create(tableName, definition); * }</pre> * * @param tableId the table's user-defined id * @param definition the table's definition * @param options options for table creation * @return a {@code Table} object for the created table * @throws BigQueryException upon failure */ public Table create(String tableId, TableDefinition definition, TableOption... options) { TableInfo tableInfo = TableInfo.of(TableId.of(getDatasetId().getDataset(), tableId), definition); return bigquery.create(tableInfo, options); }
/** Example of listing the tables in a dataset. */ // [TARGET listTables(DatasetId, TableListOption...)] // [VARIABLE "my_project_id"] // [VARIABLE "my_dataset_name"] public Page<Table> listTablesFromId(String projectId, String datasetName) { // [START bigquery_list_tables] DatasetId datasetId = DatasetId.of(projectId, datasetName); Page<Table> tables = bigquery.listTables(datasetId, TableListOption.pageSize(100)); for (Table table : tables.iterateAll()) { // do something with the table } // [END bigquery_list_tables] return tables; }
/** * 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); }
@Override DatasetId parse(String... args) throws Exception { String message; if (args.length == 1) { return DatasetId.of(args[0]); } else if (args.length > 1) { message = "Too many arguments."; } else { message = "Missing required dataset id."; } throw new IllegalArgumentException(message); }
@Test public void testListDatasets() { Page<Dataset> datasets = bigquery.listDatasets("bigquery-public-data"); Iterator<Dataset> iterator = datasets.iterateAll().iterator(); Set<String> datasetNames = new HashSet<>(); while (iterator.hasNext()) { datasetNames.add(iterator.next().getDatasetId().getDataset()); } for (String type : PUBLIC_DATASETS) { assertTrue(datasetNames.contains(type)); } }
@Test public void testNamedQueryParameters() throws InterruptedException { String query = "SELECT TimestampField, StringField, BooleanField FROM " + TABLE_ID.getTable() + " WHERE StringField = @stringParam" + " AND IntegerField IN UNNEST(@integerList)"; QueryParameterValue stringParameter = QueryParameterValue.string("stringValue"); QueryParameterValue intArrayParameter = QueryParameterValue.array(new Integer[] {3, 4}, Integer.class); QueryJobConfiguration config = QueryJobConfiguration.newBuilder(query) .setDefaultDataset(DatasetId.of(DATASET)) .setUseLegacySql(false) .addNamedParameter("stringParam", stringParameter) .addNamedParameter("integerList", intArrayParameter) .build(); TableResult result = bigquery.query(config); assertEquals(QUERY_RESULT_SCHEMA, result.getSchema()); assertEquals(2, Iterables.size(result.getValues())); }
/** * Fetches current dataset's latest information. Returns {@code null} if the dataset does not * exist. * * <p>Example of reloading a dataset. * * <pre>{@code * Dataset latestDataset = dataset.reload(); * if (latestDataset == null) { * // The dataset was not found * } * }</pre> * * @param options dataset options * @return a {@code Dataset} object with latest information or {@code null} if not found * @throws BigQueryException upon failure */ public Dataset reload(DatasetOption... options) { return bigquery.getDataset(getDatasetId().getDataset(), options); }