Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bigquery): add helper classes for cloud-client testing (1/3) #10034

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions bigquery/cloud-client/snippets/pom.xml
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>
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: insert region tags. Make sure that you use the same region tags as used in the same sample (create dataset) in other languages. It looks like Go uses bigquery_create_dataset.

See https://googlecloudplatform.github.io/samples-style-guide/#region-tags.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now, those samples (with region tags) are in a different repo, but I need the classes to test the samples in the list. I'm planning to migrate the region tags and use the samples in this PR and repo once the PRs 10034, 10035 and 10036 are merged as that migration is not part of the current scope.

Is this a good way to go?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, that makes sense. I assume that there is a work item or bug that tracks adding region tags to these files?

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());
}
}
}
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());
}
}
}
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());
}
}
}
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());
}
}
}
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());
}
}
}
Loading