Skip to content

Update Cloud Spanner sample to use latest client. #1256

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
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
2 changes: 1 addition & 1 deletion spanner/cloud-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ limitations under the License.
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-bom</artifactId>
<version>0.66.0-alpha</version>
<version>0.70.0-alpha</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@
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;
import com.google.cloud.spanner.DatabaseId;
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;
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;
Expand All @@ -42,6 +44,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
Expand All @@ -56,6 +59,8 @@
* <li>Writing data using a read-write transaction.
* <li>Using an index to read and execute SQL queries over data.
* <li>Using commit timestamp for tracking when a record was last updated.
* <li>Using Google API Extensions for Java to make thread-safe requests via
* long-running operations. http://googleapis.github.io/gax-java/
* </ul>
*/
public class SpannerSample {
Expand Down Expand Up @@ -132,7 +137,7 @@ static class Performance {

// [START spanner_create_database]
static void createDatabase(DatabaseAdminClient dbAdminClient, DatabaseId id) {
Operation<Database, CreateDatabaseMetadata> op =
OperationFuture<Database, CreateDatabaseMetadata> op =
dbAdminClient.createDatabase(
id.getInstanceId().getInstance(),
id.getDatabase(),
Expand All @@ -149,14 +154,24 @@ 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 {
// 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);
}
}
// [END spanner_create_database]

// [START spanner_create_table_with_timestamp_column]
static void createTableWithTimestamp(DatabaseAdminClient dbAdminClient, DatabaseId id) {
Operation<Void, UpdateDatabaseDdlMetadata> op =
OperationFuture<Void, UpdateDatabaseDdlMetadata> op =
dbAdminClient.updateDatabaseDdl(
id.getInstanceId().getInstance(),
id.getDatabase(),
Expand All @@ -170,8 +185,18 @@ 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 {
// 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);
}
}
// [END spanner_create_table_with_timestamp_column]

Expand Down Expand Up @@ -260,14 +285,25 @@ 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<Void, UpdateDatabaseDdlMetadata> op =
adminClient
.updateDatabaseDdl(
dbId.getInstanceId().getInstance(),
dbId.getDatabase(),
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);
}
}
// [END spanner_add_column]

Expand Down Expand Up @@ -371,14 +407,25 @@ 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<Void, UpdateDatabaseDdlMetadata> op =
adminClient
.updateDatabaseDdl(
dbId.getInstanceId().getInstance(),
dbId.getDatabase(),
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);
}
}
// [END spanner_create_index]

Expand Down Expand Up @@ -431,15 +478,27 @@ 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<Void, UpdateDatabaseDdlMetadata> op =
adminClient
.updateDatabaseDdl(
dbId.getInstanceId().getInstance(),
dbId.getDatabase(),
Arrays.asList(
"CREATE INDEX AlbumsByAlbumTitle2 ON Albums(AlbumTitle) "
+ "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);
}
}
// [END spanner_create_storing_index]

Expand Down Expand Up @@ -509,16 +568,27 @@ 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<Void, UpdateDatabaseDdlMetadata> op =
adminClient
.updateDatabaseDdl(
dbId.getInstanceId().getInstance(),
dbId.getDatabase(),
Arrays.asList(
"ALTER TABLE Albums ADD COLUMN LastUpdateTime TIMESTAMP "
+ "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);
}
}
// [END spanner_add_timestamp_column]

Expand Down Expand Up @@ -605,8 +675,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",
Expand Down