From 29882b7e7902d47c819e265e71f048a0da79d764 Mon Sep 17 00:00:00 2001 From: Jonathan Simon Date: Fri, 9 Nov 2018 10:10:36 -0800 Subject: [PATCH 1/3] Update Cloud Spanner sample to use latest client. --- spanner/cloud-client/pom.xml | 2 +- .../com/example/spanner/SpannerSample.java | 28 +++++++++++++++---- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/spanner/cloud-client/pom.xml b/spanner/cloud-client/pom.xml index 687a5077991..d7f71c67fc9 100644 --- a/spanner/cloud-client/pom.xml +++ b/spanner/cloud-client/pom.xml @@ -42,7 +42,7 @@ limitations under the License. com.google.cloud google-cloud-bom - 0.66.0-alpha + 0.70.0-alpha pom import diff --git a/spanner/cloud-client/src/main/java/com/example/spanner/SpannerSample.java b/spanner/cloud-client/src/main/java/com/example/spanner/SpannerSample.java index a849df70887..0983ce79d03 100644 --- a/spanner/cloud-client/src/main/java/com/example/spanner/SpannerSample.java +++ b/spanner/cloud-client/src/main/java/com/example/spanner/SpannerSample.java @@ -19,6 +19,7 @@ import static com.google.cloud.spanner.TransactionRunner.TransactionCallable; import static com.google.cloud.spanner.Type.StructField; +import com.google.api.gax.longrunning.OperationFuture; import com.google.cloud.spanner.Database; import com.google.cloud.spanner.DatabaseAdminClient; import com.google.cloud.spanner.DatabaseClient; @@ -30,6 +31,8 @@ import com.google.cloud.spanner.ReadOnlyTransaction; import com.google.cloud.spanner.ResultSet; import com.google.cloud.spanner.Spanner; +import com.google.cloud.spanner.SpannerException; +import com.google.cloud.spanner.SpannerExceptionFactory; import com.google.cloud.spanner.SpannerOptions; import com.google.cloud.spanner.Statement; import com.google.cloud.spanner.Struct; @@ -42,6 +45,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; /** @@ -132,7 +136,7 @@ static class Performance { // [START spanner_create_database] static void createDatabase(DatabaseAdminClient dbAdminClient, DatabaseId id) { - Operation op = + OperationFuture op = dbAdminClient.createDatabase( id.getInstanceId().getInstance(), id.getDatabase(), @@ -149,14 +153,20 @@ static void createDatabase(DatabaseAdminClient dbAdminClient, DatabaseId id) { + " AlbumTitle STRING(MAX)\n" + ") PRIMARY KEY (SingerId, AlbumId),\n" + " INTERLEAVE IN PARENT Singers ON DELETE CASCADE")); - Database db = op.waitFor().getResult(); - System.out.println("Created database [" + db.getId() + "]"); + try { + Database db = op.get(); + System.out.println("Created database [" + db.getId() + "]"); + } catch (ExecutionException e) { + throw (SpannerException) e.getCause(); + } catch (InterruptedException e) { + throw SpannerExceptionFactory.propagateInterrupt(e); + } } // [END spanner_create_database] // [START spanner_create_table_with_timestamp_column] static void createTableWithTimestamp(DatabaseAdminClient dbAdminClient, DatabaseId id) { - Operation op = + OperationFuture op = dbAdminClient.updateDatabaseDdl( id.getInstanceId().getInstance(), id.getDatabase(), @@ -170,8 +180,14 @@ static void createTableWithTimestamp(DatabaseAdminClient dbAdminClient, Database + ") PRIMARY KEY (SingerId, VenueId, EventDate),\n" + " INTERLEAVE IN PARENT Singers ON DELETE CASCADE"), null); - op.waitFor().getResult(); - System.out.println("Created Performances table in database: [" + id + "]"); + try { + op.get(); + System.out.println("Created Performances table in database: [" + id + "]"); + } catch (ExecutionException e) { + throw (SpannerException) e.getCause(); + } catch (InterruptedException e) { + throw SpannerExceptionFactory.propagateInterrupt(e); + } } // [END spanner_create_table_with_timestamp_column] From 359ab185a612f4e1ea92a986edbec77c094264f0 Mon Sep 17 00:00:00 2001 From: Jonathan Simon Date: Fri, 9 Nov 2018 16:52:34 -0800 Subject: [PATCH 2/3] Update additional methods to use new client. --- .../com/example/spanner/SpannerSample.java | 104 +++++++++++------- 1 file changed, 66 insertions(+), 38 deletions(-) diff --git a/spanner/cloud-client/src/main/java/com/example/spanner/SpannerSample.java b/spanner/cloud-client/src/main/java/com/example/spanner/SpannerSample.java index 0983ce79d03..d84087d6315 100644 --- a/spanner/cloud-client/src/main/java/com/example/spanner/SpannerSample.java +++ b/spanner/cloud-client/src/main/java/com/example/spanner/SpannerSample.java @@ -27,7 +27,6 @@ import com.google.cloud.spanner.Key; import com.google.cloud.spanner.KeySet; import com.google.cloud.spanner.Mutation; -import com.google.cloud.spanner.Operation; import com.google.cloud.spanner.ReadOnlyTransaction; import com.google.cloud.spanner.ResultSet; import com.google.cloud.spanner.Spanner; @@ -276,14 +275,21 @@ static void read(DatabaseClient dbClient) { // [START spanner_add_column] static void addMarketingBudget(DatabaseAdminClient adminClient, DatabaseId dbId) { - adminClient - .updateDatabaseDdl( - dbId.getInstanceId().getInstance(), - dbId.getDatabase(), - Arrays.asList("ALTER TABLE Albums ADD COLUMN MarketingBudget INT64"), - null) - .waitFor(); - System.out.println("Added MarketingBudget column"); + OperationFuture op = + adminClient + .updateDatabaseDdl( + dbId.getInstanceId().getInstance(), + dbId.getDatabase(), + Arrays.asList("ALTER TABLE Albums ADD COLUMN MarketingBudget INT64"), + null); + try { + op.get(); + System.out.println("Added MarketingBudget column"); + } catch (ExecutionException e) { + throw (SpannerException) e.getCause(); + } catch (InterruptedException e) { + throw SpannerExceptionFactory.propagateInterrupt(e); + } } // [END spanner_add_column] @@ -387,14 +393,21 @@ static void queryMarketingBudget(DatabaseClient dbClient) { // [START spanner_create_index] static void addIndex(DatabaseAdminClient adminClient, DatabaseId dbId) { - adminClient - .updateDatabaseDdl( - dbId.getInstanceId().getInstance(), - dbId.getDatabase(), - Arrays.asList("CREATE INDEX AlbumsByAlbumTitle ON Albums(AlbumTitle)"), - null) - .waitFor(); - System.out.println("Added AlbumsByAlbumTitle index"); + OperationFuture op = + adminClient + .updateDatabaseDdl( + dbId.getInstanceId().getInstance(), + dbId.getDatabase(), + Arrays.asList("CREATE INDEX AlbumsByAlbumTitle ON Albums(AlbumTitle)"), + null); + try { + op.get(); + System.out.println("Added AlbumsByAlbumTitle index"); + } catch (ExecutionException e) { + throw (SpannerException) e.getCause(); + } catch (InterruptedException e) { + throw SpannerExceptionFactory.propagateInterrupt(e); + } } // [END spanner_create_index] @@ -447,15 +460,23 @@ static void readUsingIndex(DatabaseClient dbClient) { // [START spanner_create_storing_index] static void addStoringIndex(DatabaseAdminClient adminClient, DatabaseId dbId) { - adminClient - .updateDatabaseDdl( - dbId.getInstanceId().getInstance(), - dbId.getDatabase(), - Arrays.asList( - "CREATE INDEX AlbumsByAlbumTitle2 ON Albums(AlbumTitle) STORING (MarketingBudget)"), - null) - .waitFor(); - System.out.println("Added AlbumsByAlbumTitle2 index"); + OperationFuture op = + adminClient + .updateDatabaseDdl( + dbId.getInstanceId().getInstance(), + dbId.getDatabase(), + Arrays.asList( + "CREATE INDEX AlbumsByAlbumTitle2 ON Albums(AlbumTitle) " + + "STORING (MarketingBudget)"), + null); + try { + op.get(); + System.out.println("Added AlbumsByAlbumTitle2 index"); + } catch (ExecutionException e) { + throw (SpannerException) e.getCause(); + } catch (InterruptedException e) { + throw SpannerExceptionFactory.propagateInterrupt(e); + } } // [END spanner_create_storing_index] @@ -525,16 +546,23 @@ static void readStaleData(DatabaseClient dbClient) { // [START spanner_add_timestamp_column] static void addCommitTimestamp(DatabaseAdminClient adminClient, DatabaseId dbId) { - adminClient - .updateDatabaseDdl( - dbId.getInstanceId().getInstance(), - dbId.getDatabase(), - Arrays.asList( - "ALTER TABLE Albums ADD COLUMN LastUpdateTime TIMESTAMP " - + "OPTIONS (allow_commit_timestamp=true)"), - null) - .waitFor(); - System.out.println("Added LastUpdateTime as a commit timestamp column in Albums table."); + OperationFuture op = + adminClient + .updateDatabaseDdl( + dbId.getInstanceId().getInstance(), + dbId.getDatabase(), + Arrays.asList( + "ALTER TABLE Albums ADD COLUMN LastUpdateTime TIMESTAMP " + + "OPTIONS (allow_commit_timestamp=true)"), + null); + try { + op.get(); + System.out.println("Added LastUpdateTime as a commit timestamp column in Albums table."); + } catch (ExecutionException e) { + throw (SpannerException) e.getCause(); + } catch (InterruptedException e) { + throw SpannerExceptionFactory.propagateInterrupt(e); + } } // [END spanner_add_timestamp_column] @@ -621,8 +649,8 @@ static void queryPerformancesTable(DatabaseClient dbClient) { .singleUse() .executeQuery( Statement.of( - "SELECT SingerId, VenueId, EventDate, Revenue, LastUpdateTime FROM Performances" - + " ORDER BY LastUpdateTime DESC")); + "SELECT SingerId, VenueId, EventDate, Revenue, LastUpdateTime " + + " FROM Performances ORDER BY LastUpdateTime DESC")); while (resultSet.next()) { System.out.printf( "%d %d %s %s %s\n", From 8ac711be9ef23e8b5243a9cea65cbf3eef205941 Mon Sep 17 00:00:00 2001 From: Jonathan Simon Date: Mon, 12 Nov 2018 16:01:07 -0800 Subject: [PATCH 3/3] Address review comments. --- .../com/example/spanner/SpannerSample.java | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/spanner/cloud-client/src/main/java/com/example/spanner/SpannerSample.java b/spanner/cloud-client/src/main/java/com/example/spanner/SpannerSample.java index d84087d6315..c686118422a 100644 --- a/spanner/cloud-client/src/main/java/com/example/spanner/SpannerSample.java +++ b/spanner/cloud-client/src/main/java/com/example/spanner/SpannerSample.java @@ -59,6 +59,8 @@ *
  • Writing data using a read-write transaction. *
  • Using an index to read and execute SQL queries over data. *
  • Using commit timestamp for tracking when a record was last updated. + *
  • Using Google API Extensions for Java to make thread-safe requests via + * long-running operations. http://googleapis.github.io/gax-java/ * */ public class SpannerSample { @@ -153,11 +155,15 @@ static void createDatabase(DatabaseAdminClient dbAdminClient, DatabaseId id) { + ") PRIMARY KEY (SingerId, AlbumId),\n" + " INTERLEAVE IN PARENT Singers ON DELETE CASCADE")); try { + // Initiate the request which returns an OperationFuture. Database db = op.get(); System.out.println("Created database [" + db.getId() + "]"); } catch (ExecutionException e) { + // If the operation failed during execution, expose the cause. throw (SpannerException) e.getCause(); } catch (InterruptedException e) { + // Throw when a thread is waiting, sleeping, or otherwise occupied, + // and the thread is interrupted, either before or during the activity. throw SpannerExceptionFactory.propagateInterrupt(e); } } @@ -180,11 +186,15 @@ static void createTableWithTimestamp(DatabaseAdminClient dbAdminClient, Database + " INTERLEAVE IN PARENT Singers ON DELETE CASCADE"), null); try { + // Initiate the request which returns an OperationFuture. op.get(); System.out.println("Created Performances table in database: [" + id + "]"); } catch (ExecutionException e) { + // If the operation failed during execution, expose the cause. throw (SpannerException) e.getCause(); } catch (InterruptedException e) { + // Throw when a thread is waiting, sleeping, or otherwise occupied, + // and the thread is interrupted, either before or during the activity. throw SpannerExceptionFactory.propagateInterrupt(e); } } @@ -283,11 +293,15 @@ static void addMarketingBudget(DatabaseAdminClient adminClient, DatabaseId dbId) Arrays.asList("ALTER TABLE Albums ADD COLUMN MarketingBudget INT64"), null); try { + // Initiate the request which returns an OperationFuture. op.get(); System.out.println("Added MarketingBudget column"); } catch (ExecutionException e) { + // If the operation failed during execution, expose the cause. throw (SpannerException) e.getCause(); } catch (InterruptedException e) { + // Throw when a thread is waiting, sleeping, or otherwise occupied, + // and the thread is interrupted, either before or during the activity. throw SpannerExceptionFactory.propagateInterrupt(e); } } @@ -401,11 +415,15 @@ static void addIndex(DatabaseAdminClient adminClient, DatabaseId dbId) { Arrays.asList("CREATE INDEX AlbumsByAlbumTitle ON Albums(AlbumTitle)"), null); try { + // Initiate the request which returns an OperationFuture. op.get(); System.out.println("Added AlbumsByAlbumTitle index"); } catch (ExecutionException e) { + // If the operation failed during execution, expose the cause. throw (SpannerException) e.getCause(); } catch (InterruptedException e) { + // Throw when a thread is waiting, sleeping, or otherwise occupied, + // and the thread is interrupted, either before or during the activity. throw SpannerExceptionFactory.propagateInterrupt(e); } } @@ -470,11 +488,15 @@ static void addStoringIndex(DatabaseAdminClient adminClient, DatabaseId dbId) { + "STORING (MarketingBudget)"), null); try { + // Initiate the request which returns an OperationFuture. op.get(); System.out.println("Added AlbumsByAlbumTitle2 index"); } catch (ExecutionException e) { + // If the operation failed during execution, expose the cause. throw (SpannerException) e.getCause(); } catch (InterruptedException e) { + // Throw when a thread is waiting, sleeping, or otherwise occupied, + // and the thread is interrupted, either before or during the activity. throw SpannerExceptionFactory.propagateInterrupt(e); } } @@ -556,11 +578,15 @@ static void addCommitTimestamp(DatabaseAdminClient adminClient, DatabaseId dbId) + "OPTIONS (allow_commit_timestamp=true)"), null); try { + // Initiate the request which returns an OperationFuture. op.get(); System.out.println("Added LastUpdateTime as a commit timestamp column in Albums table."); } catch (ExecutionException e) { + // If the operation failed during execution, expose the cause. throw (SpannerException) e.getCause(); } catch (InterruptedException e) { + // Throw when a thread is waiting, sleeping, or otherwise occupied, + // and the thread is interrupted, either before or during the activity. throw SpannerExceptionFactory.propagateInterrupt(e); } } @@ -650,7 +676,7 @@ static void queryPerformancesTable(DatabaseClient dbClient) { .executeQuery( Statement.of( "SELECT SingerId, VenueId, EventDate, Revenue, LastUpdateTime " - + " FROM Performances ORDER BY LastUpdateTime DESC")); + + "FROM Performances ORDER BY LastUpdateTime DESC")); while (resultSet.next()) { System.out.printf( "%d %d %s %s %s\n",