Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -42,34 +42,39 @@ static void addAndDropDatabaseRole() {
}

static void addAndDropDatabaseRole(
String projectId, String instanceId, String databaseId,
String parentRole, String childRole, String... tables) {
String projectId,
String instanceId,
String databaseId,
String parentRole,
String childRole,
String... tables) {
try (Spanner spanner =
SpannerOptions.newBuilder()
.setProjectId(projectId)
.build()
.getService();
SpannerOptions.newBuilder().setProjectId(projectId).build().getService();
DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) {
System.out.println("Waiting for role create operation to complete...");
List<String> roleStatements = new ArrayList<>(ImmutableList.of(
String.format("CREATE ROLE %s", parentRole),
String.format("CREATE ROLE %s", childRole),
String.format("GRANT ROLE %s TO ROLE %s", parentRole, childRole)));
List<String> roleStatements =
new ArrayList<>(
ImmutableList.of(
String.format("CREATE ROLE %s", parentRole),
String.format("CREATE ROLE %s", childRole),
String.format("GRANT ROLE %s TO ROLE %s", parentRole, childRole)));
for (String table : tables) {
roleStatements.add(String.format("GRANT SELECT ON TABLE %s TO ROLE %s", table, parentRole));
}
databaseAdminClient.updateDatabaseDdlAsync(
databaseAdminClient
.updateDatabaseDdlAsync(
DatabaseName.of(projectId, instanceId, databaseId), roleStatements)
.get(5, TimeUnit.MINUTES);
System.out.printf(
"Created roles %s and %s and granted privileges%n", parentRole, childRole);
System.out.printf("Created roles %s and %s and granted privileges%n", parentRole, childRole);
// Delete role and membership.
System.out.println("Waiting for role revoke & drop operation to complete...");
databaseAdminClient.updateDatabaseDdlAsync(
DatabaseName.of(projectId, instanceId, databaseId),
ImmutableList.of(
String.format("REVOKE ROLE %s FROM ROLE %s", parentRole, childRole),
String.format("DROP ROLE %s", childRole))).get(5, TimeUnit.MINUTES);
databaseAdminClient
.updateDatabaseDdlAsync(
DatabaseName.of(projectId, instanceId, databaseId),
ImmutableList.of(
String.format("REVOKE ROLE %s FROM ROLE %s", parentRole, childRole),
String.format("DROP ROLE %s", childRole)))
.get(5, TimeUnit.MINUTES);
System.out.printf("Revoked privileges and dropped role %s%n", childRole);
} catch (ExecutionException | TimeoutException e) {
System.out.printf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,15 @@ static void addJsonColumn() throws InterruptedException, ExecutionException {
static void addJsonColumn(String projectId, String instanceId, String databaseId)
throws InterruptedException, ExecutionException {
try (Spanner spanner =
SpannerOptions.newBuilder()
.setProjectId(projectId)
.build()
.getService();
SpannerOptions.newBuilder().setProjectId(projectId).build().getService();
DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) {
// Wait for the operation to finish.
// This will throw an ExecutionException if the operation fails.
databaseAdminClient.updateDatabaseDdlAsync(
DatabaseName.of(projectId, instanceId, databaseId),
ImmutableList.of("ALTER TABLE Venues ADD COLUMN VenueDetails JSON")).get();
databaseAdminClient
.updateDatabaseDdlAsync(
DatabaseName.of(projectId, instanceId, databaseId),
ImmutableList.of("ALTER TABLE Venues ADD COLUMN VenueDetails JSON"))
.get();
System.out.printf("Successfully added column `VenueDetails`%n");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,16 @@ static void addJsonbColumn() throws InterruptedException, ExecutionException {
static void addJsonbColumn(String projectId, String instanceId, String databaseId)
throws InterruptedException, ExecutionException {
try (Spanner spanner =
SpannerOptions.newBuilder()
.setProjectId(projectId)
.build()
.getService();
SpannerOptions.newBuilder().setProjectId(projectId).build().getService();
DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) {
// JSONB datatype is only supported with PostgreSQL-dialect databases.
// Wait for the operation to finish.
// This will throw an ExecutionException if the operation fails.
databaseAdminClient.updateDatabaseDdlAsync(
DatabaseName.of(projectId, instanceId, databaseId),
ImmutableList.of("ALTER TABLE Venues ADD COLUMN VenueDetails JSONB")).get();
databaseAdminClient
.updateDatabaseDdlAsync(
DatabaseName.of(projectId, instanceId, databaseId),
ImmutableList.of("ALTER TABLE Venues ADD COLUMN VenueDetails JSONB"))
.get();
System.out.printf("Successfully added column `VenueDetails`%n");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,15 @@ static void addNumericColumn() throws InterruptedException, ExecutionException {
static void addNumericColumn(String projectId, String instanceId, String databaseId)
throws InterruptedException, ExecutionException {
try (Spanner spanner =
SpannerOptions.newBuilder()
.setProjectId(projectId)
.build()
.getService();
SpannerOptions.newBuilder().setProjectId(projectId).build().getService();
DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) {
// Wait for the operation to finish.
// This will throw an ExecutionException if the operation fails.
databaseAdminClient.updateDatabaseDdlAsync(
DatabaseName.of(projectId, instanceId, databaseId),
ImmutableList.of("ALTER TABLE Venues ADD COLUMN Revenue NUMERIC")).get();
databaseAdminClient
.updateDatabaseDdlAsync(
DatabaseName.of(projectId, instanceId, databaseId),
ImmutableList.of("ALTER TABLE Venues ADD COLUMN Revenue NUMERIC"))
.get();
System.out.printf("Successfully added column `Revenue`%n");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ static void alterSequence() {

static void alterSequence(String projectId, String instanceId, String databaseId) {
try (Spanner spanner =
SpannerOptions.newBuilder().setProjectId(projectId).build().getService();
SpannerOptions.newBuilder().setProjectId(projectId).build().getService();
DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) {

databaseAdminClient
.updateDatabaseDdlAsync(DatabaseName.of(projectId, instanceId, databaseId),
.updateDatabaseDdlAsync(
DatabaseName.of(projectId, instanceId, databaseId),
ImmutableList.of(
"ALTER SEQUENCE Seq SET OPTIONS "
+ "(skip_range_min = 1000, skip_range_max = 5000000)"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ static void alterForeignKeyDeleteCascadeConstraint() {
static void alterForeignKeyDeleteCascadeConstraint(
String projectId, String instanceId, String databaseId) {
try (Spanner spanner =
SpannerOptions.newBuilder().setProjectId(projectId).build().getService();
SpannerOptions.newBuilder().setProjectId(projectId).build().getService();
DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) {
databaseAdminClient.updateDatabaseDdlAsync(DatabaseName.of(projectId, instanceId,
databaseId),
databaseAdminClient.updateDatabaseDdlAsync(
DatabaseName.of(projectId, instanceId, databaseId),
ImmutableList.of(
"ALTER TABLE ShoppingCarts\n"
+ " ADD CONSTRAINT FKShoppingCartsCustomerName\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@
// [START spanner_async_dml_standard_insert]
import com.google.api.core.ApiFuture;
import com.google.cloud.spanner.AsyncRunner;
import com.google.cloud.spanner.AsyncRunner.AsyncWork;
import com.google.cloud.spanner.DatabaseClient;
import com.google.cloud.spanner.DatabaseId;
import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.SpannerOptions;
import com.google.cloud.spanner.Statement;
import com.google.cloud.spanner.TransactionContext;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -68,4 +66,4 @@ static void asyncDml(DatabaseClient client)
executor.shutdown();
}
}
//[END spanner_async_dml_standard_insert]
// [END spanner_async_dml_standard_insert]
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,4 @@ public CallbackResponse cursorReady(AsyncResultSet resultSet) {
executor.shutdown();
}
}
//[END spanner_async_query_data]
// [END spanner_async_query_data]
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
* Example code for using Async query on Cloud Spanner and convert it to list.
*/
/** Example code for using Async query on Cloud Spanner and convert it to list. */
class AsyncQueryToListAsyncExample {
static class Album {
final long singerId;
Expand Down Expand Up @@ -88,4 +86,4 @@ static void asyncQueryToList(DatabaseClient client)
executor.shutdown();
}
}
//[END spanner_async_query_to_list]
// [END spanner_async_query_to_list]
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,4 @@ public CallbackResponse cursorReady(AsyncResultSet resultSet) {
executor.shutdown();
}
}
//[END spanner_async_read_data]
// [END spanner_async_read_data]
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,4 @@ public CallbackResponse cursorReady(AsyncResultSet resultSet) {
executor.shutdown();
}
}
//[END spanner_async_read_only_transaction]
// [END spanner_async_read_only_transaction]
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.example.spanner;

//[START spanner_async_read_row]
// [START spanner_async_read_row]
import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutureCallback;
import com.google.api.core.ApiFutures;
Expand Down Expand Up @@ -81,4 +81,4 @@ public void onSuccess(Struct result) {
printed.get(30L, TimeUnit.SECONDS);
}
}
//[END spanner_async_read_row]
// [END spanner_async_read_row]
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.example.spanner;

//[START spanner_async_read_data_with_index]
// [START spanner_async_read_data_with_index]
import com.google.api.core.ApiFuture;
import com.google.cloud.spanner.AsyncResultSet;
import com.google.cloud.spanner.AsyncResultSet.CallbackResponse;
Expand Down Expand Up @@ -109,4 +109,4 @@ public CallbackResponse cursorReady(AsyncResultSet resultSet) {
executor.shutdown();
}
}
//[END spanner_async_read_data_with_index]
// [END spanner_async_read_data_with_index]
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@

package com.example.spanner;

//[START spanner_async_read_write_transaction]
// [START spanner_async_read_write_transaction]
import com.google.api.core.ApiFunction;
import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutures;
import com.google.cloud.spanner.AsyncRunner;
import com.google.cloud.spanner.AsyncRunner.AsyncWork;
import com.google.cloud.spanner.DatabaseClient;
import com.google.cloud.spanner.DatabaseId;
import com.google.cloud.spanner.Key;
Expand All @@ -30,7 +29,6 @@
import com.google.cloud.spanner.SpannerOptions;
import com.google.cloud.spanner.Statement;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

critical

The removal of this import causes a compilation error because TransactionContext is still used in the AsyncRunner.AsyncWork implementation.

Suggested change
import com.google.cloud.spanner.Statement;
import com.google.cloud.spanner.TransactionContext;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

These two imports are not used in this file.

import com.google.cloud.spanner.Struct;
import com.google.cloud.spanner.TransactionContext;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.MoreExecutors;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
Expand Down Expand Up @@ -103,8 +101,7 @@ static void asyncRunner(DatabaseClient client)
.bind("AlbumBudget")
.to(album2Budget)
.build();
return txn.batchUpdateAsync(
ImmutableList.of(updateStatement1, updateStatement2));
return txn.batchUpdateAsync(ImmutableList.of(updateStatement1, updateStatement2));
} else {
return ApiFutures.immediateFuture(new long[] {0L, 0L});
}
Expand All @@ -131,4 +128,4 @@ public Long apply(long[] input) {
executor.shutdown();
}
}
//[END spanner_async_read_write_transaction]
// [END spanner_async_read_write_transaction]
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.example.spanner;

//[START spanner_async_transaction_manager]
// [START spanner_async_transaction_manager]
import com.google.api.core.ApiFunction;
import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutures;
Expand Down Expand Up @@ -72,51 +72,52 @@ static void asyncTransactionManager(DatabaseClient client)
try {
updateCounts =
txn.then(
(transaction, v) -> {
// Execute two reads in parallel and return the result of these as the input
// for the next step of the transaction.
ApiFuture<Struct> album1BudgetFut =
transaction.readRowAsync(
"Albums", Key.of(1, 1), ImmutableList.of("MarketingBudget"));
ApiFuture<Struct> album2BudgetFut =
transaction.readRowAsync(
"Albums", Key.of(2, 2), ImmutableList.of("MarketingBudget"));
return ApiFutures.allAsList(Arrays.asList(album1BudgetFut, album2BudgetFut));
},
executor)
(transaction, v) -> {
// Execute two reads in parallel and return the result of these as the input
// for the next step of the transaction.
ApiFuture<Struct> album1BudgetFut =
transaction.readRowAsync(
"Albums", Key.of(1, 1), ImmutableList.of("MarketingBudget"));
ApiFuture<Struct> album2BudgetFut =
transaction.readRowAsync(
"Albums", Key.of(2, 2), ImmutableList.of("MarketingBudget"));
return ApiFutures.allAsList(
Arrays.asList(album1BudgetFut, album2BudgetFut));
},
executor)
// The input of the next step of the transaction is the return value of the
// previous step, i.e. a list containing the marketing budget of two Albums.
.then(
(transaction, budgets) -> {
long album1Budget = budgets.get(0).getLong(0);
long album2Budget = budgets.get(1).getLong(0);
long transfer = 200_000;
if (album2Budget >= transfer) {
album1Budget += transfer;
album2Budget -= transfer;
Statement updateStatement1 =
Statement.newBuilder(
"UPDATE Albums "
+ "SET MarketingBudget = @AlbumBudget "
+ "WHERE SingerId = 1 and AlbumId = 1")
.bind("AlbumBudget")
.to(album1Budget)
.build();
Statement updateStatement2 =
Statement.newBuilder(
"UPDATE Albums "
+ "SET MarketingBudget = @AlbumBudget "
+ "WHERE SingerId = 2 and AlbumId = 2")
.bind("AlbumBudget")
.to(album2Budget)
.build();
return transaction.batchUpdateAsync(
ImmutableList.of(updateStatement1, updateStatement2));
} else {
return ApiFutures.immediateFuture(new long[] {0L, 0L});
}
},
executor);
(transaction, budgets) -> {
long album1Budget = budgets.get(0).getLong(0);
long album2Budget = budgets.get(1).getLong(0);
long transfer = 200_000;
if (album2Budget >= transfer) {
album1Budget += transfer;
album2Budget -= transfer;
Statement updateStatement1 =
Statement.newBuilder(
"UPDATE Albums "
+ "SET MarketingBudget = @AlbumBudget "
+ "WHERE SingerId = 1 and AlbumId = 1")
.bind("AlbumBudget")
.to(album1Budget)
.build();
Statement updateStatement2 =
Statement.newBuilder(
"UPDATE Albums "
+ "SET MarketingBudget = @AlbumBudget "
+ "WHERE SingerId = 2 and AlbumId = 2")
.bind("AlbumBudget")
.to(album2Budget)
.build();
return transaction.batchUpdateAsync(
ImmutableList.of(updateStatement1, updateStatement2));
} else {
return ApiFutures.immediateFuture(new long[] {0L, 0L});
}
},
executor);
// Commit after the updates.
CommitTimestampFuture commitTsFut = updateCounts.commitAsync();
// Wait for the transaction to finish and execute a retry if necessary.
Expand Down Expand Up @@ -144,4 +145,4 @@ public Long apply(long[] input) {
executor.shutdown();
}
}
//[END spanner_async_transaction_manager]
// [END spanner_async_transaction_manager]
Loading
Loading