Example of creating a dataset. * - *
{@code - * String datasetName = "my_dataset_name"; - * Dataset dataset = null; - * DatasetInfo datasetInfo = DatasetInfo.newBuilder(datasetName).build(); - * try { - * // the dataset was created - * dataset = bigquery.create(datasetInfo); - * } catch (BigQueryException e) { - * // the dataset was not created + ** * @throws BigQueryException upon failure */ @@ -664,19 +682,22 @@ public int hashCode() { * *+ * { + * @code + * String datasetName = "my_dataset_name"; + * Dataset dataset = null; + * DatasetInfo datasetInfo = DatasetInfo.newBuilder(datasetName).build(); + * try { + * // the dataset was created + * dataset = bigquery.create(datasetInfo); + * } catch (BigQueryException e) { + * // the dataset was not created + * } * } - * }+ *
Example of creating a table. * - *
{@code - * String datasetName = "my_dataset_name"; - * String tableName = "my_table_name"; - * String fieldName = "string_field"; - * 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); - * }+ *
+ * { + * @code + * String datasetName = "my_dataset_name"; + * String tableName = "my_table_name"; + * String fieldName = "string_field"; + * 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); + * } + ** * @throws BigQueryException upon failure */ @@ -694,46 +715,46 @@ public int hashCode() { * *
Example of loading a newline-delimited-json file with textual fields from GCS to a table. * - *
{@code - * String datasetName = "my_dataset_name"; - * String tableName = "my_table_name"; - * String sourceUri = "gs://cloud-samples-data/bigquery/us-states/us-states.json"; - * TableId tableId = TableId.of(datasetName, tableName); - * // Table field definition - * Field[] fields = - * new Field[] { - * Field.of("name", LegacySQLTypeName.STRING), - * Field.of("post_abbr", LegacySQLTypeName.STRING) - * }; - * // Table schema definition - * Schema schema = Schema.of(fields); - * LoadJobConfiguration configuration = - * LoadJobConfiguration.builder(tableId, sourceUri) - * .setFormatOptions(FormatOptions.json()) - * .setCreateDisposition(CreateDisposition.CREATE_IF_NEEDED) - * .setSchema(schema) - * .build(); - * // Load the table - * Job loadJob = bigquery.create(JobInfo.of(configuration)); - * loadJob = loadJob.waitFor(); - * // Check the table - * System.out.println("State: " + loadJob.getStatus().getState()); - * return ((StandardTableDefinition) bigquery.getTable(tableId).getDefinition()).getNumRows(); - * }+ *
+ * { + * @code + * String datasetName = "my_dataset_name"; + * String tableName = "my_table_name"; + * String sourceUri = "gs://cloud-samples-data/bigquery/us-states/us-states.json"; + * TableId tableId = TableId.of(datasetName, tableName); + * // Table field definition + * Field[] fields = new Field[] { Field.of("name", LegacySQLTypeName.STRING), + * Field.of("post_abbr", LegacySQLTypeName.STRING) }; + * // Table schema definition + * Schema schema = Schema.of(fields); + * LoadJobConfiguration configuration = LoadJobConfiguration.builder(tableId, sourceUri) + * .setFormatOptions(FormatOptions.json()).setCreateDisposition(CreateDisposition.CREATE_IF_NEEDED) + * .setSchema(schema).build(); + * // Load the table + * Job loadJob = bigquery.create(JobInfo.of(configuration)); + * loadJob = loadJob.waitFor(); + * // Check the table + * System.out.println("State: " + loadJob.getStatus().getState()); + * return ((StandardTableDefinition) bigquery.getTable(tableId).getDefinition()).getNumRows(); + * } + ** *
Example of creating a query job. * - *
{@code - * String query = "SELECT field FROM my_dataset_name.my_table_name"; - * Job job = null; - * JobConfiguration jobConfiguration = QueryJobConfiguration.of(query); - * JobInfo jobInfo = JobInfo.of(jobConfiguration); - * try { - * job = bigquery.create(jobInfo); - * } catch (BigQueryException e) { - * // the job was not created + ** * @throws BigQueryException upon failure */ @@ -744,10 +765,13 @@ public int hashCode() { * *+ * { + * @code + * String query = "SELECT field FROM my_dataset_name.my_table_name"; + * Job job = null; + * JobConfiguration jobConfiguration = QueryJobConfiguration.of(query); + * JobInfo jobInfo = JobInfo.of(jobConfiguration); + * try { + * job = bigquery.create(jobInfo); + * } catch (BigQueryException e) { + * // the job was not created + * } * } - * }+ *
Example of getting a dataset. * - *
{@code - * String datasetName = "my_dataset"; - * Dataset dataset = bigquery.getDataset(datasetName); - * }+ *
+ * { + * @code + * String datasetName = "my_dataset"; + * Dataset dataset = bigquery.getDataset(datasetName); + * } + ** * @throws BigQueryException upon failure */ @@ -758,12 +782,15 @@ public int hashCode() { * *
Example of getting a dataset. * - *
{@code - * String projectId = "my_project_id"; - * String datasetName = "my_dataset_name"; - * DatasetId datasetId = DatasetId.of(projectId, datasetName); - * Dataset dataset = bigquery.getDataset(datasetId); - * }+ *
+ * { + * @code + * String projectId = "my_project_id"; + * String datasetName = "my_dataset_name"; + * DatasetId datasetId = DatasetId.of(projectId, datasetName); + * Dataset dataset = bigquery.getDataset(datasetId); + * } + ** * @throws BigQueryException upon failure */ @@ -777,13 +804,16 @@ public int hashCode() { * *
Example of listing datasets, specifying the page size. * - *
{@code - * // List datasets in the default project - * Page* * @throws BigQueryException upon failure */ @@ -797,14 +827,17 @@ public int hashCode() { * *datasets = bigquery.listDatasets(DatasetListOption.pageSize(100)); - * for (Dataset dataset : datasets.iterateAll()) { - * // do something with the dataset + * + * { + * @code + * // List datasets in the default project + * Page+ *datasets = bigquery.listDatasets(DatasetListOption.pageSize(100)); + * for (Dataset dataset : datasets.iterateAll()) { + * // do something with the dataset + * } * } - * }
Example of listing datasets in a project, specifying the page size. * - *
{@code - * String projectId = "my_project_id"; - * // List datasets in a specified project - * Page* * @throws BigQueryException upon failure */ @@ -815,15 +848,18 @@ public int hashCode() { * *datasets = bigquery.listDatasets(projectId, DatasetListOption.pageSize(100)); - * for (Dataset dataset : datasets.iterateAll()) { - * // do something with the dataset + * + * { + * @code + * String projectId = "my_project_id"; + * // List datasets in a specified project + * Page+ *datasets = bigquery.listDatasets(projectId, DatasetListOption.pageSize(100)); + * for (Dataset dataset : datasets.iterateAll()) { + * // do something with the dataset + * } * } - * }
Example of deleting a dataset from its id, even if non-empty. * - *
{@code - * String datasetName = "my_dataset_name"; - * boolean deleted = bigquery.delete(datasetName, DatasetDeleteOption.deleteContents()); - * if (deleted) { - * // the dataset was deleted - * } else { - * // the dataset was not found + ** * @return {@code true} if dataset was deleted, {@code false} if it was not found * @throws BigQueryException upon failure @@ -835,17 +871,20 @@ public int hashCode() { * *+ * { + * @code + * String datasetName = "my_dataset_name"; + * boolean deleted = bigquery.delete(datasetName, DatasetDeleteOption.deleteContents()); + * if (deleted) { + * // the dataset was deleted + * } else { + * // the dataset was not found + * } * } - * }+ *
Example of deleting a dataset, even if non-empty. * - *
{@code - * String projectId = "my_project_id"; - * String datasetName = "my_dataset_name"; - * 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 + ** * @return {@code true} if dataset was deleted, {@code false} if it was not found * @throws BigQueryException upon failure @@ -869,18 +908,21 @@ public int hashCode() { * *+ * { + * @code + * String projectId = "my_project_id"; + * String datasetName = "my_dataset_name"; + * 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 + * } * } - * }+ *
Example of deleting a table. * - *
{@code - * String projectId = "my_project_id"; - * String datasetName = "my_dataset_name"; - * String tableName = "my_table_name"; - * TableId tableId = TableId.of(projectId, datasetName, tableName); - * boolean deleted = bigquery.delete(tableId); - * if (deleted) { - * // the table was deleted - * } else { - * // the table was not found + ** * @return {@code true} if table was deleted, {@code false} if it was not found * @throws BigQueryException upon failure @@ -892,18 +934,21 @@ public int hashCode() { * *+ * { + * @code + * String projectId = "my_project_id"; + * String datasetName = "my_dataset_name"; + * String tableName = "my_table_name"; + * TableId tableId = TableId.of(projectId, datasetName, tableName); + * boolean deleted = bigquery.delete(tableId); + * if (deleted) { + * // the table was deleted + * } else { + * // the table was not found + * } * } - * }+ *
Example of deleting a model. * - *
{@code - * String projectId = "my_project_id"; - * String datasetName = "my_dataset_name"; - * String tableName = "my_model_name"; - * ModelId modelId = ModelId.of(projectId, datasetName, modelName); - * boolean deleted = bigquery.delete(modelId); - * if (deleted) { - * // the model was deleted - * } else { - * // the model was not found + ** * @return {@code true} if model was deleted, {@code false} if it was not found * @throws BigQueryException upon failure @@ -913,9 +958,11 @@ public int hashCode() { /** * Deletes the requested routine. * - *+ * { + * @code + * String projectId = "my_project_id"; + * String datasetName = "my_dataset_name"; + * String tableName = "my_model_name"; + * ModelId modelId = ModelId.of(projectId, datasetName, modelName); + * boolean deleted = bigquery.delete(modelId); + * if (deleted) { + * // the model was deleted + * } else { + * // the model was not found + * } * } - * }+ *
Example of deleting a routine. + *
+ * Example of deleting a routine. * - *
{@code + *+ * {@code * String projectId = "my_project_id"; * String datasetId = "my_dataset_id"; * String routineId = "my_routine_id"; @@ -928,7 +975,9 @@ public int hashCode() { * } ** - * @return {@code true} if routine was deleted, {@code false} if it was not found + * @return {@code true} if routine was deleted, {@code false} if it was not + * found + * * @throws BigQueryException upon failure */ boolean delete(RoutineId routineId); @@ -937,20 +986,22 @@ public int hashCode() { * Updates dataset information. * *Example of updating a dataset by changing its description. - * + * * - *
{@code - * // String datasetName = "my_dataset_name"; - * // String tableName = "my_table_name"; - * // String newDescription = "new_description"; + ** * * @@ -963,31 +1014,33 @@ public int hashCode() { * *+ * { + * @code + * // 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); + * Table beforeTable = bigquery.getTable(datasetName, tableName); + * TableInfo tableInfo = beforeTable.toBuilder().setDescription(newDescription).build(); + * Table afterTable = bigquery.update(tableInfo); * - * }+ * } + *Example of updating a table by changing its description. * - *
{@code - * 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); - * }+ *+ * { + * @code + * 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); + * } + ** *Example of updating a table by changing its expiration. * - *
{@code - * String datasetName = "my_dataset_name"; - * String tableName = "my_table_name"; - * 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); - * }+ *+ * { + * @code + * String datasetName = "my_dataset_name"; + * String tableName = "my_table_name"; + * 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); + * } + ** * @throws BigQueryException upon failure */ @@ -998,31 +1051,33 @@ public int hashCode() { * *Example of updating a model by changing its description. * - *
{@code - * String datasetName = "my_dataset_name"; - * String modelName = "my_model_name"; - * String newDescription = "new_description"; - * Model beforeModel = bigquery.getModel(datasetName, modelName); - * ModelInfo modelInfo = beforeModel.toBuilder() - * .setDescription(newDescription) - * .build(); - * Model afterModel = bigquery.update(modelInfo); - * }+ *+ * { + * @code + * String datasetName = "my_dataset_name"; + * String modelName = "my_model_name"; + * String newDescription = "new_description"; + * Model beforeModel = bigquery.getModel(datasetName, modelName); + * ModelInfo modelInfo = beforeModel.toBuilder().setDescription(newDescription).build(); + * Model afterModel = bigquery.update(modelInfo); + * } + ** *Example of updating a model by changing its expiration. * - *
{@code - * String datasetName = "my_dataset_name"; - * String modelName = "my_model_name"; - * Model beforeModel = bigquery.getModel(datasetName, modelName); - * - * // Set model to expire 5 days from now. - * long expirationMillis = DateTime.now().plusDays(5).getMillis(); - * ModelInfo modelInfo = beforeModel.toBuilder() - * .setExpirationTime(expirationMillis) - * .build(); - * Model afterModel = bigquery.update(modelInfo); - * }+ *+ * { + * @code + * String datasetName = "my_dataset_name"; + * String modelName = "my_model_name"; + * Model beforeModel = bigquery.getModel(datasetName, modelName); + * + * // Set model to expire 5 days from now. + * long expirationMillis = DateTime.now().plusDays(5).getMillis(); + * ModelInfo modelInfo = beforeModel.toBuilder().setExpirationTime(expirationMillis).build(); + * Model afterModel = bigquery.update(modelInfo); + * } + ** * @throws BigQueryException upon failure */ @@ -1040,11 +1095,14 @@ public int hashCode() { * *Example of getting a table. * - *
{@code - * String datasetName = "my_dataset_name"; - * String tableName = "my_table_name"; - * Table table = bigquery.getTable(datasetName, tableName); - * }+ *+ * { + * @code + * String datasetName = "my_dataset_name"; + * String tableName = "my_table_name"; + * Table table = bigquery.getTable(datasetName, tableName); + * } + ** * @throws BigQueryException upon failure */ @@ -1055,13 +1113,16 @@ public int hashCode() { * *Example of getting a table. * - *
{@code - * String projectId = "my_project_id"; - * String datasetName = "my_dataset_name"; - * String tableName = "my_table_name"; - * TableId tableId = TableId.of(projectId, datasetName, tableName); - * Table table = bigquery.getTable(tableId); - * }+ *+ * { + * @code + * String projectId = "my_project_id"; + * String datasetName = "my_dataset_name"; + * String tableName = "my_table_name"; + * TableId tableId = TableId.of(projectId, datasetName, tableName); + * Table table = bigquery.getTable(tableId); + * } + ** * @throws BigQueryException upon failure */ @@ -1079,13 +1140,16 @@ public int hashCode() { * *Example of getting a model. * - *
{@code - * String projectId = "my_project_id"; - * String datasetName = "my_dataset_name"; - * String modelName = "my_model_name"; - * ModelId modelId = ModelId.of(projectId, datasetName, tableName); - * Model model = bigquery.getModel(modelId); - * }+ *+ * { + * @code + * String projectId = "my_project_id"; + * String datasetName = "my_dataset_name"; + * String modelName = "my_model_name"; + * ModelId modelId = ModelId.of(projectId, datasetName, tableName); + * Model model = bigquery.getModel(modelId); + * } + ** * @throws BigQueryException upon failure */ @@ -1112,42 +1176,54 @@ public int hashCode() { PagelistRoutines(DatasetId datasetId, RoutineListOption... options); /** - * Lists the tables in the dataset. This method returns partial information on each table: ({@link - * Table#getTableId()}, {@link Table#getFriendlyName()}, {@link Table#getGeneratedId()} and type, - * which is part of {@link Table#getDefinition()}). To get complete information use either {@link - * #getTable(TableId, TableOption...)} or {@link #getTable(String, String, TableOption...)}. - * - * Example of listing the tables in a dataset, specifying the page size. - * - *
{@code - * String datasetName = "my_dataset_name"; - * Pagetables = bigquery.listTables(datasetName, TableListOption.pageSize(100)); - * for (Table table : tables.iterateAll()) { - * // do something with the table + * Lists the tables in the dataset. This method returns partial information on + * each table: ({@link Table#getTableId()}, {@link Table#getFriendlyName()}, + * {@link Table#getGeneratedId()} and type, which is part of + * {@link Table#getDefinition()}). To get complete information use either + * {@link #getTable(TableId, TableOption...)} or + * {@link #getTable(String, String, TableOption...)}. + * + *
+ * Example of listing the tables in a dataset, specifying the page size. + * + *
+ * { + * @code + * String datasetName = "my_dataset_name"; + * Pagetables = bigquery.listTables(datasetName, TableListOption.pageSize(100)); + * for (Table table : tables.iterateAll()) { + * // do something with the table + * } * } - * } + * * * @throws BigQueryException upon failure */ Page
listTables(String datasetId, TableListOption... options); /** - * Lists the tables in the dataset. This method returns partial information on each table: ({@link - * Table#getTableId()}, {@link Table#getFriendlyName()}, {@link Table#getGeneratedId()} and type, - * which is part of {@link Table#getDefinition()}). To get complete information use either {@link - * #getTable(TableId, TableOption...)} or {@link #getTable(String, String, TableOption...)}. - * - *
Example of listing the tables in a dataset. - * - *
{@code - * String projectId = "my_project_id"; - * String datasetName = "my_dataset_name"; - * DatasetId datasetId = DatasetId.of(projectId, datasetName); - * Pagetables = bigquery.listTables(datasetId, TableListOption.pageSize(100)); - * for (Table table : tables.iterateAll()) { - * // do something with the table + * Lists the tables in the dataset. This method returns partial information on + * each table: ({@link Table#getTableId()}, {@link Table#getFriendlyName()}, + * {@link Table#getGeneratedId()} and type, which is part of + * {@link Table#getDefinition()}). To get complete information use either + * {@link #getTable(TableId, TableOption...)} or + * {@link #getTable(String, String, TableOption...)}. + * + *
+ * Example of listing the tables in a dataset. + * + *
+ * { + * @code + * String projectId = "my_project_id"; + * String datasetName = "my_dataset_name"; + * DatasetId datasetId = DatasetId.of(projectId, datasetName); + * Pagetables = bigquery.listTables(datasetId, TableListOption.pageSize(100)); + * for (Table table : tables.iterateAll()) { + * // do something with the table + * } * } - * } + * * * @throws BigQueryException upon failure */ @@ -1170,32 +1246,33 @@ public int hashCode() { * *
Example of inserting rows into a table without running a load job. * - *
{@code - * String datasetName = "my_dataset_name"; - * String tableName = "my_table_name"; - * TableId tableId = TableId.of(datasetName, tableName); - * // Values of the row to insert - * Map* * @throws BigQueryException upon failure */ @@ -1206,17 +1283,20 @@ public int hashCode() { * *rowContent = new HashMap<>(); - * rowContent.put("booleanField", true); - * // Bytes are passed in base64 - * rowContent.put("bytesField", "Cg0NDg0="); // 0xA, 0xD, 0xD, 0xE, 0xD in base64 - * // Records are passed as a map - * Map recordsContent = new HashMap<>(); - * recordsContent.put("stringField", "Hello, World!"); - * rowContent.put("recordField", recordsContent); - * InsertAllResponse response = - * bigquery.insertAll( - * InsertAllRequest.newBuilder(tableId) - * .addRow("rowId", rowContent) - * // More rows can be added in the same RPC by invoking .addRow() on the builder - * .build()); - * if (response.hasErrors()) { - * // If any of the insertions failed, this lets you inspect the errors - * for (Entry > entry : response.getInsertErrors().entrySet()) { - * // inspect row error + * + * { + * @code + * String datasetName = "my_dataset_name"; + * String tableName = "my_table_name"; + * TableId tableId = TableId.of(datasetName, tableName); + * // Values of the row to insert + * Map+ *rowContent = new HashMap<>(); + * rowContent.put("booleanField", true); + * // Bytes are passed in base64 + * rowContent.put("bytesField", "Cg0NDg0="); // 0xA, 0xD, 0xD, 0xE, 0xD in base64 + * // Records are passed as a map + * Map recordsContent = new HashMap<>(); + * recordsContent.put("stringField", "Hello, World!"); + * rowContent.put("recordField", recordsContent); + * InsertAllResponse response = bigquery.insertAll(InsertAllRequest.newBuilder(tableId).addRow("rowId", rowContent) + * // More rows can be added in the same RPC by invoking .addRow() on the + * // builder + * .build()); + * if (response.hasErrors()) { + * // If any of the insertions failed, this lets you inspect the errors + * for (Entry > entry : response.getInsertErrors().entrySet()) { + * // inspect row error + * } * } * } - * } Example of listing table rows, specifying the page size. * - *
{@code - * String datasetName = "my_dataset_name"; - * String tableName = "my_table_name"; - * // This example reads the result 100 rows per RPC call. If there's no need to limit the number, - * // simply omit the option. - * TableResult tableData = - * bigquery.listTableData(datasetName, tableName, TableDataListOption.pageSize(100)); - * for (FieldValueList row : tableData.iterateAll()) { - * // do something with the row + ** * @throws BigQueryException upon failure */ @@ -1227,18 +1307,21 @@ public int hashCode() { * *+ * { + * @code + * String datasetName = "my_dataset_name"; + * String tableName = "my_table_name"; + * // This example reads the result 100 rows per RPC call. If there's no need + * // to limit the number, + * // simply omit the option. + * TableResult tableData = bigquery.listTableData(datasetName, tableName, TableDataListOption.pageSize(100)); + * for (FieldValueList row : tableData.iterateAll()) { + * // do something with the row + * } * } - * }+ *Example of listing table rows, specifying the page size. * - *
{@code - * String datasetName = "my_dataset_name"; - * String tableName = "my_table_name"; - * TableId tableIdObject = TableId.of(datasetName, tableName); - * // This example reads the result 100 rows per RPC call. If there's no need to limit the number, - * // simply omit the option. - * TableResult tableData = - * bigquery.listTableData(tableIdObject, TableDataListOption.pageSize(100)); - * for (FieldValueList row : tableData.iterateAll()) { - * // do something with the row + ** * @throws BigQueryException upon failure */ @@ -1272,19 +1355,18 @@ TableResult listTableData( * *+ * { + * @code + * String datasetName = "my_dataset_name"; + * String tableName = "my_table_name"; + * TableId tableIdObject = TableId.of(datasetName, tableName); + * // This example reads the result 100 rows per RPC call. If there's no need + * // to limit the number, + * // simply omit the option. + * TableResult tableData = bigquery.listTableData(tableIdObject, TableDataListOption.pageSize(100)); + * for (FieldValueList row : tableData.iterateAll()) { + * // do something with the row + * } * } - * }+ *Example of listing table rows with schema. * - *
{@code - * Schema schema = - * Schema.of( - * Field.of("word", LegacySQLTypeName.STRING), - * Field.of("word_count", LegacySQLTypeName.STRING), - * Field.of("corpus", LegacySQLTypeName.STRING), - * Field.of("corpus_date", LegacySQLTypeName.STRING)); - * TableResult tableData = - * bigquery.listTableData( - * TableId.of("bigquery-public-data", "samples", "shakespeare"), schema); - * FieldValueList row = tableData.getValues().iterator().next(); - * System.out.println(row.get("word").getStringValue()); - * }+ *+ * { + * @code + * Schema schema = Schema.of(Field.of("word", LegacySQLTypeName.STRING), + * Field.of("word_count", LegacySQLTypeName.STRING), Field.of("corpus", LegacySQLTypeName.STRING), + * Field.of("corpus_date", LegacySQLTypeName.STRING)); + * TableResult tableData = bigquery.listTableData(TableId.of("bigquery-public-data", "samples", "shakespeare"), + * schema); + * FieldValueList row = tableData.getValues().iterator().next(); + * System.out.println(row.get("word").getStringValue()); + * } + ** * @throws BigQueryException upon failure */ @@ -1296,13 +1378,16 @@ TableResult listTableData( * *Example of getting a job. * - *
{@code - * String jobName = "my_job_name"; - * Job job = bigquery.getJob(jobName); - * if (job == null) { - * // job was not found + ** * @throws BigQueryException upon failure */ @@ -1314,14 +1399,17 @@ TableResult listTableData( * *+ * { + * @code + * String jobName = "my_job_name"; + * Job job = bigquery.getJob(jobName); + * if (job == null) { + * // job was not found + * } * } - * }+ *Example of getting a job. * - *
{@code - * String jobName = "my_job_name"; - * JobId jobIdObject = JobId.of(jobName); - * Job job = bigquery.getJob(jobIdObject); - * if (job == null) { - * // job was not found + ** * @throws BigQueryException upon failure */ @@ -1332,12 +1420,15 @@ TableResult listTableData( * *+ * { + * @code + * String jobName = "my_job_name"; + * JobId jobIdObject = JobId.of(jobName); + * Job job = bigquery.getJob(jobIdObject); + * if (job == null) { + * // job was not found + * } * } - * }+ *Example of listing jobs, specifying the page size. * - *
{@code - * Page* * @throws BigQueryException upon failure */ @@ -1352,15 +1443,18 @@ TableResult listTableData( * *jobs = bigquery.listJobs(JobListOption.pageSize(100)); - * for (Job job : jobs.iterateAll()) { - * // do something with the job + * + * { + * @code + * Page+ *jobs = bigquery.listJobs(JobListOption.pageSize(100)); + * for (Job job : jobs.iterateAll()) { + * // do something with the job + * } * } - * } Example of cancelling a job. * - *
{@code - * String jobName = "my_job_name"; - * boolean success = bigquery.cancel(jobName); - * if (success) { - * // job was cancelled - * } else { - * // job was not found + ** * @return {@code true} if cancel was requested successfully, {@code false} if the job was not * found @@ -1378,16 +1472,19 @@ TableResult listTableData( * *+ * { + * @code + * String jobName = "my_job_name"; + * boolean success = bigquery.cancel(jobName); + * if (success) { + * // job was cancelled + * } else { + * // job was not found + * } * } - * }+ *Example of cancelling a job. * - *
{@code - * String jobName = "my_job_name"; - * JobId jobId = JobId.of(jobName); - * boolean success = bigquery.cancel(jobId); - * if (success) { - * // job was cancelled - * } else { - * // job was not found + ** * @return {@code true} if cancel was requested successfully, {@code false} if the job was not * found @@ -1406,19 +1503,22 @@ TableResult listTableData( * *+ * { + * @code + * String jobName = "my_job_name"; + * JobId jobId = JobId.of(jobName); + * boolean success = bigquery.cancel(jobId); + * if (success) { + * // job was cancelled + * } else { + * // job was not found + * } * } - * }+ *Example of running a query. * - *
{@code - * // BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); - * String query = "SELECT corpus FROM `bigquery-public-data.samples.shakespeare` GROUP BY corpus;"; - * QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder(query).build(); - * - * // Print the results. - * for (FieldValueList row : bigquery.query(queryConfig).iterateAll()) { - * for (FieldValue val : row) { - * System.out.printf("%s,", val.toString()); + ** * @throws BigQueryException upon failure * @throws InterruptedException if the current thread gets interrupted while waiting for the query @@ -1463,50 +1563,56 @@ TableResult query(QueryJobConfiguration configuration, JobId jobId, JobOption... * *+ * { + * @code + * // BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + * String query = "SELECT corpus FROM `bigquery-public-data.samples.shakespeare` GROUP BY corpus;"; + * QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder(query).build(); + * + * // Print the results. + * for (FieldValueList row : bigquery.query(queryConfig).iterateAll()) { + * for (FieldValue val : row) { + * System.out.printf("%s,", val.toString()); + * } + * System.out.printf("\n"); * } - * System.out.printf("\n"); * } - * }+ *Example of creating a channel with which to write to a table. * - *
{@code - * String datasetName = "my_dataset_name"; - * String tableName = "my_table_name"; - * String csvData = "StringValue1\nStringValue2\n"; - * TableId tableId = TableId.of(datasetName, tableName); - * WriteChannelConfiguration writeChannelConfiguration = - * WriteChannelConfiguration.newBuilder(tableId).setFormatOptions(FormatOptions.csv()).build(); - * TableDataWriteChannel writer = bigquery.writer(writeChannelConfiguration); - * // Write data to writer - * try { - * writer.write(ByteBuffer.wrap(csvData.getBytes(Charsets.UTF_8))); - * } finally { - * writer.close(); + ** *+ * { + * @code + * String datasetName = "my_dataset_name"; + * String tableName = "my_table_name"; + * String csvData = "StringValue1\nStringValue2\n"; + * TableId tableId = TableId.of(datasetName, tableName); + * WriteChannelConfiguration writeChannelConfiguration = WriteChannelConfiguration.newBuilder(tableId) + * .setFormatOptions(FormatOptions.csv()).build(); + * TableDataWriteChannel writer = bigquery.writer(writeChannelConfiguration); + * // Write data to writer + * try { + * writer.write(ByteBuffer.wrap(csvData.getBytes(Charsets.UTF_8))); + * } finally { + * writer.close(); + * } + * // Get load job + * Job job = writer.getJob(); + * job = job.waitFor(); + * LoadStatistics stats = job.getStatistics(); + * return stats.getOutputRows(); * } - * // Get load job - * Job job = writer.getJob(); - * job = job.waitFor(); - * LoadStatistics stats = job.getStatistics(); - * return stats.getOutputRows(); - * }+ *Example of writing a local file to a table. * - *
{@code - * String datasetName = "my_dataset_name"; - * String tableName = "my_table_name"; - * Path csvPath = FileSystems.getDefault().getPath(".", "my-data.csv"); - * String location = "us"; - * TableId tableId = TableId.of(datasetName, tableName); - * WriteChannelConfiguration writeChannelConfiguration = - * WriteChannelConfiguration.newBuilder(tableId).setFormatOptions(FormatOptions.csv()).build(); - * // The location must be specified; other fields can be auto-detected. - * JobId jobId = JobId.newBuilder().setLocation(location).build(); - * TableDataWriteChannel writer = bigquery.writer(jobId, writeChannelConfiguration); - * // Write data to writer - * try (OutputStream stream = Channels.newOutputStream(writer)) { - * Files.copy(csvPath, stream); + ** * @throws BigQueryException upon failure */ @@ -1519,29 +1625,44 @@ TableResult query(QueryJobConfiguration configuration, JobId jobId, JobOption... * *+ * { + * @code + * String datasetName = "my_dataset_name"; + * String tableName = "my_table_name"; + * Path csvPath = FileSystems.getDefault().getPath(".", "my-data.csv"); + * String location = "us"; + * TableId tableId = TableId.of(datasetName, tableName); + * WriteChannelConfiguration writeChannelConfiguration = WriteChannelConfiguration.newBuilder(tableId) + * .setFormatOptions(FormatOptions.csv()).build(); + * // The location must be specified; other fields can be auto-detected. + * JobId jobId = JobId.newBuilder().setLocation(location).build(); + * TableDataWriteChannel writer = bigquery.writer(jobId, writeChannelConfiguration); + * // Write data to writer + * try (OutputStream stream = Channels.newOutputStream(writer)) { + * Files.copy(csvPath, stream); + * } + * // Get load job + * Job job = writer.getJob(); + * job = job.waitFor(); + * LoadStatistics stats = job.getStatistics(); + * return stats.getOutputRows(); * } - * // Get load job - * Job job = writer.getJob(); - * job = job.waitFor(); - * LoadStatistics stats = job.getStatistics(); - * return stats.getOutputRows(); - * }+ *Example of creating a channel with which to write to a table. * - *
{@code - * String datasetName = "my_dataset_name"; - * String tableName = "my_table_name"; - * String csvData = "StringValue1\nStringValue2\n"; - * String location = "us"; - * TableId tableId = TableId.of(datasetName, tableName); - * WriteChannelConfiguration writeChannelConfiguration = - * WriteChannelConfiguration.newBuilder(tableId).setFormatOptions(FormatOptions.csv()).build(); - * // The location must be specified; other fields can be auto-detected. - * JobId jobId = JobId.newBuilder().setLocation(location).build(); - * TableDataWriteChannel writer = bigquery.writer(jobId, writeChannelConfiguration); - * // Write data to writer - * try { - * writer.write(ByteBuffer.wrap(csvData.getBytes(Charsets.UTF_8))); - * } finally { - * writer.close(); + **/ TableDataWriteChannel writer(JobId jobId, WriteChannelConfiguration writeChannelConfiguration); + + /** Gets the IAM policy for a specified table. */ + Policy getIamPolicy(TableId tableId, IAMOption... options); + + /** Sets the IAM policy for a specified table. */ + Policy setIamPolicy(TableId tableId, Policy policy, IAMOption... options); + + /** + * Tests whether the caller holds specific permissions on a BigQuery table. The returned list + * represents the subset of granted permissions. + */ + List+ * { + * @code + * String datasetName = "my_dataset_name"; + * String tableName = "my_table_name"; + * String csvData = "StringValue1\nStringValue2\n"; + * String location = "us"; + * TableId tableId = TableId.of(datasetName, tableName); + * WriteChannelConfiguration writeChannelConfiguration = WriteChannelConfiguration.newBuilder(tableId) + * .setFormatOptions(FormatOptions.csv()).build(); + * // The location must be specified; other fields can be auto-detected. + * JobId jobId = JobId.newBuilder().setLocation(location).build(); + * TableDataWriteChannel writer = bigquery.writer(jobId, writeChannelConfiguration); + * // Write data to writer + * try { + * writer.write(ByteBuffer.wrap(csvData.getBytes(Charsets.UTF_8))); + * } finally { + * writer.close(); + * } + * // Get load job + * Job job = writer.getJob(); + * job = job.waitFor(); + * LoadStatistics stats = job.getStatistics(); + * return stats.getOutputRows(); * } - * // Get load job - * Job job = writer.getJob(); - * job = job.waitFor(); - * LoadStatistics stats = job.getStatistics(); - * return stats.getOutputRows(); - * }+ *testIamPermissions(TableId table, List permissions, IAMOption... options); } diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java index a3526077d..070a4c965 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java @@ -17,6 +17,8 @@ package com.google.cloud.bigquery; import static com.google.cloud.RetryHelper.runWithRetries; +import static com.google.cloud.bigquery.PolicyHelper.convertFromApiPolicy; +import static com.google.cloud.bigquery.PolicyHelper.convertToApiPolicy; import static com.google.common.base.Preconditions.checkArgument; import static java.net.HttpURLConnection.HTTP_NOT_FOUND; @@ -33,6 +35,7 @@ import com.google.cloud.BaseService; import com.google.cloud.PageImpl; import com.google.cloud.PageImpl.NextPageFetcher; +import com.google.cloud.Policy; import com.google.cloud.RetryHelper; import com.google.cloud.RetryHelper.RetryHelperException; import com.google.cloud.Tuple; @@ -338,9 +341,11 @@ public com.google.api.services.bigquery.model.Job call() { throw createException; } - // If create RPC fails, it's still possible that the job has been successfully created, + // If create RPC fails, it's still possible that the job has been successfully + // created, // and get might work. - // We can only do this if we randomly generated the ID. Otherwise we might mistakenly + // We can only do this if we randomly generated the ID. Otherwise we might + // mistakenly // fetch a job created by someone else. Job job; try { @@ -653,7 +658,8 @@ public Table getTable(final String datasetId, final String tableId, TableOption. @Override public Table getTable(TableId tableId, TableOption... options) { - // More context about why this: https://github.com/googleapis/google-cloud-java/issues/3808 + // More context about why this: + // https://github.com/googleapis/google-cloud-java/issues/3808 final TableId completeTableId = tableId.setProjectId( Strings.isNullOrEmpty(tableId.getProject()) @@ -941,7 +947,8 @@ public InsertAllResponse insertAll(InsertAllRequest request) { requestPb.setIgnoreUnknownValues(request.ignoreUnknownValues()); requestPb.setSkipInvalidRows(request.skipInvalidRows()); requestPb.setTemplateSuffix(request.getTemplateSuffix()); - // Using an array of size 1 here to have a mutable boolean variable, which can be modified in + // Using an array of size 1 here to have a mutable boolean variable, which can + // be modified in // an anonymous inner class. final boolean[] allInsertIdsSet = {true}; List rowsPb = @@ -1249,6 +1256,86 @@ public TableDataWriteChannel writer( writeChannelConfiguration.setProjectId(getOptions().getProjectId())); } + @Override + public Policy getIamPolicy(TableId tableId, IAMOption... options) { + final TableId completeTableId = + tableId.setProjectId( + Strings.isNullOrEmpty(tableId.getProject()) + ? getOptions().getProjectId() + : tableId.getProject()); + + try { + final Map optionsMap = optionMap(options); + return convertFromApiPolicy( + runWithRetries( + new Callable () { + @Override + public com.google.api.services.bigquery.model.Policy call() { + return bigQueryRpc.getIamPolicy(completeTableId.getIAMResourceName(), optionsMap); + } + }, + getOptions().getRetrySettings(), + EXCEPTION_HANDLER, + getOptions().getClock())); + } catch (RetryHelper.RetryHelperException e) { + throw BigQueryException.translateAndThrow(e); + } + } + + @Override + public Policy setIamPolicy(TableId tableId, final Policy policy, IAMOption... options) { + final TableId completeTableId = + tableId.setProjectId( + Strings.isNullOrEmpty(tableId.getProject()) + ? getOptions().getProjectId() + : tableId.getProject()); + try { + final Map optionsMap = optionMap(options); + return convertFromApiPolicy( + runWithRetries( + new Callable () { + @Override + public com.google.api.services.bigquery.model.Policy call() { + return bigQueryRpc.setIamPolicy( + completeTableId.getIAMResourceName(), convertToApiPolicy(policy), optionsMap); + } + }, + getOptions().getRetrySettings(), + EXCEPTION_HANDLER, + getOptions().getClock())); + } catch (RetryHelperException e) { + throw BigQueryException.translateAndThrow(e); + } + } + + @Override + public List testIamPermissions( + TableId tableId, final List permissions, IAMOption... options) { + final TableId completeTableId = + tableId.setProjectId( + Strings.isNullOrEmpty(tableId.getProject()) + ? getOptions().getProjectId() + : tableId.getProject()); + try { + final Map optionsMap = optionMap(options); + com.google.api.services.bigquery.model.TestIamPermissionsResponse response = + runWithRetries( + new Callable () { + @Override + public com.google.api.services.bigquery.model.TestIamPermissionsResponse call() { + return bigQueryRpc.testIamPermissions( + completeTableId.getIAMResourceName(), permissions, optionsMap); + } + }, + getOptions().getRetrySettings(), + EXCEPTION_HANDLER, + getOptions().getClock()); + return ImmutableList.copyOf(response.getPermissions()); + } catch (RetryHelperException e) { + throw BigQueryException.translateAndThrow(e); + } + } + @VisibleForTesting static Map optionMap(Option... options) { Map optionMap = Maps.newEnumMap(BigQueryRpc.Option.class); diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java index ae4db388f..5687d7898 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryOptions.java @@ -94,6 +94,8 @@ private BigQueryOptions(Builder builder) { private static class BigQueryDefaults implements ServiceDefaults { + private static final long serialVersionUID = -4551722608999107711L; + @Override public BigQueryFactory getDefaultServiceFactory() { return DefaultBigQueryFactory.INSTANCE; diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/PolicyHelper.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/PolicyHelper.java new file mode 100644 index 000000000..c830bb3e3 --- /dev/null +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/PolicyHelper.java @@ -0,0 +1,80 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.cloud.bigquery; + +import com.google.api.services.bigquery.model.Expr; +import com.google.cloud.Binding; +import com.google.cloud.Condition; +import com.google.cloud.Policy; +import com.google.common.collect.ImmutableList; +import java.util.ArrayList; +import java.util.List; + +class PolicyHelper { + + static Policy convertFromApiPolicy(com.google.api.services.bigquery.model.Policy apiPolicy) { + Policy.Builder policyBuilder = Policy.newBuilder(); + List bindings = apiPolicy.getBindings(); + + if (null != bindings) { + ImmutableList.Builder coreBindings = ImmutableList.builder(); + Binding.Builder bindingBuilder = Binding.newBuilder(); + for (com.google.api.services.bigquery.model.Binding binding : bindings) { + bindingBuilder.setRole(binding.getRole()); + bindingBuilder.setMembers(binding.getMembers()); + if (binding.getCondition() != null) { + Condition.Builder conditionBuilder = Condition.newBuilder(); + conditionBuilder.setTitle(binding.getCondition().getTitle()); + conditionBuilder.setDescription(binding.getCondition().getDescription()); + conditionBuilder.setExpression(binding.getCondition().getExpression()); + bindingBuilder.setCondition(conditionBuilder.build()); + } + coreBindings.add(bindingBuilder.build()); + } + policyBuilder.setBindings(coreBindings.build()); + } + if (null != apiPolicy.getVersion()) { + policyBuilder.setVersion(apiPolicy.getVersion()); + } + return policyBuilder.setEtag(apiPolicy.getEtag()).build(); + } + + static com.google.api.services.bigquery.model.Policy convertToApiPolicy(Policy policy) { + List bindings = null; + if (policy.getBindingsList().size() != 0) { + bindings = new ArrayList<>(policy.getBindingsList().size()); + + for (Binding binding : policy.getBindingsList()) { + com.google.api.services.bigquery.model.Binding apiBinding = + new com.google.api.services.bigquery.model.Binding(); + apiBinding.setRole(binding.getRole()); + apiBinding.setMembers(new ArrayList<>(binding.getMembers())); + if (binding.getCondition() != null) { + Expr expr = new Expr(); + expr.setTitle(binding.getCondition().getTitle()); + expr.setDescription(binding.getCondition().getDescription()); + expr.setExpression(binding.getCondition().getExpression()); + apiBinding.setCondition(expr); + } + bindings.add(apiBinding); + } + } + return new com.google.api.services.bigquery.model.Policy() + .setBindings(bindings) + .setEtag(policy.getEtag()) + .setVersion(policy.getVersion()); + } +} diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableId.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableId.java index f475e5070..b74055d4f 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableId.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/TableId.java @@ -63,6 +63,12 @@ public String getTable() { return table; } + /** Returns the IAM resource name for the table. * */ + public String getIAMResourceName() { + return String.format( + "projects/%s/datasets/%s/tables/%s", getProject(), getDataset(), getTable()); + } + private TableId(String project, String dataset, String table) { checkArgument(!isNullOrEmpty(dataset), "Provided dataset is null or empty"); checkArgument(!isNullOrEmpty(table), "Provided table is null or empty"); diff --git a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/BigQueryRpc.java b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/BigQueryRpc.java index 7ba0cd764..c0b9bb4be 100644 --- a/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/BigQueryRpc.java +++ b/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/spi/v2/BigQueryRpc.java @@ -21,14 +21,17 @@ import com.google.api.services.bigquery.model.GetQueryResultsResponse; import com.google.api.services.bigquery.model.Job; import com.google.api.services.bigquery.model.Model; +import com.google.api.services.bigquery.model.Policy; import com.google.api.services.bigquery.model.Routine; import com.google.api.services.bigquery.model.Table; import com.google.api.services.bigquery.model.TableDataInsertAllRequest; import com.google.api.services.bigquery.model.TableDataInsertAllResponse; import com.google.api.services.bigquery.model.TableDataList; +import com.google.api.services.bigquery.model.TestIamPermissionsResponse; import com.google.cloud.ServiceRpc; import com.google.cloud.Tuple; import com.google.cloud.bigquery.BigQueryException; +import java.util.List; import java.util.Map; @InternalExtensionOnly @@ -48,7 +51,8 @@ enum Option { PARENT_JOB_ID("parentJobId"), START_INDEX("startIndex"), STATE_FILTER("stateFilter"), - TIMEOUT("timeoutMs"); + TIMEOUT("timeoutMs"), + REQUESTED_POLICY_VERSION("requestedPolicyVersion"); private final String value; @@ -300,4 +304,27 @@ Job write( long destOffset, int length, boolean last); + + /** + * Returns the IAM Policy for the specified resource, using Policy V1. + * + * @throws BigQueryException upon failure + */ + Policy getIamPolicy(String resourceId, Map