-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding samples for Jsonb data type (#2147)
* feat: adding samples for Jsonb data type * update copyright year * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * add duplicate keys sample * fix json Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
- Loading branch information
1 parent
4c29c17
commit 1112203
Showing
5 changed files
with
280 additions
and
1 deletion.
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
57 changes: 57 additions & 0 deletions
57
samples/snippets/src/main/java/com/example/spanner/AddJsonbColumnSample.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,57 @@ | ||
/* | ||
* Copyright 2022 Google Inc. | ||
* | ||
* 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.spanner; | ||
|
||
// [START spanner_postgresql_jsonb_add_column] | ||
import com.google.api.gax.longrunning.OperationFuture; | ||
import com.google.cloud.spanner.DatabaseAdminClient; | ||
import com.google.cloud.spanner.Spanner; | ||
import com.google.cloud.spanner.SpannerOptions; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
class AddJsonbColumnSample { | ||
|
||
static void addJsonbColumn() throws InterruptedException, ExecutionException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "my-project"; | ||
String instanceId = "my-instance"; | ||
String databaseId = "my-database"; | ||
|
||
try (Spanner spanner = | ||
SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) { | ||
DatabaseAdminClient adminClient = spanner.getDatabaseAdminClient(); | ||
addJsonbColumn(adminClient, instanceId, databaseId); | ||
} | ||
} | ||
|
||
static void addJsonbColumn(DatabaseAdminClient adminClient, String instanceId, String databaseId) | ||
throws InterruptedException, ExecutionException { | ||
OperationFuture<Void, UpdateDatabaseDdlMetadata> operation = | ||
adminClient.updateDatabaseDdl( | ||
instanceId, | ||
databaseId, | ||
ImmutableList.of("ALTER TABLE Venues ADD COLUMN VenueDetails JSONB"), | ||
null); | ||
// Wait for the operation to finish. | ||
// This will throw an ExecutionException if the operation fails. | ||
operation.get(); | ||
System.out.printf("Successfully added column `VenueDetails`%n"); | ||
} | ||
} | ||
// [END spanner_postgresql_jsonb_add_column] |
64 changes: 64 additions & 0 deletions
64
samples/snippets/src/main/java/com/example/spanner/QueryWithJsonbParameterSample.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 2022 Google Inc. | ||
* | ||
* 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.spanner; | ||
|
||
// [START spanner_postgresql_jsonb_query_parameter] | ||
import com.google.cloud.spanner.DatabaseClient; | ||
import com.google.cloud.spanner.DatabaseId; | ||
import com.google.cloud.spanner.ResultSet; | ||
import com.google.cloud.spanner.Spanner; | ||
import com.google.cloud.spanner.SpannerOptions; | ||
import com.google.cloud.spanner.Statement; | ||
import com.google.cloud.spanner.Value; | ||
|
||
class QueryWithJsonbParameterSample { | ||
|
||
static void queryWithJsonbParameter() { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "my-project"; | ||
String instanceId = "my-instance"; | ||
String databaseId = "my-database"; | ||
|
||
try (Spanner spanner = | ||
SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) { | ||
DatabaseClient client = | ||
spanner.getDatabaseClient(DatabaseId.of(projectId, instanceId, databaseId)); | ||
queryWithJsonbParameter(client); | ||
} | ||
} | ||
|
||
static void queryWithJsonbParameter(DatabaseClient client) { | ||
int rating = 2; | ||
Statement statement = | ||
Statement.newBuilder( | ||
"SELECT VenueId, VenueDetails\n" | ||
+ "FROM Venues\n" | ||
+ "WHERE CAST(venuedetails ->> 'rating' " | ||
+ "AS INTEGER) > $1") | ||
.bind("p1") | ||
.to(Value.int64(rating)) | ||
.build(); | ||
try (ResultSet resultSet = client.singleUse().executeQuery(statement)) { | ||
while (resultSet.next()) { | ||
System.out.printf( | ||
"VenueId: %s, VenueDetails: %s%n", | ||
resultSet.getLong("venueid"), resultSet.getPgJsonb("venuedetails")); | ||
} | ||
} | ||
} | ||
} | ||
// [END spanner_postgresql_jsonb_query_parameter] |
78 changes: 78 additions & 0 deletions
78
samples/snippets/src/main/java/com/example/spanner/UpdateJsonbDataSample.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,78 @@ | ||
/* | ||
* Copyright 2022 Google Inc. | ||
* | ||
* 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.spanner; | ||
|
||
// [START spanner_postgresql_jsonb_update_data] | ||
import com.google.cloud.spanner.DatabaseClient; | ||
import com.google.cloud.spanner.DatabaseId; | ||
import com.google.cloud.spanner.Mutation; | ||
import com.google.cloud.spanner.Spanner; | ||
import com.google.cloud.spanner.SpannerOptions; | ||
import com.google.cloud.spanner.Value; | ||
import com.google.common.collect.ImmutableList; | ||
|
||
class UpdateJsonbDataSample { | ||
|
||
static void updateJsonbData() { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "my-project"; | ||
String instanceId = "my-instance"; | ||
String databaseId = "my-database"; | ||
|
||
try (Spanner spanner = | ||
SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) { | ||
DatabaseClient client = | ||
spanner.getDatabaseClient(DatabaseId.of(projectId, instanceId, databaseId)); | ||
updateJsonbData(client); | ||
} | ||
} | ||
|
||
static void updateJsonbData(DatabaseClient client) { | ||
// PG JSONB takes the last value in the case of duplicate keys. | ||
// PG JSONB sorts first by key length and then lexicographically with | ||
// equivalent key length. | ||
client.write( | ||
ImmutableList.of( | ||
Mutation.newInsertOrUpdateBuilder("Venues") | ||
.set("VenueId") | ||
.to(4L) | ||
.set("VenueDetails") | ||
.to( | ||
Value.pgJsonb( | ||
"[{\"name\":\"room 1\",\"open\":true,\"name\":\"room 3\"}," | ||
+ "{\"name\":\"room 2\",\"open\":false}]")) | ||
.build(), | ||
Mutation.newInsertOrUpdateBuilder("Venues") | ||
.set("VenueId") | ||
.to(19L) | ||
.set("VenueDetails") | ||
.to(Value.pgJsonb("{\"rating\":9,\"open\":true}")) | ||
.build(), | ||
Mutation.newInsertOrUpdateBuilder("Venues") | ||
.set("VenueId") | ||
.to(42L) | ||
.set("VenueDetails") | ||
.to( | ||
Value.pgJsonb( | ||
"{\"name\":null," | ||
+ "\"open\":{\"Monday\":true,\"Tuesday\":false}," | ||
+ "\"tags\":[\"large\",\"airy\"]}")) | ||
.build())); | ||
System.out.println("Venues successfully updated"); | ||
} | ||
} | ||
// [END spanner_postgresql_jsonb_update_data] |
Oops, something went wrong.