8000 feat(bigquery): add helper classes for cloud-client testing (1/3) (#1… · amirx-cloud/java-docs-samples@1975c4c · GitHub
[go: up one dir, main page]

Skip to content

Commit 1975c4c

Browse files
feat(bigquery): add helper classes for cloud-client testing (1/3) (GoogleCloudPlatform#10034)
* feat(bigquery): add helper classes for cloud-client testing * use consistent names and fields * add Util class for testing helpers
1 parent 5171dd5 commit 1975c4c

File tree

12 files changed

+886
-0
lines changed

12 files changed

+886
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<!--
3+
Copyright 2025 Google LLC
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
13+
-->
14+
<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">
15+
<modelVersion>4.0.0</modelVersion>
16+
<groupId>com.example.bigquery</groupId>
17+
<artifactId>cloud-client-snippets</artifactId>
18+
<packaging>jar</packaging>
19+
<name>Google Cloud BigQuery Cloud Client Snippets</name>
20+
21+
<!--
22+
The parent pom defines common style checks and testing strategies for our samples.
23+
Removing or replacing it should not affect the execution of the samples in anyway.
24+
-->
25+
<parent>
26+
<groupId>com.google.cloud.samples</groupId>
27+
<artifactId>shared-configuration</artifactId>
28+
<version>1.2.0</version>
29+
</parent>
30+
31+
<properties>
32+
<maven.compiler.target>21</maven.compiler.target>
33+
<maven.compiler.source>21</maven.compiler.source>
34+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
35+
</properties>
36+
37+
<dependencyManagement>
38+
<dependencies>
39+
<dependency>
40+
<groupId>com.google.cloud</groupId>
41+
<artifactId>libraries-bom</artifactId>
42+
<version>26.32.0</version>
43+
<type>pom</type>
44+
<scope>import</scope>
45+
</dependency>
46+
</dependencies>
47+
</dependencyManagement>
48+
49+
<dependencies>
50+
<dependency>
51+
<groupId>com.google.cloud</groupId>
52+
<artifactId>google-cloud-bigquery</artifactId>
53+
</dependency>
54+
<dependency>
55+
<groupId>junit</groupId>
56+
<artifactId>junit</artifactId>
57+
<version>4.13.2</version>
58+
<scope>test</scope>
59+
</dependency>
60+
<dependency>
61+
<groupId>com.google.truth</groupId>
62+
<artifactId>truth</artifactId>
63+
<version>1.4.4</version>
64+
<scope>test</scope>
65+
</dependency>
66+
</dependencies>
67+
</project>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigquery;
18+
19+
import com.google.cloud.bigquery.BigQuery;
20+
import com.google.cloud.bigquery.BigQueryException;
21+
import com.google.cloud.bigquery.BigQueryOptions;
22+
import com.google.cloud.bigquery.Dataset;
23+
import com.google.cloud.bigquery.DatasetId;
24+
import com.google.cloud.bigquery.DatasetInfo;
25+
26+
public class CreateDataset {
27+
public static void main(String[] args) {
28+
// TODO(developer): Replace these variables before running the sample.
29+
// Project where to create the dataset.
30+
String projectId = "MY_PROJECT_ID";
31+
String datasetName = "MY_DATASET_NAME";
32+
createDataset(projectId, datasetName);
33+
}
34+
35+
public static void createDataset(String projectId, String datasetName) {
36+
try {
37+
// Initialize client that will be used to send requests. This client only needs
38+
// to be created once, and can be reused for multiple requests.
39+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
40+
41+
String location = "US";
42+
43+
// Create datasetId with the projectId and the datasetName, and set it into the datasetInfo.
44+
DatasetId datasetId = DatasetId.of(projectId, datasetName);
45+
DatasetInfo datasetInfo = DatasetInfo.newBuilder(datasetId).setLocation(location).build();
46+
47+
// Create Dataset.
48+
Dataset dataset = bigquery.create(datasetInfo);
49+
System.out.println(
50+
"Dataset \"" + dataset.getDatasetId().getDataset() + "\" created successfully");
51+
} catch (BigQueryException e) {
52+
System.out.println("Dataset was not created. \n" + e.toString());
53+
}
54+
}
55+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigquery;
18+
19+
import com.google.cloud.bigquery.BigQuery;
20+
import com.google.cloud.bigquery.BigQueryException;
21+
import com.google.cloud.bigquery.BigQueryOptions;
22+
import com.google.cloud.bigquery.Field;
23+
import com.google.cloud.bigquery.Schema;
24+
import com.google.cloud.bigquery.StandardSQLTypeName;
25+
import com.google.cloud.bigquery.StandardTableDefinition;
26+
import com.google.cloud.bigquery.Table;
27+
import com.google.cloud.bigquery.TableDefinition;
28+
import com.google.cloud.bigquery.TableId;
29+
import com.google.cloud.bigquery.TableInfo;
30+
31+
public class CreateTable {
32+
33+
public static void main(String[] args) {
34+
// TODO(developer): Replace these variables before running the sample.
35+
// Project and dataset name to create a new table
36+
String projectId = "MY_PROJECT_ID";
37+
String datasetName = "MY_DATASET_NAME";
38+
String tableName = "MY_TABLE_NAME";
39+
40+
// Schema for a Google BigQuery Table.
41+
Schema schema =
42+
Schema.of(
43+
Field.of("stringField", StandardSQLTypeName.STRING),
44+
Field.of("isBooleanField", StandardSQLTypeName.BOOL));
45+
createTable(projectId, datasetName, tableName, schema);
46+
}
47+
48+
public static void createTable(
49+
String projectId, String datasetName, String tableName, Schema schema) {
50+
try {
51+
// Initialize client that will be used to send requests. This client only needs
52+
// to be created once, and can be reused for multiple requests.
53+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
54+
55+
// Create table identity given the projectId, the datasetName and the tableName.
56+
TableId tableId = TableId.of(projectId, datasetName, tableName);
57+
// Create table definition to build the table information
58+
TableDefinition tableDefinition = StandardTableDefinition.of(schema);
59+
TableInfo tableInfo = TableInfo.newBuilder(tableId, tableDefinition).build();
60+
61+
// Create table
62+
Table table = bigquery.create(tableInfo);
63+
System.out.println("Table \"" + table.getTableId().getTable() + "\" created successfully");
64+
} catch (BigQueryException e) {
65+
System.out.println("Table was not created. \n" + e.toString());
66+
}
67+
}
68+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigquery;
18+
19+
import com.google.cloud.bigquery.BigQuery;
20+
import com.google.cloud.bigquery.BigQueryException;
21+
import com.google.cloud.bigquery.BigQueryOptions;
22+
import com.google.cloud.bigquery.Table;
23+
import com.google.cloud.bigquery.TableId;
24+
import com.google.cloud.bigquery.TableInfo;
25+
import com.google.cloud.bigquery.ViewDefinition;
26+
27+
// Sample to create a view
28+
public class CreateView {
29+
30+
public static void main(String[] args) {
31+
// TODO(developer): Replace these variables before running the sample.
32+
// Project, dataset and table name to create a new view
33+
String projectId = "MY_PROJECT_ID";
34+
String datasetName = "MY_DATASET_NAME";
35+
String tableName = "MY_TABLE_NAME";
36+
String viewName = "MY_VIEW_NAME";
37+
String query =
38+
String.format("SELECT stringField, isBooleanField FROM %s.%s", datasetName, tableName);
39+
createView(projectId, datasetName, viewName, query);
40+
}
41+
42+
public static void createView(
43+
String projectId, String datasetName, String viewName, String query) {
44+
try {
45+
// Initialize client that will be used to send requests. This client only needs
46+
// to be created once, and can be reused for multiple requests.
47+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
48+
49+
// Create table identity given the projectId, the datasetName and the viewName.
50+
TableId tableId = TableId.of(projectId, datasetName, viewName);
51+
52+
// Create view definition to generate the table information.
53+
ViewDefinition viewDefinition =
54+
ViewDefinition.newBuilder(query).setUseLegacySql(false).build();
55+
TableInfo tableInfo = TableInfo.of(tableId, viewDefinition);
56+
57+
// Create view.
58+
Table view = bigquery.create(tableInfo);
59+
System.out.println("View \"" + view.getTableId().getTable() + "\" created successfully");
60+
} catch (BigQueryException e) {
61+
System.out.println("View was not created. \n" + e.toString());
62+
}
63+
}
64+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigquery;
18+
19+
import com.google.cloud.bigquery.BigQuery;
20+
import com.google.cloud.bigquery.BigQuery.DatasetDeleteOption;
21+
import com.google.cloud.bigquery.BigQueryException;
22+
import com.google.cloud.bigquery.BigQueryOptions;
23+
import com.google.cloud.bigquery.DatasetId;
24+
25+
public class DeleteDataset {
26+
27+
public static void main(String[] args) {
28+
// TODO(developer): Replace these variables before running the sample.
29+
// Project from which to delete the dataset
30+
String projectId = "MY_PROJECT_ID";
31+
String datasetName = "MY_DATASET_NAME";
32+
deleteDataset(projectId, datasetName);
33+
}
34+
35+
public static void deleteDataset(String projectId, String datasetName) {
36+
try {
37+
// Initialize client that will be used to send requests. This client only needs
38+
// to be created once, and can be reused for multiple requests.
39+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
40+
41+
// Create datasetId with the projectId and the datasetName.
42+
DatasetId datasetId = DatasetId.of(projectId, datasetName);
43+
44+
// Delete dataset.
45+
boolean success = bigquery.delete(datasetId, DatasetDeleteOption.deleteContents());
46+
if (success) {
47+
System.out.println("Dataset \"" + datasetName + "\" deleted successfully");
48+
} else {
49+
System.out.println("Dataset was not found");
50+
}
51+
} catch (BigQueryException e) {
52+
System.out.println("Dataset was not deleted. \n" + e.toString());
53+
}
54+
}
55+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigquery;
18+
19+
import com.google.cloud.bigquery.BigQuery;
20+
import com.google.cloud.bigquery.BigQueryException;
21+
import com.google.cloud.bigquery.BigQueryOptions;
22+
import com.google.cloud.bigquery.TableId;
23+
24+
public class DeleteTable {
25+
26+
public static void main(String[] args) {
27+
// TODO(developer): Replace these variables before running the sample.
28+
// Project, dataset and table name to create a new table
29+
String projectId = "MY_PROJECT_ID";
30+
String datasetName = "MY_DATASET_NAME";
31+
String tableName = "MY_TABLE_NAME";
32+
deleteTable(projectId, datasetName, tableName);
33+
}
34+
35+
public static void deleteTable(String projectId, String datasetName, String tableName) {
36+
try {
37+
// Initialize client that will be used to send requests. This client only needs
38+
// to be created once, and can be reused for multiple requests.
39+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
40+
41+
// Create table identity given the projectId, the datasetName and the tableName.
42+
TableId tableId = TableId.of(projectId, datasetName, tableName);
43+
44+
// Delete the table.
45+
boolean success = bigquery.delete(tableId);
46+
if (success) {
47+
System.out.println("Table \"" + tableName + "\" deleted successfully");
48+
} else {
49+
System.out.println("Table was not found");
50+
}
51+
} catch (BigQueryException e) {
52+
System.out.println("Table was not deleted. \n" + e.toString());
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)
0