60
60
* <li>Writing data using a read-write transaction.
61
61
* <li>Using an index to read and execute SQL queries over data.
62
62
* <li>Using commit timestamp for tracking when a record was last updated.
63
- * <li>Using Google API Extensions for Java to make thread-safe requests via
64
- * long-running operations. http://googleapis.github.io/gax-java/
63
+ * <li>Using Google API Extensions for Java to make thread-safe requests via long-running
64
+ * operations. http://googleapis.github.io/gax-java/
65
65
* </ul>
66
66
*/
67
67
public class SpannerSample {
@@ -262,13 +262,13 @@ static void deleteExampleData(DatabaseClient dbClient) {
262
262
// KeySet.singleKey() can be used to delete one row at a time.
263
263
for (Singer singer : SINGERS ) {
264
264
mutations .add (
265
- Mutation .delete ("Singers" ,
266
- KeySet .singleKey (Key .newBuilder ().append (singer .singerId ).build ())));
265
+ Mutation .delete (
266
+ "Singers" , KeySet .singleKey (Key .newBuilder ().append (singer .singerId ).build ())));
267
267
}
268
268
269
269
dbClient .write (mutations );
270
270
System .out .printf ("Records deleted.\n " );
271
- }
271
+ }
272
272
// [END spanner_delete_data]
273
273
274
274
// [START spanner_query_data]
@@ -306,12 +306,11 @@ static void read(DatabaseClient dbClient) {
306
306
// [START spanner_add_column]
307
307
static void addMarketingBudget (DatabaseAdminClient adminClient , DatabaseId dbId ) {
308
308
OperationFuture <Void , UpdateDatabaseDdlMetadata > op =
309
- adminClient
310
- .updateDatabaseDdl (
311
- dbId .getInstanceId ().getInstance (),
312
- dbId .getDatabase (),
313
- Arrays .asList ("ALTER TABLE Albums ADD COLUMN MarketingBudget INT64" ),
314
- null );
309
+ adminClient .updateDatabaseDdl (
310
+ dbId .getInstanceId ().getInstance (),
311
+ dbId .getDatabase (),
312
+ Arrays .asList ("ALTER TABLE Albums ADD COLUMN MarketingBudget INT64" ),
313
+ null );
315
314
try {
316
315
// Initiate the request which returns an OperationFuture.
317
316
op .get ();
@@ -372,12 +371,12 @@ public Void run(TransactionContext transaction) throws Exception {
372
371
// Transaction will only be committed if this condition still holds at the time of
373
372
// commit. Otherwise it will be aborted and the callable will be rerun by the
374
373
// client library.
375
- if (album2Budget >= 300000 ) {
374
+ long transfer = 200000 ;
375
+ if (album2Budget >= transfer ) {
376
376
long album1Budget =
377
377
transaction
378
378
.readRow ("Albums" , Key .of (1 , 1 ), Arrays .asList ("MarketingBudget" ))
379
379
.getLong (0 );
380
- long transfer = 200000 ;
381
380
album1Budget += transfer ;
382
381
album2Budget -= transfer ;
383
382
transaction .buffer (
@@ -428,12 +427,11 @@ static void queryMarketingBudget(DatabaseClient dbClient) {
428
427
// [START spanner_create_index]
429
428
static void addIndex (DatabaseAdminClient adminClient , DatabaseId dbId ) {
430
429
OperationFuture <Void , UpdateDatabaseDdlMetadata > op =
431
- adminClient
432
- .updateDatabaseDdl (
433
- dbId .getInstanceId ().getInstance (),
434
- dbId .getDatabase (),
435
- Arrays .asList ("CREATE INDEX AlbumsByAlbumTitle ON Albums(AlbumTitle)" ),
436
- null );
430
+ adminClient .updateDatabaseDdl (
431
+ dbId .getInstanceId ().getInstance (),
432
+ dbId .getDatabase (),
433
+ Arrays .asList ("CREATE INDEX AlbumsByAlbumTitle ON Albums(AlbumTitle)" ),
434
+ null );
437
435
try {
438
436
// Initiate the request which returns an OperationFuture.
439
437
op .get ();
@@ -499,14 +497,13 @@ static void readUsingIndex(DatabaseClient dbClient) {
499
497
// [START spanner_create_storing_index]
500
498
static void addStoringIndex (DatabaseAdminClient adminClient , DatabaseId dbId ) {
501
499
OperationFuture <Void , UpdateDatabaseDdlMetadata > op =
502
- adminClient
503
- .updateDatabaseDdl (
504
- dbId .getInstanceId ().getInstance (),
505
- dbId .getDatabase (),
506
- Arrays .asList (
507
- "CREATE INDEX AlbumsByAlbumTitle2 ON Albums(AlbumTitle) "
508
- + "STORING (MarketingBudget)" ),
509
- null );
500
+ adminClient .updateDatabaseDdl (
501
+ dbId .getInstanceId ().getInstance (),
502
+ dbId .getDatabase (),
503
+ Arrays .asList (
504
+ "CREATE INDEX AlbumsByAlbumTitle2 ON Albums(AlbumTitle) "
505
+ + "STORING (MarketingBudget)" ),
506
+ null );
510
507
try {
511
508
// Initiate the request which returns an OperationFuture.
512
509
op .get ();
@@ -589,14 +586,13 @@ static void readStaleData(DatabaseClient dbClient) {
589
586
// [START spanner_add_timestamp_column]
590
587
static void addCommitTimestamp (DatabaseAdminClient adminClient , DatabaseId dbId ) {
591
588
OperationFuture <Void , UpdateDatabaseDdlMetadata > op =
592
- adminClient
593
- .updateDatabaseDdl (
594
- dbId .getInstanceId ().getInstance (),
595
- dbId .getDatabase (),
596
- Arrays .asList (
597
- "ALTER TABLE Albums ADD COLUMN LastUpdateTime TIMESTAMP "
598
- + "OPTIONS (allow_commit_timestamp=true)" ),
599
- null );
589
+ adminClient .updateDatabaseDdl (
590
+ dbId .getInstanceId ().getInstance (),
591
+ dbId .getDatabase (),
592
+ Arrays .asList (
593
+ "ALTER TABLE Albums ADD COLUMN LastUpdateTime TIMESTAMP "
594
+ + "OPTIONS (allow_commit_timestamp=true)" ),
595
+ null );
600
596
try {
601
597
// Initiate the request which returns an OperationFuture.
602
598
op .get ();
@@ -675,9 +671,7 @@ static void querySingersTable(DatabaseClient dbClient) {
675
671
ResultSet resultSet =
676
672
dbClient
677
673
.singleUse ()
678
- .executeQuery (
679
- Statement .of (
680
- "SELECT SingerId, FirstName, LastName FROM Singers" ));
674
+ .executeQuery (Statement .of ("SELECT SingerId, FirstName, LastName FROM Singers" ));
681
675
while (resultSet .next ()) {
682
676
System .out .printf (
683
677
"%s %s %s\n " ,
@@ -1016,8 +1010,7 @@ public Void run(TransactionContext transaction) throws Exception {
1016
1010
// [START spanner_query_with_parameter]
1017
1011
static void queryWithParameter (DatabaseClient dbClient ) {
1018
1012
Statement statement =
1019
- Statement
1020
- .newBuilder (
1013
+ Statement .newBuilder (
1021
1014
"SELECT SingerId, FirstName, LastName\n "
1022
1015
+ "FROM Singers\n "
1023
1016
+ "WHERE LastName = @lastName" )
@@ -1047,40 +1040,40 @@ public Void run(TransactionContext transaction) throws Exception {
1047
1040
// Transfer marketing budget from one album to another. We do it in a transaction to
1048
1041
// ensure that the transfer is atomic.
1049
1042
String sql1 =
1050
- "SELECT MarketingBudget from Albums WHERE SingerId = 1 and AlbumId = 1 " ;
1043
+ "SELECT MarketingBudget from Albums WHERE SingerId = 2 and AlbumId = 2 " ;
1051
1044
ResultSet resultSet = transaction .executeQuery (Statement .of (sql1 ));
1052
- long album1Budget = 0 ;
1045
+ long album2Budget = 0 ;
1053
1046
while (resultSet .next ()) {
1054
- album1Budget = resultSet .getLong ("MarketingBudget" );
1047
+ album2Budget = resultSet .getLong ("MarketingBudget" );
1055
1048
}
1056
1049
// Transaction will only be committed if this condition still holds at the time of
1057
1050
// commit. Otherwise it will be aborted and the callable will be rerun by the
1058
1051
// client library.
1059
- if (album1Budget >= 300000 ) {
1052
+ long transfer = 200000 ;
1053
+ if (album2Budget >= transfer ) {
1060
1054
String sql2 =
1061
- "SELECT MarketingBudget from Albums WHERE SingerId = 2 and AlbumId = 2 " ;
1055
+ "SELECT MarketingBudget from Albums WHERE SingerId = 1 and AlbumId = 1 " ;
1062
1056
ResultSet resultSet2 = transaction .executeQuery (Statement .of (sql2 ));
1063
- long album2Budget = 0 ;
1064
- while (resultSet .next ()) {
1065
- album2Budget = resultSet2 .getLong ("MarketingBudget" );
1057
+ long album1Budget = 0 ;
1058
+ while (resultSet2 .next ()) {
1059
+ album1Budget = resultSet2 .getLong ("MarketingBudget" );
1066
1060
}
1067
- long transfer = 200000 ;
1068
- album2Budget += transfer ;
1069
- album1Budget -= transfer ;
1061
+ album1Budget += transfer ;
1062
+ album2Budget -= transfer ;
1070
1063
Statement updateStatement =
1071
1064
Statement .newBuilder (
1072
- "UPDATE Albums "
1073
- + "SET MarketingBudget = @AlbumBudget "
1074
- + "WHERE SingerId = 1 and AlbumId = 1" )
1065
+ "UPDATE Albums "
1066
+ + "SET MarketingBudget = @AlbumBudget "
1067
+ + "WHERE SingerId = 1 and AlbumId = 1" )
1075
1068
.bind ("AlbumBudget" )
1076
1069
.to (album1Budget )
1077
1070
.build ();
1078
1071
transaction .executeUpdate (updateStatement );
1079
1072
Statement updateStatement2 =
1080
1073
Statement .newBuilder (
1081
- "UPDATE Albums "
1082
- + "SET MarketingBudget = @AlbumBudget "
1083
- + "WHERE SingerId = 2 and AlbumId = 2" )
1074
+ "UPDATE Albums "
1075
+ + "SET MarketingBudget = @AlbumBudget "
1076
+ + "WHERE SingerId = 2 and AlbumId = 2" )
1084
1077
.bind ("AlbumBudget" )
1085
1078
.to (album2Budget )
1086
1079
.build ();
@@ -1108,7 +1101,7 @@ static void deleteUsingPartitionedDml(DatabaseClient dbClient) {
1108
1101
}
1109
1102
// [END spanner_dml_partitioned_delete]
1110
1103
1111
- // [START spanner_dml_batch_update]
1104
+ // [START spanner_dml_batch_update]
1112
1105
static void updateUsingBatchDml (DatabaseClient dbClient ) {
1113
1106
dbClient
1114
1107
.readWriteTransaction ()
@@ -1117,23 +1110,24 @@ static void updateUsingBatchDml(DatabaseClient dbClient) {
1117
1110
@ Override
1118
1111
public Void run (TransactionContext transaction ) throws Exception {
1119
1112
List <Statement > stmts = new ArrayList <Statement >();
1120
- String sql = "INSERT INTO Albums "
1121
- + "(SingerId, AlbumId, AlbumTitle, MarketingBudget) "
1122
- + "VALUES (1, 3, 'Test Album Title', 10000) " ;
1113
+ String sql =
1114
+ "INSERT INTO Albums "
1115
+ + "(SingerId, AlbumId, AlbumTitle, MarketingBudget) "
1116
+ + "VALUES (1, 3, 'Test Album Title', 10000) " ;
1123
1117
stmts .add (Statement .of (sql ));
1124
- sql = "UPDATE Albums "
1118
+ sql =
1119
+ "UPDATE Albums "
1125
1120
+ "SET MarketingBudget = MarketingBudget * 2 "
1126
1121
+ "WHERE SingerId = 1 and AlbumId = 3" ;
1127
1122
stmts .add (Statement .of (sql ));
1128
- long [] rowCounts ;
1123
+ long [] rowCounts ;
1129
1124
try {
1130
1125
rowCounts = transaction .batchUpdate (stmts );
1131
1126
} catch (SpannerBatchUpdateException e ) {
1132
1127
rowCounts = e .getUpdateCounts ();
1133
1128
}
1134
1129
for (int i = 0 ; i < rowCounts .length ; i ++) {
1135
- System .out .printf (
1136
- "%d record updated by stmt %d.\n " , rowCounts [i ], i );
1130
+ System .out .printf ("%d record updated by stmt %d.\n " , rowCounts [i ], i );
1137
1131
}
1138
1132
return null ;
1139
1133
}
@@ -1298,7 +1292,7 @@ static void printUsageAndExit() {
1298
1292
System .err .println (" SpannerExample querywithtimestamp my-instance example-db" );
1299
1293
System .err .println (" SpannerExample createtablewithtimestamp my-instance example-db" );
1300
1294
System .err .println (" SpannerExample writewithtimestamp my-instance example-db" );
1301
- System .err .println (" SpannerExample querysingerstable my-instance example-db" );
1295
+ System .err .println (" SpannerExample querysingerstable my-instance example-db" );
1302
1296
System .err .println (" SpannerExample queryperformancestable my-instance example-db" );
1303
1297
System .err .println (" SpannerExample writestructdata my-instance example-db" );
1304
1298
System .err .println (" SpannerExample querywithstruct my-instance example-db" );
0 commit comments