-
Notifications
You must be signed in to change notification settings - Fork 132
feat: adding samples for Jsonb data type #2147
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.