-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bigquery): add helper classes for cloud-client testing (1/3) (#1…
…0034) * feat(bigquery): add helper classes for cloud-client testing * use consistent names and fields * add Util class for testing helpers
- Loading branch information
1 parent
5171dd5
commit 1975c4c
Showing
12 changed files
with
886 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?xml version='1.0' encoding='UTF-8'?> | ||
<!-- | ||
Copyright 2025 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. | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.example.bigquery</groupId> | ||
<artifactId>cloud-client-snippets</artifactId> | ||
<packaging>jar</packaging> | ||
<name>Google Cloud BigQuery Cloud Client Snippets</name> | ||
|
||
<!-- | ||
The parent pom defines common style checks and testing strategies for our samples. | ||
Removing or replacing it should not affect the execution of the samples in anyway. | ||
--> | ||
<parent> | ||
<groupId>com.google.cloud.samples</groupId> | ||
<artifactId>shared-configuration</artifactId> | ||
<version>1.2.0</version> | ||
</parent> | ||
|
||
<properties> | ||
<maven.compiler.target>21</maven.compiler.target> | ||
<maven.compiler.source>21</maven.compiler.source> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>libraries-bom</artifactId> | ||
<version>26.32.0</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>google-cloud-bigquery</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.13.2</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.truth</groupId> | ||
<artifactId>truth</artifactId> | ||
<version>1.4.4</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
55 changes: 55 additions & 0 deletions
55
bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/CreateDataset.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2025 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 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.DatasetInfo; | ||
|
||
public class CreateDataset { | ||
public static void main(String[] args) { | ||
// TODO(developer): Replace these variables before running the sample. | ||
// Project where to create the dataset. | ||
String projectId = "MY_PROJECT_ID"; | ||
String datasetName = "MY_DATASET_NAME"; | ||
createDataset(projectId, datasetName); | ||
} | ||
|
||
public static void createDataset(String projectId, 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(); | ||
|
||
String location = "US"; | ||
|
||
// Create datasetId with the projectId and the datasetName, and set it into the datasetInfo. | ||
DatasetId datasetId = DatasetId.of(projectId, datasetName); | ||
DatasetInfo datasetInfo = DatasetInfo.newBuilder(datasetId).setLocation(location).build(); | ||
|
||
// Create Dataset. | ||
Dataset dataset = bigquery.create(datasetInfo); | ||
System.out.println( | ||
"Dataset \"" + dataset.getDatasetId().getDataset() + "\" created successfully"); | ||
} catch (BigQueryException e) { | ||
System.out.println("Dataset was not created. \n" + e.toString()); | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/CreateTable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright 2025 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 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.Schema; | ||
import com.google.cloud.bigquery.StandardSQLTypeName; | ||
import com.google.cloud.bigquery.StandardTableDefinition; | ||
import com.google.cloud.bigquery.Table; | ||
import com.google.cloud.bigquery.TableDefinition; | ||
import com.google.cloud.bigquery.TableId; | ||
import com.google.cloud.bigquery.TableInfo; | ||
|
||
public class CreateTable { | ||
|
||
public static void main(String[] args) { | ||
// TODO(developer): Replace these variables before running the sample. | ||
// Project and dataset name to create a new table | ||
String projectId = "MY_PROJECT_ID"; | ||
String datasetName = "MY_DATASET_NAME"; | ||
String tableName = "MY_TABLE_NAME"; | ||
|
||
// Schema for a Google BigQuery Table. | ||
Schema schema = | ||
Schema.of( | ||
Field.of("stringField", StandardSQLTypeName.STRING), | ||
Field.of("isBooleanField", StandardSQLTypeName.BOOL)); | ||
createTable(projectId, datasetName, tableName, schema); | ||
} | ||
|
||
public static void createTable( | ||
String projectId, String datasetName, String tableName, 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(); | ||
|
||
// Create table identity given the projectId, the datasetName and the tableName. | ||
TableId tableId = TableId.of(projectId, datasetName, tableName); | ||
// Create table definition to build the table information | ||
TableDefinition tableDefinition = StandardTableDefinition.of(schema); | ||
TableInfo tableInfo = TableInfo.newBuilder(tableId, tableDefinition).build(); | ||
|
||
// Create table | ||
Table table = bigquery.create(tableInfo); | ||
System.out.println("Table \"" + table.getTableId().getTable() + "\" created successfully"); | ||
} catch (BigQueryException e) { | ||
System.out.println("Table was not created. \n" + e.toString()); | ||
} | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/CreateView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2025 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 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; | ||
import com.google.cloud.bigquery.TableInfo; | ||
import com.google.cloud.bigquery.ViewDefinition; | ||
|
||
// Sample to create a view | ||
public class CreateView { | ||
|
||
public static void main(String[] args) { | ||
// TODO(developer): Replace these variables before running the sample. | ||
// Project, dataset and table name to create a new view | ||
String projectId = "MY_PROJECT_ID"; | ||
String datasetName = "MY_DATASET_NAME"; | ||
String tableName = "MY_TABLE_NAME"; | ||
String viewName = "MY_VIEW_NAME"; | ||
String query = | ||
String.format("SELECT stringField, isBooleanField FROM %s.%s", datasetName, tableName); | ||
createView(projectId, datasetName, viewName, query); | ||
} | ||
|
||
public static void createView( | ||
String projectId, String datasetName, String viewName, String query) { | ||
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(); | ||
|
||
// Create table identity given the projectId, the datasetName and the viewName. | ||
TableId tableId = TableId.of(projectId, datasetName, viewName); | ||
|
||
// Create view definition to generate the table information. | ||
ViewDefinition viewDefinition = | ||
ViewDefinition.newBuilder(query).setUseLegacySql(false).build(); | ||
TableInfo tableInfo = TableInfo.of(tableId, viewDefinition); | ||
|
||
// Create view. | ||
Table view = bigquery.create(tableInfo); | ||
System.out.println("View \"" + view.getTableId().getTable() + "\" created successfully"); | ||
} catch (BigQueryException e) { | ||
System.out.println("View was not created. \n" + e.toString()); | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/DeleteDataset.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2025 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 com.google.cloud.bigquery.BigQuery; | ||
import com.google.cloud.bigquery.BigQuery.DatasetDeleteOption; | ||
import com.google.cloud.bigquery.BigQueryException; | ||
import com.google.cloud.bigquery.BigQueryOptions; | ||
import com.google.cloud.bigquery.DatasetId; | ||
|
||
public class DeleteDataset { | ||
|
||
public static void main(String[] args) { | ||
// TODO(developer): Replace these variables before running the sample. | ||
// Project from which to delete the dataset | ||
String projectId = "MY_PROJECT_ID"; | ||
String datasetName = "MY_DATASET_NAME"; | ||
deleteDataset(projectId, datasetName); | ||
} | ||
|
||
public static void deleteDataset(String projectId, 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(); | ||
|
||
// Create datasetId with the projectId and the datasetName. | ||
DatasetId datasetId = DatasetId.of(projectId, datasetName); | ||
|
||
// Delete dataset. | ||
boolean success = bigquery.delete(datasetId, DatasetDeleteOption.deleteContents()); | ||
if (success) { | ||
System.out.println("Dataset \"" + datasetName + "\" deleted successfully"); | ||
} else { | ||
System.out.println("Dataset was not found"); | ||
} | ||
} catch (BigQueryException e) { | ||
System.out.println("Dataset was not deleted. \n" + e.toString()); | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
bigquery/cloud-client/snippets/src/main/java/com/example/bigquery/DeleteTable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2025 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 com.google.cloud.bigquery.BigQuery; | ||
import com.google.cloud.bigquery.BigQueryException; | ||
import com.google.cloud.bigquery.BigQueryOptions; | ||
import com.google.cloud.bigquery.TableId; | ||
|
||
public class DeleteTable { | ||
|
||
public static void main(String[] args) { | ||
// TODO(developer): Replace these variables before running the sample. | ||
// Project, dataset and table name to create a new table | ||
String projectId = "MY_PROJECT_ID"; | ||
String datasetName = "MY_DATASET_NAME"; | ||
String tableName = "MY_TABLE_NAME"; | ||
deleteTable(projectId, datasetName, tableName); | ||
} | ||
|
||
public static void deleteTable(String projectId, 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(); | ||
|
||
// Create table identity given the projectId, the datasetName and the tableName. | ||
TableId tableId = TableId.of(projectId, datasetName, tableName); | ||
|
||
// Delete the table. | ||
boolean success = bigquery.delete(tableId); | ||
if (success) { | ||
System.out.println("Table \"" + tableName + "\" deleted successfully"); | ||
} else { | ||
System.out.println("Table was not found"); | ||
} | ||
} catch (BigQueryException e) { | ||
System.out.println("Table was not deleted. \n" + e.toString()); | ||
} | ||
} | ||
} |
Oops, something went wrong.