diff --git a/CHANGELOG.md b/CHANGELOG.md index 814982806..5bab3f4d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,40 @@ # Changelog -### [1.116.7](https://www.github.com/googleapis/java-bigquery/compare/v1.116.6...v1.116.7) (2020-07-10) +### [1.116.7](https://www.github.com/googleapis/java-bigquery/compare/v1.116.6...v1.116.7) (2020-07-21) + + +### Bug Fixes + +* **sample:** region tag ([#544](https://www.github.com/googleapis/java-bigquery/issues/544)) ([f7f6e73](https://www.github.com/googleapis/java-bigquery/commit/f7f6e73ef82924fb0f7cbae5c871f1cd27620cc6)) + + +### Documentation + +* **samples:** add auth user flow and query ([#478](https://www.github.com/googleapis/java-bigquery/issues/478)) ([65dffac](https://www.github.com/googleapis/java-bigquery/commit/65dffac6af316d0baca5cfbd2b3dd3fae323549d)) +* **samples:** add create view ddl ([#558](https://www.github.com/googleapis/java-bigquery/issues/558)) ([9253218](https://www.github.com/googleapis/java-bigquery/commit/925321881cde3edd187a9a442a26eb508535744a)) +* **samples:** add extract compressed table ([#538](https://www.github.com/googleapis/java-bigquery/issues/538)) ([3f129a6](https://www.github.com/googleapis/java-bigquery/commit/3f129a6a4fff2162d1aef5ba29b4fa6246793d97)) +* **samples:** add get dataset labels ([#539](https://www.github.com/googleapis/java-bigquery/issues/539)) ([563157c](https://www.github.com/googleapis/java-bigquery/commit/563157cc40727cb7cb9aed16dfbb956a668ead51)) +* **samples:** add get table labels ([#540](https://www.github.com/googleapis/java-bigquery/issues/540)) ([a6d9491](https://www.github.com/googleapis/java-bigquery/commit/a6d94913f74a6b4d29e9314a0652a9e3fa30aed9)) +* **samples:** add grant view access ([#563](https://www.github.com/googleapis/java-bigquery/issues/563)) ([0c092e0](https://www.github.com/googleapis/java-bigquery/commit/0c092e06cdf47882a38901e8e4814afa87c7eba4)) +* **samples:** add insert rows without rowids in a table ([#573](https://www.github.com/googleapis/java-bigquery/issues/573)) ([b07dee3](https://www.github.com/googleapis/java-bigquery/commit/b07dee31e4b6fec8bf05f23ed52006829dbdbc4a)) +* **samples:** add list dataset by label ([#555](https://www.github.com/googleapis/java-bigquery/issues/555)) ([a0d7bea](https://www.github.com/googleapis/java-bigquery/commit/a0d7beab025a2895f0efc1e9888594e386a0a1b3)) +* **samples:** add list jobs ([#554](https://www.github.com/googleapis/java-bigquery/issues/554)) ([b87cecf](https://www.github.com/googleapis/java-bigquery/commit/b87cecff3f41339b20382b26381c38c4d7150305)) +* **samples:** add list routines ([#546](https://www.github.com/googleapis/java-bigquery/issues/546)) ([2cd81d4](https://www.github.com/googleapis/java-bigquery/commit/2cd81d4fb616588e9ed966f6dd32cd4615d305c4)) +* **samples:** add load avro table with truncate from gcs ([#567](https://www.github.com/googleapis/java-bigquery/issues/567)) ([fa998ba](https://www.github.com/googleapis/java-bigquery/commit/fa998baa32437a5e39670c5c7399c0945de56d52)) +* **samples:** add load json file from gcs into a table ([#568](https://www.github.com/googleapis/java-bigquery/issues/568)) ([3d65821](https://www.github.com/googleapis/java-bigquery/commit/3d658210650d2849c86df96dccd3587e95a6f4a6)) +* **samples:** add load json file from gcs into a table with autodetect schema ([#569](https://www.github.com/googleapis/java-bigquery/issues/569)) ([259d470](https://www.github.com/googleapis/java-bigquery/commit/259d470ecadc9f384920164c8a2f45643cff130a)) +* **samples:** add load json table with encryption key from gcs ([#575](https://www.github.com/googleapis/java-bigquery/issues/575)) ([2ac556e](https://www.github.com/googleapis/java-bigquery/commit/2ac556e2707b3088446353ca03d9a8d08b6d66e2)) +* **samples:** add load table from gcs using avro file ([#564](https://www.github.com/googleapis/java-bigquery/issues/564)) ([219f7a9](https://www.github.com/googleapis/java-bigquery/commit/219f7a978ab83421917ec4eb0295af38f4a9dd04)) +* **samples:** add query destination table with encryption key ([#576](https://www.github.com/googleapis/java-bigquery/issues/576)) ([29b8a7f](https://www.github.com/googleapis/java-bigquery/commit/29b8a7f179bf1fcd125f211353ef95021c0142d8)) +* **samples:** add region tag for oauth dependencies ([#570](https://www.github.com/googleapis/java-bigquery/issues/570)) ([8ed6bd6](https://www.github.com/googleapis/java-bigquery/commit/8ed6bd679761744bed7f4dcb4f66bf6f37119154)) +* correct comment ([#553](https://www.github.com/googleapis/java-bigquery/issues/553)) ([48cdc92](https://www.github.com/googleapis/java-bigquery/commit/48cdc92b6f1e128b4f0c8c25cd9ea907ab77bd93)) + + +### Dependencies + +* update core transport dependencies to v1.31.0 ([#565](https://www.github.com/googleapis/java-bigquery/issues/565)) ([5fd73ee](https://www.github.com/googleapis/java-bigquery/commit/5fd73eeb5b5d8c91154c23642737c9ece37b1750)) + +### [1.116.6](https://www.github.com/googleapis/java-bigquery/compare/v1.116.5...v1.116.6) (2020-07-10) ### Dependencies @@ -8,7 +42,7 @@ * update dependency com.google.apis:google-api-services-bigquery to v2-rev20200625-1.30.10 ([#533](https://www.github.com/googleapis/java-bigquery/issues/533)) ([d5b4b49](https://www.github.com/googleapis/java-bigquery/commit/d5b4b496b062c6c7f402503524732381915fd688)) * update dependency com.google.cloud:google-cloud-storage to v1.111.2 ([#531](https://www.github.com/googleapis/java-bigquery/issues/531)) ([9b2aeb4](https://www.github.com/googleapis/java-bigquery/commit/9b2aeb4d729b0719e19fcc054eabb08527dd4ac2)) -### [1.116.6](https://www.github.com/googleapis/java-bigquery/compare/v1.116.5...v1.116.6) (2020-07-10) +### [1.116.5](https://www.github.com/googleapis/java-bigquery/compare/v1.116.4...v1.116.5) (2020-07-10) ### Bug Fixes @@ -26,7 +60,7 @@ * update dependency com.google.cloud:google-cloud-shared-dependencies to v0.8.3 ([#523](https://www.github.com/googleapis/java-bigquery/issues/523)) ([82341cb](https://www.github.com/googleapis/java-bigquery/commit/82341cbb12ad6f5e020b5ba15701f66f0cbbfa19)) -### [1.116.5](https://www.github.com/googleapis/java-bigquery/compare/v1.116.4...v1.116.5) (2020-07-08) +### [1.116.4](https://www.github.com/googleapis/java-bigquery/compare/v1.116.3...v1.116.4) (2020-07-08) ### Dependencies @@ -57,7 +91,7 @@ * **samples:** add insert data types ([#479](https://www.github.com/googleapis/java-bigquery/issues/479)) ([12a2bea](https://www.github.com/googleapis/java-bigquery/commit/12a2bea2c65d80a1c401cb65f3d826eb07fc3037)) * **samples:** add query batch ([#469](https://www.github.com/googleapis/java-bigquery/issues/469)) ([0ffbac3](https://www.github.com/googleapis/java-bigquery/commit/0ffbac349cc27ce72b06e05723e45a973b69cef1)) -### [1.116.4](https://www.github.com/googleapis/java-bigquery/compare/v1.116.3...v1.116.4) (2020-06-18) +### [1.116.3](https://www.github.com/googleapis/java-bigquery/compare/v1.116.2...v1.116.3) (2020-06-18) ### Documentation diff --git a/README.md b/README.md index a6eef33f8..466907704 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ See https://github.com/GoogleCloudPlatform/cloud-opensource-java/wiki/The-Google com.google.cloud libraries-bom - 8.0.0 + 8.1.0 pom import @@ -40,7 +40,7 @@ If you are using Maven without BOM, add this to your dependencies: com.google.cloud google-cloud-bigquery - 1.116.3 + 1.116.6 ``` @@ -49,11 +49,11 @@ If you are using Maven without BOM, add this to your dependencies: If you are using Gradle, add this to your dependencies ```Groovy -compile 'com.google.cloud:google-cloud-bigquery:1.116.6' +compile 'com.google.cloud:google-cloud-bigquery:1.116.7' ``` If you are using SBT, add this to your dependencies ```Scala -libraryDependencies += "com.google.cloud" % "google-cloud-bigquery" % "1.116.6" +libraryDependencies += "com.google.cloud" % "google-cloud-bigquery" % "1.116.7" ``` [//]: # ({x-version-update-end}) @@ -204,6 +204,8 @@ has instructions for running the samples. | Add Empty Column | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/AddEmptyColumn.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/AddEmptyColumn.java) | | Auth Drive Scope | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/AuthDriveScope.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/AuthDriveScope.java) | | Auth Snippets | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/AuthSnippets.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/AuthSnippets.java) | +| Auth User Flow | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/AuthUserFlow.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/AuthUserFlow.java) | +| Auth User Query | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/AuthUserQuery.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/AuthUserQuery.java) | | Browse Table | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/BrowseTable.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/BrowseTable.java) | | Cancel Job | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/CancelJob.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/CancelJob.java) | | Copy Multiple Tables | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/CopyMultipleTables.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/CopyMultipleTables.java) | @@ -221,25 +223,41 @@ has instructions for running the samples. | Create Table CMEK | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/CreateTableCMEK.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/CreateTableCMEK.java) | | Create Table Without Schema | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/CreateTableWithoutSchema.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/CreateTableWithoutSchema.java) | | Create View | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/CreateView.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/CreateView.java) | +| DDL Create View | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/DDLCreateView.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/DDLCreateView.java) | | Dataset Exists | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/DatasetExists.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/DatasetExists.java) | | Delete Dataset | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/DeleteDataset.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/DeleteDataset.java) | +| Delete Label Dataset | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/DeleteLabelDataset.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/DeleteLabelDataset.java) | +| Delete Label Table | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/DeleteLabelTable.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/DeleteLabelTable.java) | | Delete Model | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/DeleteModel.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/DeleteModel.java) | | Delete Routine | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/DeleteRoutine.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/DeleteRoutine.java) | | Delete Table | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/DeleteTable.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/DeleteTable.java) | +| Extract Table Compressed | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/ExtractTableCompressed.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/ExtractTableCompressed.java) | | Extract Table To Csv | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/ExtractTableToCsv.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/ExtractTableToCsv.java) | | Extract Table To Json | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/ExtractTableToJson.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/ExtractTableToJson.java) | | Get Dataset Info | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/GetDatasetInfo.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/GetDatasetInfo.java) | +| Get Dataset Labels | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/GetDatasetLabels.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/GetDatasetLabels.java) | | Get Job | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/GetJob.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/GetJob.java) | | Get Model | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/GetModel.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/GetModel.java) | | Get Routine | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/GetRoutine.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/GetRoutine.java) | | Get Table | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/GetTable.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/GetTable.java) | +| Get Table Labels | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/GetTableLabels.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/GetTableLabels.java) | | Get View | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/GetView.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/GetView.java) | +| Grant View Access | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/GrantViewAccess.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/GrantViewAccess.java) | | Inserting Data Types | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/InsertingDataTypes.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/InsertingDataTypes.java) | +| Label Dataset | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/LabelDataset.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/LabelDataset.java) | +| Label Table | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/LabelTable.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/LabelTable.java) | | List Datasets | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/ListDatasets.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/ListDatasets.java) | +| List Datasets By Label | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/ListDatasetsByLabel.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/ListDatasetsByLabel.java) | +| List Jobs | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/ListJobs.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/ListJobs.java) | | List Models | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/ListModels.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/ListModels.java) | +| List Routines | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/ListRoutines.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/ListRoutines.java) | | List Tables | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/ListTables.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/ListTables.java) | +| Load Avro From GCS | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/LoadAvroFromGCS.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/LoadAvroFromGCS.java) | +| Load Avro From GCS Truncate | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/LoadAvroFromGCSTruncate.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/LoadAvroFromGCSTruncate.java) | | Load Csv From Gcs | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/LoadCsvFromGcs.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/LoadCsvFromGcs.java) | | Load Csv From Gcs Truncate | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/LoadCsvFromGcsTruncate.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/LoadCsvFromGcsTruncate.java) | +| Load Json From GCS | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/LoadJsonFromGCS.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/LoadJsonFromGCS.java) | +| Load Json From GCS Autodetect | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/LoadJsonFromGCSAutodetect.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/LoadJsonFromGCSAutodetect.java) | | Load Local File | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/LoadLocalFile.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/LoadLocalFile.java) | | Load Parquet | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/LoadParquet.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/LoadParquet.java) | | Load Parquet Replace Table | [source code](https://github.com/googleapis/java-bigquery/blob/master/samples/snippets/src/main/java/com/example/bigquery/LoadParquetReplaceTable.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/LoadParquetReplaceTable.java) | diff --git a/google-cloud-bigquery/pom.xml b/google-cloud-bigquery/pom.xml index 038f7762c..535ae744f 100644 --- a/google-cloud-bigquery/pom.xml +++ b/google-cloud-bigquery/pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.google.cloud google-cloud-bigquery - 1.116.6 + 1.116.7 jar BigQuery https://github.com/googleapis/java-bigquery @@ -11,7 +11,7 @@ com.google.cloud google-cloud-bigquery-parent - 1.116.6 + 1.116.7 google-cloud-bigquery diff --git a/pom.xml b/pom.xml index c59a014b6..1341b6057 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.google.cloud google-cloud-bigquery-parent pom - 1.116.6 + 1.116.7 BigQuery Parent https://github.com/googleapis/java-bigquery @@ -86,7 +86,7 @@ com.google.cloud google-cloud-bigquery - 1.116.6 + 1.116.7 diff --git a/samples/install-without-bom/pom.xml b/samples/install-without-bom/pom.xml index a9626a7d9..1ae56b0e0 100644 --- a/samples/install-without-bom/pom.xml +++ b/samples/install-without-bom/pom.xml @@ -45,10 +45,20 @@ com.google.cloud google-cloud-bigquery - 1.116.4 + 1.116.6 + + com.google.oauth-client + google-oauth-client-java6 + 1.31.0 + + + com.google.oauth-client + google-oauth-client-jetty + 1.31.0 + junit diff --git a/samples/snapshot/pom.xml b/samples/snapshot/pom.xml index 5d7062e2b..719fc79aa 100644 --- a/samples/snapshot/pom.xml +++ b/samples/snapshot/pom.xml @@ -44,10 +44,19 @@ com.google.cloud google-cloud-bigquery - 1.116.6 + 1.116.7 - + + com.google.oauth-client + google-oauth-client-java6 + 1.31.0 + + + com.google.oauth-client + google-oauth-client-jetty + 1.31.0 + junit @@ -61,7 +70,6 @@ 1.0.1 test - diff --git a/samples/snippets/pom.xml b/samples/snippets/pom.xml index 2ca932e58..9eff2ba7b 100644 --- a/samples/snippets/pom.xml +++ b/samples/snippets/pom.xml @@ -44,7 +44,7 @@ com.google.cloud libraries-bom - 8.0.0 + 8.1.0 pom import @@ -59,6 +59,19 @@ + + + com.google.oauth-client + google-oauth-client-java6 + 1.31.0 + + + com.google.oauth-client + google-oauth-client-jetty + 1.31.0 + + + junit diff --git a/samples/snippets/src/main/java/com/example/bigquery/AuthUserFlow.java b/samples/snippets/src/main/java/com/example/bigquery/AuthUserFlow.java new file mode 100644 index 000000000..4d2de24f4 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigquery/AuthUserFlow.java @@ -0,0 +1,122 @@ +/* + * 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.example.bigquery; + +// [START bigquery_auth_user_flow] +import com.google.api.client.auth.oauth2.Credential; +import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; +import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; +import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; +import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.json.JsonFactory; +import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.client.util.store.FileDataStoreFactory; +import com.google.api.gax.paging.Page; +import com.google.auth.oauth2.GoogleCredentials; +import com.google.auth.oauth2.UserCredentials; +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.Dataset; +import com.google.common.collect.ImmutableList; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.security.GeneralSecurityException; +import java.util.List; + +// Sample to authenticate by using a user credential +public class AuthUserFlow { + + private static final File DATA_STORE_DIR = + new File(AuthUserFlow.class.getResource("/").getPath(), "credentials"); + private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); + // i.e redirect_uri http://localhost:61984/Callback + private static final int LOCAL_RECEIVER_PORT = 61984; + + public static void runAuthUserFlow() { + // TODO(developer): Replace these variables before running the sample. + /** + * Download your OAuth2 configuration from the Google Developers Console API Credentials page. + * https://console.cloud.google.com/apis/credentials + */ + Path credentialsPath = Paths.get("path/to/your/client_secret.json"); + List scopes = ImmutableList.of("https://www.googleapis.com/auth/bigquery"); + authUserFlow(credentialsPath, scopes); + } + + public static void authUserFlow(Path credentialsPath, List selectedScopes) { + // Reading credentials file + try (InputStream inputStream = Files.newInputStream(credentialsPath)) { + + // Load client_secret.json file + GoogleClientSecrets clientSecrets = + GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(inputStream)); + String clientId = clientSecrets.getDetails().getClientId(); + String clientSecret = clientSecrets.getDetails().getClientSecret(); + + // Generate the url that will be used for the consent dialog. + GoogleAuthorizationCodeFlow flow = + new GoogleAuthorizationCodeFlow.Builder( + GoogleNetHttpTransport.newTrustedTransport(), + JSON_FACTORY, + clientSecrets, + selectedScopes) + .setDataStoreFactory(new FileDataStoreFactory(DATA_STORE_DIR)) + .setAccessType("offline") + .setApprovalPrompt("auto") + .build(); + + // Exchange an authorization code for refresh token + LocalServerReceiver receiver = + new LocalServerReceiver.Builder().setPort(LOCAL_RECEIVER_PORT).build(); + Credential credential = new AuthorizationCodeInstalledApp(flow, receiver).authorize("user"); + + // OAuth2 Credentials representing a user's identity and consent + GoogleCredentials credentials = + UserCredentials.newBuilder() + .setClientId(clientId) + .setClientSecret(clientSecret) + .setRefreshToken(credential.getRefreshToken()) + .build(); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + BigQuery bigquery = + BigQueryOptions.newBuilder().setCredentials(credentials).build().getService(); + + Page datasets = bigquery.listDatasets(BigQuery.DatasetListOption.pageSize(100)); + if (datasets == null) { + System.out.println("Dataset does not contain any models"); + return; + } + datasets + .iterateAll() + .forEach( + dataset -> System.out.printf("Success! Dataset ID: %s ", dataset.getDatasetId())); + + } catch (BigQueryException | IOException | GeneralSecurityException ex) { + System.out.println("Project does not contain any datasets \n" + ex.toString()); + } + } +} +// [END bigquery_auth_user_flow] diff --git a/samples/snippets/src/main/java/com/example/bigquery/AuthUserQuery.java b/samples/snippets/src/main/java/com/example/bigquery/AuthUserQuery.java new file mode 100644 index 000000000..7be558659 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigquery/AuthUserQuery.java @@ -0,0 +1,128 @@ +/* + * 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.example.bigquery; + +// [START bigquery_auth_user_query] +import com.google.api.client.auth.oauth2.Credential; +import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; +import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; +import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; +import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.json.JsonFactory; +import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.client.util.store.FileDataStoreFactory; +import com.google.auth.oauth2.GoogleCredentials; +import com.google.auth.oauth2.UserCredentials; +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.QueryJobConfiguration; +import com.google.cloud.bigquery.TableResult; +import com.google.common.collect.ImmutableList; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.security.GeneralSecurityException; +import java.util.List; + +// Sample to query by using a user credential +public class AuthUserQuery { + + private static final File DATA_STORE_DIR = + new File(AuthUserQuery.class.getResource("/").getPath(), "credentials"); + private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); + // i.e redirect_uri http://localhost:61984/Callback + private static final int LOCAL_RECEIVER_PORT = 61984; + + public static void runAuthUserQuery() { + // TODO(developer): Replace these variables before running the sample. + /** + * Download your OAuth2 configuration from the Google Developers Console API Credentials page. + * https://console.cloud.google.com/apis/credentials + */ + Path credentialsPath = Paths.get("path/to/your/client_secret.json"); + List scopes = ImmutableList.of("https://www.googleapis.com/auth/bigquery"); + String query = + "SELECT name, SUM(number) as total" + + " FROM `bigquery-public-data.usa_names.usa_1910_current`" + + " WHERE name = 'William'" + + " GROUP BY name;"; + authUserQuery(credentialsPath, scopes, query); + } + + public static void authUserQuery( + Path credentialsPath, List selectedScopes, String query) { + // Reading credentials file + try (InputStream inputStream = Files.newInputStream(credentialsPath)) { + + // Load client_secret.json file + GoogleClientSecrets clientSecrets = + GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(inputStream)); + String clientId = clientSecrets.getDetails().getClientId(); + String clientSecret = clientSecrets.getDetails().getClientSecret(); + + // Generate the url that will be used for the consent dialog. + GoogleAuthorizationCodeFlow flow = + new GoogleAuthorizationCodeFlow.Builder( + GoogleNetHttpTransport.newTrustedTransport(), + JSON_FACTORY, + clientSecrets, + selectedScopes) + .setDataStoreFactory(new FileDataStoreFactory(DATA_STORE_DIR)) + .setAccessType("offline") + .setApprovalPrompt("auto") + .build(); + + // Exchange an authorization code for refresh token + LocalServerReceiver receiver = + new LocalServerReceiver.Builder().setPort(LOCAL_RECEIVER_PORT).build(); + Credential credential = new AuthorizationCodeInstalledApp(flow, receiver).authorize("user"); + + // OAuth2 Credentials representing a user's identity and consent + GoogleCredentials credentials = + UserCredentials.newBuilder() + .setClientId(clientId) + .setClientSecret(clientSecret) + .setRefreshToken(credential.getRefreshToken()) + .build(); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + BigQuery bigquery = + BigQueryOptions.newBuilder().setCredentials(credentials).build().getService(); + + QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder(query).build(); + + TableResult results = bigquery.query(queryConfig); + + results + .iterateAll() + .forEach(row -> row.forEach(val -> System.out.printf("%s,", val.toString()))); + + System.out.println("Query performed successfully."); + + } catch (BigQueryException | IOException | GeneralSecurityException | InterruptedException ex) { + System.out.println("Query not performed \n" + ex.toString()); + } + } +} +// [END bigquery_auth_user_query] diff --git a/samples/snippets/src/main/java/com/example/bigquery/DDLCreateView.java b/samples/snippets/src/main/java/com/example/bigquery/DDLCreateView.java new file mode 100644 index 000000000..89f23fac7 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigquery/DDLCreateView.java @@ -0,0 +1,78 @@ +/* + * 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.example.bigquery; + +// [START bigquery_ddl_create_view] +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.Job; +import com.google.cloud.bigquery.JobInfo; +import com.google.cloud.bigquery.QueryJobConfiguration; + +// Sample to create a view using DDL +public class DDLCreateView { + + public static void runDDLCreateView() { + // TODO(developer): Replace these variables before running the sample. + String projectId = "MY_PROJECT_ID"; + String datasetId = "MY_DATASET_ID"; + String tableId = "MY_VIEW_ID"; + String ddl = + "CREATE VIEW " + + "`" + + projectId + + "." + + datasetId + + "." + + tableId + + "`" + + " OPTIONS(" + + " expiration_timestamp=TIMESTAMP_ADD(" + + " CURRENT_TIMESTAMP(), INTERVAL 48 HOUR)," + + " friendly_name=\"new_view\"," + + " description=\"a view that expires in 2 days\"," + + " labels=[(\"org_unit\", \"development\")]" + + " )" + + " AS SELECT name, state, year, number" + + " FROM `bigquery-public-data.usa_names.usa_1910_current`" + + " WHERE state LIKE 'W%'`"; + ddlCreateView(ddl); + } + + public static void ddlCreateView(String ddl) { + try { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + + QueryJobConfiguration config = QueryJobConfiguration.newBuilder(ddl).build(); + + // create a view using query and it will wait to complete job. + Job job = bigquery.create(JobInfo.of(config)); + job = job.waitFor(); + if (job.isDone()) { + System.out.println("View created successfully"); + } else { + System.out.println("View was not created"); + } + } catch (BigQueryException | InterruptedException e) { + System.out.println("View was not created. \n" + e.toString()); + } + } +} +// [END bigquery_ddl_create_view] diff --git a/samples/snippets/src/main/java/com/example/bigquery/DeleteLabelTable.java b/samples/snippets/src/main/java/com/example/bigquery/DeleteLabelTable.java index 043c13a91..088e50859 100644 --- a/samples/snippets/src/main/java/com/example/bigquery/DeleteLabelTable.java +++ b/samples/snippets/src/main/java/com/example/bigquery/DeleteLabelTable.java @@ -54,4 +54,4 @@ public static void deleteLabelTable(String datasetName, String tableName) { } } } -// [START bigquery_delete_label_table] +// [END bigquery_delete_label_table] diff --git a/samples/snippets/src/main/java/com/example/bigquery/ExtractTableCompressed.java b/samples/snippets/src/main/java/com/example/bigquery/ExtractTableCompressed.java new file mode 100644 index 000000000..b30385ab1 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigquery/ExtractTableCompressed.java @@ -0,0 +1,87 @@ +/* + * 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.example.bigquery; + +// [START bigquery_extract_table_compressed] +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.ExtractJobConfiguration; +import com.google.cloud.bigquery.Job; +import com.google.cloud.bigquery.JobInfo; +import com.google.cloud.bigquery.TableId; + +// Sample to extract a compressed table +public class ExtractTableCompressed { + + public static void runExtractTableCompressed() { + // TODO(developer): Replace these variables before running the sample. + String projectName = "MY_PROJECT_NAME"; + String datasetName = "MY_DATASET_NAME"; + String tableName = "MY_TABLE_NAME"; + String bucketName = "MY-BUCKET-NAME"; + String destinationUri = "gs://" + bucketName + "/path/to/file"; + // For more information on export formats available see: + // https://cloud.google.com/bigquery/docs/exporting-data#export_formats_and_compression_types + String compressed = "gzip"; + // For more information on Job see: + // https://googleapis.dev/java/google-cloud-clients/latest/index.html?com/google/cloud/bigquery/package-summary.html + String dataFormat = "CSV"; + + extractTableCompressed( + projectName, datasetName, tableName, destinationUri, dataFormat, compressed); + } + + public static void extractTableCompressed( + String projectName, + String datasetName, + String tableName, + String destinationUri, + String dataFormat, + String compressed) { + try { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + + TableId tableId = TableId.of(projectName, datasetName, tableName); + + ExtractJobConfiguration extractConfig = + ExtractJobConfiguration.newBuilder(tableId, destinationUri) + .setCompression(compressed) + .setFormat(dataFormat) + .build(); + + Job job = bigquery.create(JobInfo.of(extractConfig)); + + // Blocks until this job completes its execution, either failing or succeeding. + Job completedJob = job.waitFor(); + if (completedJob == null) { + System.out.println("Job not executed since it no longer exists."); + return; + } else if (completedJob.getStatus().getError() != null) { + System.out.println( + "BigQuery was unable to extract due to an error: \n" + job.getStatus().getError()); + return; + } + System.out.println("Table extract compressed successful"); + } catch (BigQueryException | InterruptedException e) { + System.out.println("Table extraction job was interrupted. \n" + e.toString()); + } + } +} +// [END bigquery_extract_table_compressed] diff --git a/samples/snippets/src/main/java/com/example/bigquery/GetDatasetLabels.java b/samples/snippets/src/main/java/com/example/bigquery/GetDatasetLabels.java new file mode 100644 index 000000000..82de732fd --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigquery/GetDatasetLabels.java @@ -0,0 +1,49 @@ +/* + * 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.example.bigquery; + +// [START bigquery_get_dataset_labels] +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.Dataset; + +// Sample to get dataset labels +public class GetDatasetLabels { + + public static void runGetDatasetLabels() { + // TODO(developer): Replace these variables before running the sample. + String datasetName = "MY_DATASET_NAME"; + getDatasetLabels(datasetName); + } + + public static void getDatasetLabels(String datasetName) { + try { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + + Dataset dataset = bigquery.getDataset(datasetName); + dataset + .getLabels() + .forEach((key, value) -> System.out.println("Retrieved labels successfully")); + } catch (BigQueryException e) { + System.out.println("Label was not found. \n" + e.toString()); + } + } +} +// [END bigquery_get_dataset_labels] diff --git a/samples/snippets/src/main/java/com/example/bigquery/GetTableLabels.java b/samples/snippets/src/main/java/com/example/bigquery/GetTableLabels.java new file mode 100644 index 000000000..e3ef02b9b --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigquery/GetTableLabels.java @@ -0,0 +1,52 @@ +/* + * 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.example.bigquery; + +// [START bigquery_get_table_labels] +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.Table; +import com.google.cloud.bigquery.TableId; + +// Sample to get table labels +public class GetTableLabels { + + public static void runGetTableLabels() { + // TODO(developer): Replace these variables before running the sample. + String datasetName = "MY_DATASET_NAME"; + String tableName = "MY_TABLE_NAME"; + getTableLabels(datasetName, tableName); + } + + public static void getTableLabels(String datasetName, String tableName) { + try { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + + // This example table starts with existing label { color: 'green' } + Table table = bigquery.getTable(TableId.of(datasetName, tableName)); + table + .getLabels() + .forEach((key, value) -> System.out.println("Retrieved labels successfully")); + } catch (BigQueryException e) { + System.out.println("Label was not deleted. \n" + e.toString()); + } + } +} +// [END bigquery_get_table_labels] diff --git a/samples/snippets/src/main/java/com/example/bigquery/GrantViewAccess.java b/samples/snippets/src/main/java/com/example/bigquery/GrantViewAccess.java new file mode 100644 index 000000000..72858af47 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigquery/GrantViewAccess.java @@ -0,0 +1,73 @@ +/* + * 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.example.bigquery; + +// [START bigquery_grant_view_access] +import com.google.cloud.bigquery.Acl; +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.Dataset; +import com.google.cloud.bigquery.DatasetId; +import com.google.cloud.bigquery.Table; +import java.util.ArrayList; +import java.util.List; + +// Sample to grant view access on dataset +public class GrantViewAccess { + + public static void runGrantViewAccess() { + // TODO(developer): Replace these variables before running the sample. + String srcDatasetId = "MY_DATASET_ID"; + String viewDatasetId = "MY_VIEW_DATASET_ID"; + String viewId = "MY_VIEW_ID"; + grantViewAccess(srcDatasetId, viewDatasetId, viewId); + } + + public static void grantViewAccess(String srcDatasetId, String viewDatasetId, String viewId) { + try { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + + Dataset srcDataset = bigquery.getDataset(DatasetId.of(srcDatasetId)); + Dataset viewDataset = bigquery.getDataset(DatasetId.of(viewDatasetId)); + Table view = viewDataset.get(viewId); + + // First, we'll add a group to the ACL for the dataset containing the view. This will allow + // users within that group to query the view, but they must have direct access to any tables + // referenced by the view. + List viewAcl = new ArrayList<>(); + viewAcl.addAll(viewDataset.getAcl()); + viewAcl.add(Acl.of(new Acl.Group("example-analyst-group@google.com"), Acl.Role.READER)); + viewDataset.toBuilder().setAcl(viewAcl).build().update(); + + // Now, we'll authorize a specific view against a source dataset, delegating access + // enforcement. Once this has been completed, members of the group previously added to the + // view dataset's ACL no longer require access to the source dataset to successfully query the + // view + List srcAcl = new ArrayList<>(); + srcAcl.addAll(srcDataset.getAcl()); + srcAcl.add(Acl.of(new Acl.View(view.getTableId()))); + srcDataset.toBuilder().setAcl(srcAcl).build().update(); + System.out.println("Grant view access successfully"); + } catch (BigQueryException e) { + System.out.println("Grant view access was not success. \n" + e.toString()); + } + } +} +// [END bigquery_grant_view_access] diff --git a/samples/snippets/src/main/java/com/example/bigquery/ListDatasetsByLabel.java b/samples/snippets/src/main/java/com/example/bigquery/ListDatasetsByLabel.java new file mode 100644 index 000000000..eccb9eada --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigquery/ListDatasetsByLabel.java @@ -0,0 +1,60 @@ +/* + * 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.example.bigquery; + +// [START bigquery_list_datasets_by_label] +import com.google.api.gax.paging.Page; +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.Dataset; + +// Sample to get list of datasets by label +public class ListDatasetsByLabel { + + public static void runListDatasetsByLabel() { + // TODO(developer): Replace these variables before running the sample. + String projectId = "MY_PROJECT_ID"; + String filter = "MY_LABEL_FILTER"; + listDatasetsByLabel(projectId, filter); + } + + public static void listDatasetsByLabel(String projectId, String filter) { + try { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + + Page datasets = + bigquery.listDatasets( + projectId, + BigQuery.DatasetListOption.pageSize(100), + BigQuery.DatasetListOption.labelFilter(filter)); // "labels.color:green" + if (datasets == null) { + System.out.println("Dataset does not contain any models"); + return; + } + datasets + .iterateAll() + .forEach( + dataset -> System.out.printf("Success! Dataset ID: %s ", dataset.getDatasetId())); + } catch (BigQueryException e) { + System.out.println("Project does not contain any datasets \n" + e.toString()); + } + } +} +// [END bigquery_list_datasets_by_label] diff --git a/samples/snippets/src/main/java/com/example/bigquery/ListJobs.java b/samples/snippets/src/main/java/com/example/bigquery/ListJobs.java new file mode 100644 index 000000000..2c9399e47 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigquery/ListJobs.java @@ -0,0 +1,50 @@ +/* + * 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.example.bigquery; + +// [START bigquery_list_jobs] +import com.google.api.gax.paging.Page; +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.Job; + +// Sample to get list of jobs +public class ListJobs { + + public static void runListJobs() { + listJobs(); + } + + public static void listJobs() { + try { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + + Page jobs = bigquery.listJobs(BigQuery.JobListOption.pageSize(10)); + if (jobs == null) { + System.out.println("Dataset does not contain any jobs."); + return; + } + jobs.getValues().forEach(job -> System.out.printf("Success! Job ID: %s", job.getJobId())); + } catch (BigQueryException e) { + System.out.println("Jobs not listed in dataset due to error: \n" + e.toString()); + } + } +} +// [END bigquery_list_jobs] diff --git a/samples/snippets/src/main/java/com/example/bigquery/ListRoutines.java b/samples/snippets/src/main/java/com/example/bigquery/ListRoutines.java new file mode 100644 index 000000000..f7c387427 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigquery/ListRoutines.java @@ -0,0 +1,55 @@ +/* + * 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.example.bigquery; + +// [START bigquery_list_routines] +import com.google.api.gax.paging.Page; +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.Routine; + +// Sample to get list of routines +public class ListRoutines { + + public static void runListRoutines() { + // TODO(developer): Replace these variables before running the sample. + String datasetName = "MY_DATASET_NAME"; + listRoutines(datasetName); + } + + public static void listRoutines(String datasetName) { + try { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + + Page routines = + bigquery.listRoutines(datasetName, BigQuery.RoutineListOption.pageSize(100)); + if (routines == null) { + System.out.println("Dataset does not contain any routines."); + return; + } + routines + .iterateAll() + .forEach(routine -> System.out.printf("Success! Routine ID: %s", routine.getRoutineId())); + } catch (BigQueryException e) { + System.out.println("Routines not listed in dataset due to error: \n" + e.toString()); + } + } +} +// [END bigquery_list_routines] diff --git a/samples/snippets/src/main/java/com/example/bigquery/LoadAvroFromGCS.java b/samples/snippets/src/main/java/com/example/bigquery/LoadAvroFromGCS.java new file mode 100644 index 000000000..b69d2d8d4 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigquery/LoadAvroFromGCS.java @@ -0,0 +1,66 @@ +/* + * 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.example.bigquery; + +// [START bigquery_load_table_gcs_avro] +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.FormatOptions; +import com.google.cloud.bigquery.Job; +import com.google.cloud.bigquery.JobInfo; +import com.google.cloud.bigquery.LoadJobConfiguration; +import com.google.cloud.bigquery.TableId; + +// Sample to load Avro data from Cloud Storage into a new BigQuery table +public class LoadAvroFromGCS { + + public static void runLoadAvroFromGCS() { + // TODO(developer): Replace these variables before running the sample. + String datasetName = "MY_DATASET_NAME"; + String tableName = "MY_TABLE_NAME"; + String sourceUri = "gs://cloud-samples-data/bigquery/us-states/us-states.avro"; + loadAvroFromGCS(datasetName, tableName, sourceUri); + } + + public static void loadAvroFromGCS(String datasetName, String tableName, String sourceUri) { + try { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + + TableId tableId = TableId.of(datasetName, tableName); + LoadJobConfiguration loadConfig = + LoadJobConfiguration.of(tableId, sourceUri, FormatOptions.avro()); + + // Load data from a GCS Avro file into the table + Job job = bigquery.create(JobInfo.of(loadConfig)); + // Blocks until this load table job completes its execution, either failing or succeeding. + job = job.waitFor(); + if (job.isDone()) { + System.out.println("Avro from GCS successfully loaded in a table"); + } else { + System.out.println( + "BigQuery was unable to load into the table due to an error:" + + job.getStatus().getError()); + } + } catch (BigQueryException | InterruptedException e) { + System.out.println("Column not added during load append \n" + e.toString()); + } + } +} +// [END bigquery_load_table_gcs_avro] diff --git a/samples/snippets/src/main/java/com/example/bigquery/LoadAvroFromGCSTruncate.java b/samples/snippets/src/main/java/com/example/bigquery/LoadAvroFromGCSTruncate.java new file mode 100644 index 000000000..d6fdfc1ec --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigquery/LoadAvroFromGCSTruncate.java @@ -0,0 +1,71 @@ +/* + * 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.example.bigquery; + +// [START bigquery_load_table_gcs_avro_truncate] +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.FormatOptions; +import com.google.cloud.bigquery.Job; +import com.google.cloud.bigquery.JobInfo; +import com.google.cloud.bigquery.LoadJobConfiguration; +import com.google.cloud.bigquery.TableId; + +// Sample to overwrite the BigQuery table data by loading a AVRO file from GCS +public class LoadAvroFromGCSTruncate { + + public static void runLoadAvroFromGCSTruncate() { + // TODO(developer): Replace these variables before running the sample. + String datasetName = "MY_DATASET_NAME"; + String tableName = "MY_TABLE_NAME"; + String sourceUri = "gs://cloud-samples-data/bigquery/us-states/us-states.avro"; + loadAvroFromGCSTruncate(datasetName, tableName, sourceUri); + } + + public static void loadAvroFromGCSTruncate( + String datasetName, String tableName, String sourceUri) { + try { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + + TableId tableId = TableId.of(datasetName, tableName); + LoadJobConfiguration loadConfig = + LoadJobConfiguration.newBuilder(tableId, sourceUri) + .setFormatOptions(FormatOptions.avro()) + // Set the write disposition to overwrite existing table data + .setWriteDisposition(JobInfo.WriteDisposition.WRITE_TRUNCATE) + .build(); + + // Load data from a GCS Avro file into the table + Job job = bigquery.create(JobInfo.of(loadConfig)); + // Blocks until this load table job completes its execution, either failing or succeeding. + job = job.waitFor(); + if (job.isDone()) { + System.out.println("Table is successfully overwritten by AVRO file loaded from GCS"); + } else { + System.out.println( + "BigQuery was unable to load into the table due to an error:" + + job.getStatus().getError()); + } + } catch (BigQueryException | InterruptedException e) { + System.out.println("Column not added during load append \n" + e.toString()); + } + } +} +// [END bigquery_load_table_gcs_avro_truncate] diff --git a/samples/snippets/src/main/java/com/example/bigquery/LoadCsvFromGcs.java b/samples/snippets/src/main/java/com/example/bigquery/LoadCsvFromGcs.java index 95c63164c..45a29c19d 100644 --- a/samples/snippets/src/main/java/com/example/bigquery/LoadCsvFromGcs.java +++ b/samples/snippets/src/main/java/com/example/bigquery/LoadCsvFromGcs.java @@ -45,7 +45,7 @@ public static void loadCsvFromGcs(String datasetName, String tableName, String s Table table = bigquery.getTable(datasetName, tableName); Job loadJob = table.load(FormatOptions.csv(), sourceUri); - // Load data from a GCS parquet file into the table + // Load data from a GCS CSV file into the table // Blocks until this load table job completes its execution, either failing or succeeding. Job completedJob = loadJob.waitFor(); diff --git a/samples/snippets/src/main/java/com/example/bigquery/LoadJsonFromGCS.java b/samples/snippets/src/main/java/com/example/bigquery/LoadJsonFromGCS.java new file mode 100644 index 000000000..df3681d91 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigquery/LoadJsonFromGCS.java @@ -0,0 +1,77 @@ +/* + * 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.example.bigquery; + +// [START bigquery_load_table_gcs_json] +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.Field; +import com.google.cloud.bigquery.FormatOptions; +import com.google.cloud.bigquery.Job; +import com.google.cloud.bigquery.JobInfo; +import com.google.cloud.bigquery.LoadJobConfiguration; +import com.google.cloud.bigquery.Schema; +import com.google.cloud.bigquery.StandardSQLTypeName; +import com.google.cloud.bigquery.TableId; + +// Sample to load JSON data from Cloud Storage into a new BigQuery table +public class LoadJsonFromGCS { + + public static void runLoadJsonFromGCS() { + // TODO(developer): Replace these variables before running the sample. + String datasetName = "MY_DATASET_NAME"; + String tableName = "MY_TABLE_NAME"; + String sourceUri = "gs://cloud-samples-data/bigquery/us-states/us-states.json"; + Schema schema = + Schema.of( + Field.of("name", StandardSQLTypeName.STRING), + Field.of("post_abbr", StandardSQLTypeName.STRING)); + loadJsonFromGCS(datasetName, tableName, sourceUri, schema); + } + + public static void loadJsonFromGCS( + String datasetName, String tableName, String sourceUri, Schema schema) { + try { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + + TableId tableId = TableId.of(datasetName, tableName); + LoadJobConfiguration loadConfig = + LoadJobConfiguration.newBuilder(tableId, sourceUri) + .setFormatOptions(FormatOptions.json()) + .setSchema(schema) + .build(); + + // Load data from a GCS JSON file into the table + Job job = bigquery.create(JobInfo.of(loadConfig)); + // Blocks until this load table job completes its execution, either failing or succeeding. + job = job.waitFor(); + if (job.isDone()) { + System.out.println("Json from GCS successfully loaded in a table"); + } else { + System.out.println( + "BigQuery was unable to load into the table due to an error:" + + job.getStatus().getError()); + } + } catch (BigQueryException | InterruptedException e) { + System.out.println("Column not added during load append \n" + e.toString()); + } + } +} +// [END bigquery_load_table_gcs_json] diff --git a/samples/snippets/src/main/java/com/example/bigquery/LoadJsonFromGCSAutodetect.java b/samples/snippets/src/main/java/com/example/bigquery/LoadJsonFromGCSAutodetect.java new file mode 100644 index 000000000..b8fab0b5c --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigquery/LoadJsonFromGCSAutodetect.java @@ -0,0 +1,70 @@ +/* + * 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.example.bigquery; + +// [START bigquery_load_table_gcs_json_autodetect] +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.FormatOptions; +import com.google.cloud.bigquery.Job; +import com.google.cloud.bigquery.JobInfo; +import com.google.cloud.bigquery.LoadJobConfiguration; +import com.google.cloud.bigquery.TableId; + +// Sample to load JSON data with autodetect schema from Cloud Storage into a new BigQuery table +public class LoadJsonFromGCSAutodetect { + + public static void runLoadJsonFromGCSAutodetect() { + // TODO(developer): Replace these variables before running the sample. + String datasetName = "MY_DATASET_NAME"; + String tableName = "MY_TABLE_NAME"; + String sourceUri = "gs://cloud-samples-data/bigquery/us-states/us-states.json"; + loadJsonFromGCSAutodetect(datasetName, tableName, sourceUri); + } + + public static void loadJsonFromGCSAutodetect( + String datasetName, String tableName, String sourceUri) { + try { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + + TableId tableId = TableId.of(datasetName, tableName); + LoadJobConfiguration loadConfig = + LoadJobConfiguration.newBuilder(tableId, sourceUri) + .setFormatOptions(FormatOptions.json()) + .setAutodetect(true) + .build(); + + // Load data from a GCS JSON file into the table + Job job = bigquery.create(JobInfo.of(loadConfig)); + // Blocks until this load table job completes its execution, either failing or succeeding. + job = job.waitFor(); + if (job.isDone()) { + System.out.println("Json Autodetect from GCS successfully loaded in a table"); + } else { + System.out.println( + "BigQuery was unable to load into the table due to an error:" + + job.getStatus().getError()); + } + } catch (BigQueryException | InterruptedException e) { + System.out.println("Column not added during load append \n" + e.toString()); + } + } +} +// [END bigquery_load_table_gcs_json_autodetect] diff --git a/samples/snippets/src/main/java/com/example/bigquery/LoadJsonFromGCSCMEK.java b/samples/snippets/src/main/java/com/example/bigquery/LoadJsonFromGCSCMEK.java new file mode 100644 index 000000000..46b02061d --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigquery/LoadJsonFromGCSCMEK.java @@ -0,0 +1,77 @@ +/* + * 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.example.bigquery; + +// [START bigquery_load_table_gcs_json_cmek] +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.EncryptionConfiguration; +import com.google.cloud.bigquery.FormatOptions; +import com.google.cloud.bigquery.Job; +import com.google.cloud.bigquery.JobInfo; +import com.google.cloud.bigquery.LoadJobConfiguration; +import com.google.cloud.bigquery.TableId; + +// Sample to load JSON data with configuration key from Cloud Storage into a new BigQuery table +public class LoadJsonFromGCSCMEK { + + public static void runLoadJsonFromGCSCMEK() { + // TODO(developer): Replace these variables before running the sample. + String datasetName = "MY_DATASET_NAME"; + String tableName = "MY_TABLE_NAME"; + String kmsKeyName = "MY_KMS_KEY_NAME"; + String sourceUri = "gs://cloud-samples-data/bigquery/us-states/us-states.json"; + // i.e. projects/{project}/locations/{location}/keyRings/{key_ring}/cryptoKeys/{cryptoKey} + EncryptionConfiguration encryption = + EncryptionConfiguration.newBuilder().setKmsKeyName(kmsKeyName).build(); + loadJsonFromGCSCMEK(datasetName, tableName, sourceUri, encryption); + } + + public static void loadJsonFromGCSCMEK( + String datasetName, String tableName, String sourceUri, EncryptionConfiguration encryption) { + try { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + + TableId tableId = TableId.of(datasetName, tableName); + LoadJobConfiguration loadConfig = + LoadJobConfiguration.newBuilder(tableId, sourceUri) + // Set the encryption key to use for the destination. + .setDestinationEncryptionConfiguration(encryption) + .setFormatOptions(FormatOptions.json()) + .setAutodetect(true) + .build(); + + // Load data from a GCS JSON file into the table + Job job = bigquery.create(JobInfo.of(loadConfig)); + // Blocks until this load table job completes its execution, either failing or succeeding. + job = job.waitFor(); + if (job.isDone()) { + System.out.println("Table loaded succesfully from GCS with configuration key"); + } else { + System.out.println( + "BigQuery was unable to load into the table due to an error:" + + job.getStatus().getError()); + } + } catch (BigQueryException | InterruptedException e) { + System.out.println("Column not added during load append \n" + e.toString()); + } + } +} +// [END bigquery_load_table_gcs_json_cmek] diff --git a/samples/snippets/src/main/java/com/example/bigquery/QueryDestinationTableCMEK.java b/samples/snippets/src/main/java/com/example/bigquery/QueryDestinationTableCMEK.java new file mode 100644 index 000000000..82b37433a --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigquery/QueryDestinationTableCMEK.java @@ -0,0 +1,65 @@ +/* + * 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.example.bigquery; + +// [START bigquery_query_destination_table_cmek] +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.EncryptionConfiguration; +import com.google.cloud.bigquery.QueryJobConfiguration; +import com.google.cloud.bigquery.TableResult; + +// Sample to query on destination table with encryption key +public class QueryDestinationTableCMEK { + + public static void runQueryDestinationTableCMEK() { + // TODO(developer): Replace these variables before running the sample. + String datasetName = "MY_DATASET_NAME"; + String tableName = "MY_TABLE_NAME"; + String kmsKeyName = "MY_KMS_KEY_NAME"; + String query = + String.format("SELECT stringField, booleanField FROM %s.%s", datasetName, tableName); + EncryptionConfiguration encryption = + EncryptionConfiguration.newBuilder().setKmsKeyName(kmsKeyName).build(); + queryDestinationTableCMEK(query, encryption); + } + + public static void queryDestinationTableCMEK(String query, EncryptionConfiguration encryption) { + try { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + + QueryJobConfiguration config = + QueryJobConfiguration.newBuilder(query) + // Set the encryption key to use for the destination. + .setDestinationEncryptionConfiguration(encryption) + .build(); + + TableResult results = bigquery.query(config); + + results + .iterateAll() + .forEach(row -> row.forEach(val -> System.out.printf("%s,", val.toString()))); + System.out.println("Query performed successfully with encryption key."); + } catch (BigQueryException | InterruptedException e) { + System.out.println("Query not performed \n" + e.toString()); + } + } +} +// [END bigquery_query_destination_table_cmek] diff --git a/samples/snippets/src/main/java/com/example/bigquery/TableInsertRows.java b/samples/snippets/src/main/java/com/example/bigquery/TableInsertRows.java index 04fa0a2c8..c405334ca 100644 --- a/samples/snippets/src/main/java/com/example/bigquery/TableInsertRows.java +++ b/samples/snippets/src/main/java/com/example/bigquery/TableInsertRows.java @@ -28,6 +28,7 @@ import java.util.List; import java.util.Map; +// Sample to inserting rows into a table without running a load job. public class TableInsertRows { public static void runTableInsertRows() { @@ -54,7 +55,13 @@ public static void tableInsertRows( // Inserts rowContent into datasetName:tableId. InsertAllResponse response = - bigquery.insertAll(InsertAllRequest.newBuilder(tableId).addRow(rowContent).build()); + bigquery.insertAll( + InsertAllRequest.newBuilder(tableId) + // More rows can be added in the same RPC by invoking .addRow() on the builder. + // You can also supply optional unique row keys to support de-duplication + // scenarios. + .addRow(rowContent) + .build()); if (response.hasErrors()) { // If any of the insertions failed, this lets you inspect the errors diff --git a/samples/snippets/src/main/java/com/example/bigquery/TableInsertRowsWithoutRowIds.java b/samples/snippets/src/main/java/com/example/bigquery/TableInsertRowsWithoutRowIds.java new file mode 100644 index 000000000..674b10f0b --- /dev/null +++ b/samples/snippets/src/main/java/com/example/bigquery/TableInsertRowsWithoutRowIds.java @@ -0,0 +1,79 @@ +/* + * 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.example.bigquery; + +// [START bigquery_table_insert_rows_explicit_none_insert_ids] +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQueryError; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.InsertAllRequest; +import com.google.cloud.bigquery.InsertAllResponse; +import com.google.cloud.bigquery.TableId; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +// Sample to insert rows without row ids in a table +public class TableInsertRowsWithoutRowIds { + + public static void runTableInsertRowsWithoutRowIds() { + // TODO(developer): Replace these variables before running the sample. + String datasetName = "MY_DATASET_NAME"; + String tableName = "MY_TABLE_NAME"; + // Create rows to insert + Map rowContent1 = new HashMap<>(); + rowContent1.put("stringField", "Phred Phlyntstone"); + rowContent1.put("numericField", 32); + Map rowContent2 = new HashMap<>(); + rowContent2.put("stringField", "Wylma Phlyntstone"); + rowContent2.put("numericField", 29); + List rowContent = new ArrayList<>(); + // insertId is null if not specified + rowContent.add(InsertAllRequest.RowToInsert.of(rowContent1)); + rowContent.add(InsertAllRequest.RowToInsert.of(rowContent2)); + tableInsertRowsWithoutRowIds(datasetName, tableName, rowContent); + } + + public static void tableInsertRowsWithoutRowIds( + String datasetName, String tableName, Iterable rows) { + try { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); + + // Get table + TableId tableId = TableId.of(datasetName, tableName); + + // Inserts rowContent into datasetName:tableId. + InsertAllResponse response = + bigquery.insertAll(InsertAllRequest.newBuilder(tableId).setRows(rows).build()); + + if (response.hasErrors()) { + // If any of the insertions failed, this lets you inspect the errors + for (Map.Entry> entry : response.getInsertErrors().entrySet()) { + System.out.println("Response error: \n" + entry.getValue()); + } + } + System.out.println("Rows successfully inserted into table without row ids"); + } catch (BigQueryException e) { + System.out.println("Insert operation not performed \n" + e.toString()); + } + } +} +// [END bigquery_table_insert_rows_explicit_none_insert_ids] diff --git a/samples/snippets/src/test/java/com/example/bigquery/DDLCreateViewIT.java b/samples/snippets/src/test/java/com/example/bigquery/DDLCreateViewIT.java new file mode 100644 index 000000000..7d6e0dae1 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/bigquery/DDLCreateViewIT.java @@ -0,0 +1,92 @@ +/* + * 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.example.bigquery; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.UUID; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class DDLCreateViewIT { + + private String viewName; + private ByteArrayOutputStream bout; + private PrintStream out; + + private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME"); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + requireEnvVar("BIGQUERY_DATASET_NAME"); + } + + @Before + public void setUp() { + viewName = "MY_VIEW_NAME_TEST_" + UUID.randomUUID().toString().substring(0, 8); + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + // Clean up + DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, viewName); + System.setOut(null); + } + + @Test + public void testDDLCreateView() { + String sql = + "CREATE VIEW " + + "`" + + PROJECT_ID + + "." + + BIGQUERY_DATASET_NAME + + "." + + viewName + + "`" + + " OPTIONS(" + + " expiration_timestamp=TIMESTAMP_ADD(" + + " CURRENT_TIMESTAMP(), INTERVAL 48 HOUR)," + + " friendly_name=\"new_view\"," + + " description=\"a view that expires in 2 days\"," + + " labels=[(\"org_unit\", \"development\")]" + + " )" + + " AS SELECT name, state, year, number" + + " FROM `bigquery-public-data.usa_names.usa_1910_current`" + + " WHERE state LIKE 'W%'"; + DDLCreateView.ddlCreateView(sql); + assertThat(bout.toString()).contains("View created successfully"); + } +} diff --git a/samples/snippets/src/test/java/com/example/bigquery/ExtractTableCompressedIT.java b/samples/snippets/src/test/java/com/example/bigquery/ExtractTableCompressedIT.java new file mode 100644 index 000000000..2a298e61e --- /dev/null +++ b/samples/snippets/src/test/java/com/example/bigquery/ExtractTableCompressedIT.java @@ -0,0 +1,73 @@ +/* + * 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.example.bigquery; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class ExtractTableCompressedIT { + + private ByteArrayOutputStream bout; + private PrintStream out; + + private static final String GCS_BUCKET = System.getenv("GCS_BUCKET"); + + private static void requireEnvVar(String varName) { + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GCS_BUCKET"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testExtractTableCompressed() { + String projectId = "bigquery-public-data"; + String datasetName = "samples"; + String tableName = "shakespeare"; + String destinationUri = "gs://" + GCS_BUCKET + "/extractTest.csv"; + String dataFormat = "CSV"; + String compressed = "gzip"; + + // Extract table content to GCS in CSV format and gzip compressed + ExtractTableCompressed.extractTableCompressed( + projectId, datasetName, tableName, destinationUri, dataFormat, compressed); + assertThat(bout.toString()).contains("Table extract compressed successful"); + } +} diff --git a/samples/snippets/src/test/java/com/example/bigquery/GetDatasetLabelsIT.java b/samples/snippets/src/test/java/com/example/bigquery/GetDatasetLabelsIT.java new file mode 100644 index 000000000..f2661172d --- /dev/null +++ b/samples/snippets/src/test/java/com/example/bigquery/GetDatasetLabelsIT.java @@ -0,0 +1,79 @@ +/* + * 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.example.bigquery; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.UUID; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class GetDatasetLabelsIT { + + private String datasetName; + private ByteArrayOutputStream bout; + private PrintStream out; + + private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + // create a temporary dataset + datasetName = "MY_DATASET_TEST_" + UUID.randomUUID().toString().substring(0, 8); + CreateDataset.createDataset(datasetName); + // add a label on dataset + LabelDataset.labelDataset(datasetName); + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + // delete a temporary dataset + DeleteDataset.deleteDataset(PROJECT_ID, datasetName); + System.setOut(null); + } + + @Test + public void testGetDatasetLabels() { + GetDatasetLabels.getDatasetLabels(datasetName); + assertThat(bout.toString()).contains("Retrieved labels successfully"); + } +} diff --git a/samples/snippets/src/test/java/com/example/bigquery/GetTableLabelsIT.java b/samples/snippets/src/test/java/com/example/bigquery/GetTableLabelsIT.java new file mode 100644 index 000000000..11de46c39 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/bigquery/GetTableLabelsIT.java @@ -0,0 +1,64 @@ +package com.example.bigquery; + +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertNotNull; + +import com.google.cloud.bigquery.Schema; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.UUID; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class GetTableLabelsIT { + + private String tableName; + private ByteArrayOutputStream bout; + private PrintStream out; + + private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME"); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("BIGQUERY_DATASET_NAME"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + // create a temporary table + tableName = "MY_TABLE_TEST_" + UUID.randomUUID().toString().substring(0, 8); + CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, Schema.of()); + // add label on a table + LabelTable.labelTable(BIGQUERY_DATASET_NAME, tableName); + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + // delete a temporary table + DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName); + System.setOut(null); + } + + @Test + public void testGetTableLabels() { + GetTableLabels.getTableLabels(BIGQUERY_DATASET_NAME, tableName); + assertThat(bout.toString()).contains("Retrieved labels successfully"); + } +} diff --git a/samples/snippets/src/test/java/com/example/bigquery/GrantViewAccessIT.java b/samples/snippets/src/test/java/com/example/bigquery/GrantViewAccessIT.java new file mode 100644 index 000000000..9ee22a785 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/bigquery/GrantViewAccessIT.java @@ -0,0 +1,103 @@ +/* + * 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.example.bigquery; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.bigquery.Field; +import com.google.cloud.bigquery.Schema; +import com.google.cloud.bigquery.StandardSQLTypeName; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.UUID; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class GrantViewAccessIT { + + private String datasetName; + private String tableName; + private String viewName; + private ByteArrayOutputStream bout; + private PrintStream out; + + private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME"); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + requireEnvVar("BIGQUERY_DATASET_NAME"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + + // create a temporary dataset, table and view to be deleted. + datasetName = "MY_DATASET_NAME_TEST_" + UUID.randomUUID().toString().substring(0, 8); + tableName = "MY_TABLE_NAME_TEST_" + UUID.randomUUID().toString().substring(0, 8); + viewName = "MY_VIEW_NAME_TEST_" + UUID.randomUUID().toString().substring(0, 8); + + CreateDataset.createDataset(datasetName); + + Schema schema = + Schema.of( + Field.of("timestampField", StandardSQLTypeName.TIMESTAMP), + Field.of("stringField", StandardSQLTypeName.STRING), + Field.of("booleanField", StandardSQLTypeName.BOOL)); + CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, schema); + + String query = + String.format( + "SELECT timestampField, stringField, booleanField FROM %s.%s", + BIGQUERY_DATASET_NAME, tableName); + CreateView.createView(BIGQUERY_DATASET_NAME, viewName, query); + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + // Clean up + DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, viewName); + DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName); + DeleteDataset.deleteDataset(PROJECT_ID, datasetName); + System.setOut(null); + } + + @Test + public void testGrantViewAccess() { + GrantViewAccess.grantViewAccess(datasetName, BIGQUERY_DATASET_NAME, viewName); + assertThat(bout.toString()).contains("Grant view access successfully"); + } +} diff --git a/samples/snippets/src/test/java/com/example/bigquery/ListDatasetsByLabelIT.java b/samples/snippets/src/test/java/com/example/bigquery/ListDatasetsByLabelIT.java new file mode 100644 index 000000000..bc6300acd --- /dev/null +++ b/samples/snippets/src/test/java/com/example/bigquery/ListDatasetsByLabelIT.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.example.bigquery; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.UUID; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class ListDatasetsByLabelIT { + + private String datasetName; + private ByteArrayOutputStream bout; + private PrintStream out; + + private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + // create a temporary dataset + datasetName = "MY_DATASET_TEST_" + UUID.randomUUID().toString().substring(0, 8); + CreateDataset.createDataset(datasetName); + // add a label on dataset + LabelDataset.labelDataset(datasetName); + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + // delete a temporary dataset + DeleteDataset.deleteDataset(PROJECT_ID, datasetName); + System.setOut(null); + } + + @Test + public void testListDatasetsByLabel() { + String filter = "labels.color:green"; + ListDatasetsByLabel.listDatasetsByLabel(PROJECT_ID, filter); + assertThat(bout.toString()).contains("Success! Dataset ID"); + } +} diff --git a/samples/snippets/src/test/java/com/example/bigquery/ListJobsIT.java b/samples/snippets/src/test/java/com/example/bigquery/ListJobsIT.java new file mode 100644 index 000000000..19871a92d --- /dev/null +++ b/samples/snippets/src/test/java/com/example/bigquery/ListJobsIT.java @@ -0,0 +1,62 @@ +/* + * 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.example.bigquery; + +import static com.google.common.truth.Truth.assertThat; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class ListJobsIT { + + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + + // Create a new job + String query = + "SELECT name" + + " FROM `bigquery-public-data.usa_names.usa_1910_2013`" + + " WHERE state = 'TX'" + + " LIMIT 100;"; + CreateJob.createJob(query); + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + // Clean up + System.setOut(null); + } + + @Test + public void testListJobs() { + ListJobs.listJobs(); + assertThat(bout.toString()).contains("Success! Job ID"); + } +} diff --git a/samples/snippets/src/test/java/com/example/bigquery/ListRoutinesIT.java b/samples/snippets/src/test/java/com/example/bigquery/ListRoutinesIT.java new file mode 100644 index 000000000..de723e78b --- /dev/null +++ b/samples/snippets/src/test/java/com/example/bigquery/ListRoutinesIT.java @@ -0,0 +1,78 @@ +/* + * 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.example.bigquery; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.UUID; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class ListRoutinesIT { + + private String routineName; + private ByteArrayOutputStream bout; + private PrintStream out; + + private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME"); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("BIGQUERY_DATASET_NAME"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + + // Create a new routine to be deleted + routineName = "MY_ROUTINE_NAME_TEST_" + UUID.randomUUID().toString().substring(0, 8); + CreateRoutine.createRoutine(BIGQUERY_DATASET_NAME, routineName); + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + // Clean up + DeleteRoutine.deleteRoutine(BIGQUERY_DATASET_NAME, routineName); + System.setOut(null); + } + + @Test + public void testListRoutines() { + ListRoutines.listRoutines(BIGQUERY_DATASET_NAME); + assertThat(bout.toString()).contains("Success! Routine ID"); + } +} diff --git a/samples/snippets/src/test/java/com/example/bigquery/LoadAvroFromGCSIT.java b/samples/snippets/src/test/java/com/example/bigquery/LoadAvroFromGCSIT.java new file mode 100644 index 000000000..6ccbdea90 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/bigquery/LoadAvroFromGCSIT.java @@ -0,0 +1,64 @@ +package com.example.bigquery; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.bigquery.Schema; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.UUID; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class LoadAvroFromGCSIT { + + private String tableName; + private ByteArrayOutputStream bout; + private PrintStream out; + + private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME"); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("BIGQUERY_DATASET_NAME"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + + // Create a test table + tableName = "MY_LOAD_AVRO_TABLE_FROM_GCS_TEST_" + UUID.randomUUID().toString().substring(0, 8); + CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, Schema.of()); + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + // Clean up + DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName); + System.setOut(null); + } + + @Test + public void loadLoadAvroFromGCS() { + String sourceUri = "gs://cloud-samples-data/bigquery/us-states/us-states.avro"; + LoadAvroFromGCS.loadAvroFromGCS(BIGQUERY_DATASET_NAME, tableName, sourceUri); + assertThat(bout.toString()).contains("Avro from GCS successfully loaded in a table"); + } +} diff --git a/samples/snippets/src/test/java/com/example/bigquery/LoadAvroFromGCSTruncateIT.java b/samples/snippets/src/test/java/com/example/bigquery/LoadAvroFromGCSTruncateIT.java new file mode 100644 index 000000000..23e1868cb --- /dev/null +++ b/samples/snippets/src/test/java/com/example/bigquery/LoadAvroFromGCSTruncateIT.java @@ -0,0 +1,81 @@ +/* + * 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.example.bigquery; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.bigquery.Schema; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.UUID; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class LoadAvroFromGCSTruncateIT { + + private String tableName; + private ByteArrayOutputStream bout; + private PrintStream out; + + private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME"); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("BIGQUERY_DATASET_NAME"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + + // Create a test table + tableName = "MY_LOAD_AVRO_TABLE_FROM_GCS_TEST_" + UUID.randomUUID().toString().substring(0, 8); + CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, Schema.of()); + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + // Clean up + DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName); + System.setOut(null); + } + + @Test + public void loadLoadAvroFromGCSTruncate() { + String sourceUri = "gs://cloud-samples-data/bigquery/us-states/us-states.avro"; + LoadAvroFromGCSTruncate.loadAvroFromGCSTruncate(BIGQUERY_DATASET_NAME, tableName, sourceUri); + assertThat(bout.toString()) + .contains("Table is successfully overwritten by AVRO file loaded from GCS"); + } +} diff --git a/samples/snippets/src/test/java/com/example/bigquery/LoadJsonFromGCSAutodetectIT.java b/samples/snippets/src/test/java/com/example/bigquery/LoadJsonFromGCSAutodetectIT.java new file mode 100644 index 000000000..bc46a4021 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/bigquery/LoadJsonFromGCSAutodetectIT.java @@ -0,0 +1,83 @@ +/* + * 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.example.bigquery; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.bigquery.Schema; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.UUID; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class LoadJsonFromGCSAutodetectIT { + + private String tableName; + private ByteArrayOutputStream bout; + private PrintStream out; + + private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME"); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("BIGQUERY_DATASET_NAME"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + + // Create a test table + tableName = + "MY_LOAD_JSON_TABLE_AUTODETECT_FROM_GCS_TEST_" + + UUID.randomUUID().toString().substring(0, 8); + CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, Schema.of()); + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + // Clean up + DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName); + System.setOut(null); + } + + @Test + public void loadLoadJsonFromGCSAutodetect() { + String sourceUri = "gs://cloud-samples-data/bigquery/us-states/us-states.json"; + LoadJsonFromGCSAutodetect.loadJsonFromGCSAutodetect( + BIGQUERY_DATASET_NAME, tableName, sourceUri); + assertThat(bout.toString()).contains("Json Autodetect from GCS successfully loaded in a table"); + } +} diff --git a/samples/snippets/src/test/java/com/example/bigquery/LoadJsonFromGCSCMEKIT.java b/samples/snippets/src/test/java/com/example/bigquery/LoadJsonFromGCSCMEKIT.java new file mode 100644 index 000000000..b435f219d --- /dev/null +++ b/samples/snippets/src/test/java/com/example/bigquery/LoadJsonFromGCSCMEKIT.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.example.bigquery; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.bigquery.EncryptionConfiguration; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.UUID; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class LoadJsonFromGCSCMEKIT { + + private String tableName; + private ByteArrayOutputStream bout; + private PrintStream out; + + private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME"); + private static final String BIGQUERY_KMS_KEY_NAME = requireEnvVar("BIGQUERY_KMS_KEY_NAME"); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("BIGQUERY_DATASET_NAME"); + requireEnvVar("BIGQUERY_KMS_KEY_NAME"); + } + + @Before + public void setUp() { + tableName = + "MY_LOAD_JSON_TABLE_CMEK_FROM_GCS_TEST_" + UUID.randomUUID().toString().substring(0, 8); + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + // Clean up + DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName); + System.setOut(null); + } + + @Test + public void testLoadJsonFromGCSCMEK() { + String sourceUri = "gs://cloud-samples-data/bigquery/us-states/us-states.json"; + EncryptionConfiguration configuration = + EncryptionConfiguration.newBuilder().setKmsKeyName(BIGQUERY_KMS_KEY_NAME).build(); + LoadJsonFromGCSCMEK.loadJsonFromGCSCMEK( + BIGQUERY_DATASET_NAME, tableName, sourceUri, configuration); + assertThat(bout.toString()) + .contains("Table loaded succesfully from GCS with configuration key"); + } +} diff --git a/samples/snippets/src/test/java/com/example/bigquery/LoadJsonFromGCSIT.java b/samples/snippets/src/test/java/com/example/bigquery/LoadJsonFromGCSIT.java new file mode 100644 index 000000000..8b2cf28d9 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/bigquery/LoadJsonFromGCSIT.java @@ -0,0 +1,86 @@ +/* + * 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.example.bigquery; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.bigquery.Field; +import com.google.cloud.bigquery.Schema; +import com.google.cloud.bigquery.StandardSQLTypeName; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.UUID; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class LoadJsonFromGCSIT { + + private String tableName; + private ByteArrayOutputStream bout; + private PrintStream out; + + private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME"); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("BIGQUERY_DATASET_NAME"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + + // Create a test table + tableName = "MY_LOAD_JSON_TABLE_FROM_GCS_TEST_" + UUID.randomUUID().toString().substring(0, 8); + CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, Schema.of()); + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + // Clean up + DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName); + System.setOut(null); + } + + @Test + public void loadLoadJsonFromGCS() { + String sourceUri = "gs://cloud-samples-data/bigquery/us-states/us-states.json"; + Schema schema = + Schema.of( + Field.of("name", StandardSQLTypeName.STRING), + Field.of("post_abbr", StandardSQLTypeName.STRING)); + LoadJsonFromGCS.loadJsonFromGCS(BIGQUERY_DATASET_NAME, tableName, sourceUri, schema); + assertThat(bout.toString()).contains("Json from GCS successfully loaded in a table"); + } +} diff --git a/samples/snippets/src/test/java/com/example/bigquery/QueryDestinationTableCMEKIT.java b/samples/snippets/src/test/java/com/example/bigquery/QueryDestinationTableCMEKIT.java new file mode 100644 index 000000000..f4e0669d3 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/bigquery/QueryDestinationTableCMEKIT.java @@ -0,0 +1,93 @@ +/* + * 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.example.bigquery; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.bigquery.EncryptionConfiguration; +import com.google.cloud.bigquery.Field; +import com.google.cloud.bigquery.Schema; +import com.google.cloud.bigquery.StandardSQLTypeName; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.UUID; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class QueryDestinationTableCMEKIT { + + private String tableName; + private EncryptionConfiguration encryption; + private ByteArrayOutputStream bout; + private PrintStream out; + + private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME"); + private static final String BIGQUERY_KMS_KEY_NAME = requireEnvVar("BIGQUERY_KMS_KEY_NAME"); + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("BIGQUERY_DATASET_NAME"); + requireEnvVar("BIGQUERY_KMS_KEY_NAME"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + + // create a test table with encryption key + tableName = "MY_TABLE_CMEK_TEST" + UUID.randomUUID().toString().substring(0, 8); + Schema schema = + Schema.of( + Field.of("stringField", StandardSQLTypeName.STRING), + Field.of("booleanField", StandardSQLTypeName.BOOL)); + encryption = EncryptionConfiguration.newBuilder().setKmsKeyName(BIGQUERY_KMS_KEY_NAME).build(); + CreateTableCMEK.createTableCMEK(BIGQUERY_DATASET_NAME, tableName, schema, encryption); + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + // Clean up + DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName); + System.setOut(null); + } + + @Test + public void testQueryDestinationTableCMEK() { + String query = + String.format( + "SELECT stringField, booleanField FROM %s.%s", BIGQUERY_DATASET_NAME, tableName); + QueryDestinationTableCMEK.queryDestinationTableCMEK(query, encryption); + assertThat(bout.toString()).contains("Query performed successfully with encryption key."); + } +} diff --git a/samples/snippets/src/test/java/com/example/bigquery/TableInsertRowsIT.java b/samples/snippets/src/test/java/com/example/bigquery/TableInsertRowsIT.java index 527907dd9..0ed414568 100644 --- a/samples/snippets/src/test/java/com/example/bigquery/TableInsertRowsIT.java +++ b/samples/snippets/src/test/java/com/example/bigquery/TableInsertRowsIT.java @@ -33,6 +33,8 @@ import org.junit.Test; public class TableInsertRowsIT { + + private String tableName; private ByteArrayOutputStream bout; private PrintStream out; @@ -54,16 +56,8 @@ public void setUp() { bout = new ByteArrayOutputStream(); out = new PrintStream(bout); System.setOut(out); - } - - @After - public void tearDown() { - System.setOut(null); - } - @Test - public void testTableInsertRows() { - String tableName = "InsertRowsTestTable_" + UUID.randomUUID().toString().replace('-', '_'); + tableName = "INSERT_ROW_INTO_TABLE_TEST" + UUID.randomUUID().toString().substring(0, 8); Schema schema = Schema.of( Field.of("booleanField", LegacySQLTypeName.BOOLEAN), @@ -72,16 +66,26 @@ public void testTableInsertRows() { // Create table in dataset for testing CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, schema); + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + // Clean up + DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName); + System.setOut(null); + } + + @Test + public void testTableInsertRows() { // Create a row to insert Map rowContent = new HashMap<>(); rowContent.put("booleanField", true); rowContent.put("numericField", "3.14"); - // Testing TableInsertRows.tableInsertRows(BIGQUERY_DATASET_NAME, tableName, rowContent); assertThat(bout.toString()).contains("Rows successfully inserted into table"); - - // Clean up - DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName); } } diff --git a/samples/snippets/src/test/java/com/example/bigquery/TableInsertRowsWithoutRowIdsIT.java b/samples/snippets/src/test/java/com/example/bigquery/TableInsertRowsWithoutRowIdsIT.java new file mode 100644 index 000000000..8441fc738 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/bigquery/TableInsertRowsWithoutRowIdsIT.java @@ -0,0 +1,100 @@ +/* + * 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.example.bigquery; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.bigquery.Field; +import com.google.cloud.bigquery.InsertAllRequest; +import com.google.cloud.bigquery.LegacySQLTypeName; +import com.google.cloud.bigquery.Schema; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class TableInsertRowsWithoutRowIdsIT { + + private String tableName; + private ByteArrayOutputStream bout; + private PrintStream out; + + private static final String BIGQUERY_DATASET_NAME = System.getenv("BIGQUERY_DATASET_NAME"); + + private static void requireEnvVar(String varName) { + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("BIGQUERY_DATASET_NAME"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + + tableName = "INSERT_ROW_WITHOUT_ROW_ID_TEST" + UUID.randomUUID().toString().substring(0, 8); + Schema schema = + Schema.of( + Field.of("stringField", LegacySQLTypeName.STRING), + Field.of("numericField", LegacySQLTypeName.NUMERIC)); + + // Create table in dataset for testing + CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, schema); + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + // Clean up + DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName); + System.setOut(null); + } + + @Test + public void testTableInsertRowsWithoutRowIds() { + // Create rows to insert + Map rowContent1 = new HashMap<>(); + rowContent1.put("stringField", "Phred Phlyntstone"); + rowContent1.put("numericField", 32); + Map rowContent2 = new HashMap<>(); + rowContent2.put("stringField", "Wylma Phlyntstone"); + rowContent2.put("numericField", 29); + List rowContent = new ArrayList<>(); + rowContent.add(InsertAllRequest.RowToInsert.of(rowContent1)); + rowContent.add(InsertAllRequest.RowToInsert.of(rowContent2)); + TableInsertRowsWithoutRowIds.tableInsertRowsWithoutRowIds( + BIGQUERY_DATASET_NAME, tableName, rowContent); + assertThat(bout.toString()).contains("Rows successfully inserted into table without row ids"); + } +} diff --git a/synth.metadata b/synth.metadata index 5cfafaf99..df8e11068 100644 --- a/synth.metadata +++ b/synth.metadata @@ -4,7 +4,7 @@ "git": { "name": ".", "remote": "https://github.com/googleapis/java-bigquery.git", - "sha": "f356c715bd1783df389b0fb66a7bb8747375765c" + "sha": "8ed6bd679761744bed7f4dcb4f66bf6f37119154" } }, { diff --git a/versions.txt b/versions.txt index 07f4a4ab1..8cb38b122 100644 --- a/versions.txt +++ b/versions.txt @@ -1,4 +1,4 @@ # Format: # module:released-version:current-version -google-cloud-bigquery:1.116.6:1.116.6 \ No newline at end of file +google-cloud-bigquery:1.116.7:1.116.7 \ No newline at end of file