16
16
17
17
package com .example .spanner ;
18
18
19
- // [START transaction_import]
20
19
import static com .google .cloud .spanner .TransactionRunner .TransactionCallable ;
21
- // [END transaction_import]
22
20
23
21
import com .google .cloud .spanner .Database ;
24
22
import com .google .cloud .spanner .DatabaseAdminClient ;
25
23
import com .google .cloud .spanner .DatabaseClient ;
26
24
import com .google .cloud .spanner .DatabaseId ;
27
- // [START transaction_import]
28
25
import com .google .cloud .spanner .Key ;
29
- // [END transaction_import]
30
- // [START read_import]
31
26
import com .google .cloud .spanner .KeySet ;
32
- // [END read_import]
33
- // [START write_import]
34
27
import com .google .cloud .spanner .Mutation ;
35
- // [END write_import]
36
28
import com .google .cloud .spanner .Operation ;
37
- // [START read_only_transaction_import]
38
29
import com .google .cloud .spanner .ReadOnlyTransaction ;
39
- // [END read_only_transaction_import]
40
- // [START query_import]
41
30
import com .google .cloud .spanner .ResultSet ;
42
- // [END query_import]
43
31
import com .google .cloud .spanner .Spanner ;
44
32
import com .google .cloud .spanner .SpannerOptions ;
45
- // [START query_import]
46
33
import com .google .cloud .spanner .Statement ;
47
- // [END query_import]
48
- // [START transaction_import]
49
34
import com .google .cloud .spanner .Struct ;
35
+ import com .google .cloud .spanner .TimestampBound ;
50
36
import com .google .cloud .spanner .TransactionContext ;
51
- // [END transaction_import]
52
37
import com .google .spanner .admin .database .v1 .CreateDatabaseMetadata ;
53
-
54
- // [START write_import]
55
-
56
38
import java .util .ArrayList ;
57
- // [END write_import]
58
39
import java .util .Arrays ;
59
40
import java .util .List ;
41
+ import java .util .concurrent .TimeUnit ;
60
42
61
43
/**
62
44
* Example code for using the Cloud Spanner API. This example demonstrates all the common
67
49
* <li> Writing data using a read-write transaction.
68
50
* <li> Using an index to read and execute SQL queries over data.
69
51
* </ul>
70
- *
71
52
*/
72
53
public class SpannerSample {
73
- /** Class to contain singer sample data. */
54
+
55
+ /**
56
+ * Class to contain singer sample data.
57
+ */
74
58
static class Singer {
59
+
75
60
final long singerId ;
76
61
final String firstName ;
77
62
final String lastName ;
@@ -83,8 +68,11 @@ static class Singer {
83
68
}
84
69
}
85
70
86
- /** Class to contain album sample data. */
71
+ /**
72
+ * Class to contain album sample data.
73
+ */
87
74
static class Album {
75
+
88
76
final long singerId ;
89
77
final long albumId ;
90
78
final String albumTitle ;
@@ -116,22 +104,22 @@ static class Album {
116
104
117
105
static void createDatabase (DatabaseAdminClient dbAdminClient , DatabaseId id ) {
118
106
Operation <Database , CreateDatabaseMetadata > op = dbAdminClient
119
- .createDatabase (
120
- id .getInstanceId ().getInstance (),
121
- id .getDatabase (),
122
- Arrays .asList (
123
- "CREATE TABLE Singers (\n "
124
- + " SingerId INT64 NOT NULL,\n "
125
- + " FirstName STRING(1024),\n "
126
- + " LastName STRING(1024),\n "
127
- + " SingerInfo BYTES(MAX)\n "
128
- + ") PRIMARY KEY (SingerId)" ,
129
- "CREATE TABLE Albums (\n "
130
- + " SingerId INT64 NOT NULL,\n "
131
- + " AlbumId INT64 NOT NULL,\n "
132
- + " AlbumTitle STRING(MAX)\n "
133
- + ") PRIMARY KEY (SingerId, AlbumId),\n "
134
- + " INTERLEAVE IN PARENT Singers ON DELETE CASCADE" ));
107
+ .createDatabase (
108
+ id .getInstanceId ().getInstance (),
109
+ id .getDatabase (),
110
+ Arrays .asList (
111
+ "CREATE TABLE Singers (\n "
112
+ + " SingerId INT64 NOT NULL,\n "
113
+ + " FirstName STRING(1024),\n "
114
+ + " LastName STRING(1024),\n "
115
+ + " SingerInfo BYTES(MAX)\n "
116
+ + ") PRIMARY KEY (SingerId)" ,
117
+ "CREATE TABLE Albums (\n "
118
+ + " SingerId INT64 NOT NULL,\n "
119
+ + " AlbumId INT64 NOT NULL,\n "
120
+ + " AlbumTitle STRING(MAX)\n "
121
+ + ") PRIMARY KEY (SingerId, AlbumId),\n "
122
+ + " INTERLEAVE IN PARENT Singers ON DELETE CASCADE" ));
135
123
Database db = op .waitFor ().getResult ();
136
124
System .out .println ("Created database [" + db .getId () + "]" );
137
125
}
@@ -413,6 +401,22 @@ static void readOnlyTransaction(DatabaseClient dbClient) {
413
401
}
414
402
// [END read_only_transaction]
415
403
404
+ // [START read_stale_data]
405
+ static void readStaleData (DatabaseClient dbClient ) {
406
+ ResultSet resultSet =
407
+ dbClient
408
+ .singleUse (TimestampBound .ofExactStaleness (10 , TimeUnit .SECONDS ))
409
+ .read ("Albums" ,
410
+ KeySet .all (),
411
+ Arrays .asList ("SingerId" , "AlbumId" , "MarketingBudget" ));
412
+ while (resultSet .next ()) {
413
+ System .out .printf (
414
+ "%d %d %s\n " , resultSet .getLong (0 ), resultSet .getLong (1 ),
415
+ resultSet .isNull (2 ) ? "NULL" : resultSet .getLong ("MarketingBudget" ));
416
+ }
417
+ }
418
+ // [END read_stale_data]
419
+
416
420
static void run (DatabaseClient dbClient , DatabaseAdminClient dbAdminClient , String command ,
417
421
DatabaseId database ) {
418
422
switch (command ) {
@@ -458,6 +462,9 @@ static void run(DatabaseClient dbClient, DatabaseAdminClient dbAdminClient, Stri
458
462
case "readonlytransaction" :
459
463
readOnlyTransaction (dbClient );
460
464
break ;
465
+ case "readstaledata" :
466
+ readStaleData (dbClient );
467
+ break ;
461
468
default :
462
469
printUsageAndExit ();
463
470
}
0 commit comments