2828import java .util .concurrent .ExecutionException ;
2929
3030public class ReadDataWithRoleSample {
31- static void readDataWithRole () throws InterruptedException , ExecutionException {
32- // TODO(developer): Replace these variables before running the sample.
33- String projectId = "my-project" ;
34- String instanceId = "my-instance" ;
35- String databaseId = "my-database" ;
36- String role = "my-role" ;
37- readDataWithRole (projectId , instanceId , databaseId , role );
38- }
31+ static void readDataWithRole () throws InterruptedException , ExecutionException {
32+ // TODO(developer): Replace these variables before running the sample.
33+ String projectId = "my-project" ;
34+ String instanceId = "my-instance" ;
35+ String databaseId = "my-database" ;
36+ String role = "my-role" ;
37+ readDataWithRole (projectId , instanceId , databaseId , role );
38+ }
3939
40- static void readDataWithRole (String projectId , String instanceId , String databaseId , String role )
41- throws InterruptedException , ExecutionException {
42- try (Spanner spanner =
43- SpannerOptions .newBuilder ()
44- .setProjectId (projectId )
45- .setDatabaseRole (role )
46- .build ()
47- .getService ()) {
48- final DatabaseClient dbClient =
49- spanner .getDatabaseClient (DatabaseId .of (projectId , instanceId , databaseId ));
50- try (ResultSet resultSet =
51- dbClient
52- .singleUse ()
53- .read (
54- "Albums" ,
55- KeySet .all (), // Read all rows in a table.
56- Arrays .asList ("SingerId" , "AlbumId" , "AlbumTitle" ))) {
57- while (resultSet .next ()) {
58- System .out .printf (
59- "%d %d %s\n " , resultSet .getLong (0 ), resultSet .getLong (1 ), resultSet .getString (2 ));
60- }
61- }
40+ static void readDataWithRole (String projectId , String instanceId , String databaseId , String role )
41+ throws InterruptedException , ExecutionException {
42+ try (Spanner spanner =
43+ SpannerOptions .newBuilder ()
44+ .setProjectId (projectId )
45+ .setDatabaseRole (role )
46+ .build ()
47+ .getService ()) {
48+ final DatabaseClient dbClient =
49+ spanner .getDatabaseClient (DatabaseId .of (projectId , instanceId , databaseId ));
50+ try (ResultSet resultSet =
51+ dbClient
52+ .singleUse ()
53+ .read (
54+ "Albums" ,
55+ KeySet .all (), // Read all rows in a table.
56+ Arrays .asList ("SingerId" , "AlbumId" , "AlbumTitle" ))) {
57+ while (resultSet .next ()) {
58+ System .out .printf (
59+ "%d %d %s\n " , resultSet .getLong (0 ), resultSet .getLong (1 ), resultSet .getString (2 ));
6260 }
61+ }
6362 }
63+ }
6464}
65- // [END spanner_read_data_with_database_role]
65+ // [END spanner_read_data_with_database_role]
0 commit comments