1818
1919import static org .junit .Assert .assertEquals ;
2020
21+ import com .google .api .gax .longrunning .OperationFuture ;
2122import com .google .cloud .spanner .Database ;
2223import com .google .cloud .spanner .DatabaseAdminClient ;
2324import com .google .cloud .spanner .DatabaseClient ;
2425import com .google .cloud .spanner .DatabaseId ;
2526import com .google .cloud .spanner .Mutation ;
26- import com .google .cloud .spanner .Operation ;
2727import com .google .cloud .spanner .Spanner ;
2828import com .google .cloud .spanner .SpannerException ;
2929import com .google .cloud .spanner .SpannerOptions ;
3434import java .nio .file .Path ;
3535import java .util .Arrays ;
3636import java .util .List ;
37+ import java .util .concurrent .ExecutionException ;
3738import java .util .stream .Collectors ;
3839import javax .annotation .Nullable ;
3940import org .junit .After ;
@@ -50,7 +51,7 @@ public class SpannerReadIT {
5051 private SpannerOptions spannerOptions ;
5152
5253 @ Before
53- public void setUp () {
54+ public void setUp () throws InterruptedException , ExecutionException {
5455 instanceId = System .getProperty ("spanner.test.instance" );
5556 databaseId = "df-spanner-read-it" ;
5657
@@ -65,61 +66,82 @@ public void setUp() {
6566 // Does not exist, ignore.
6667 }
6768
68- Operation <Database , CreateDatabaseMetadata > op = adminClient
69- .createDatabase (instanceId , databaseId , Arrays .asList ("CREATE TABLE Singers "
70- + "(singerId INT64 NOT NULL, firstName STRING(MAX) NOT NULL, "
71- + "lastName STRING(MAX) NOT NULL,) PRIMARY KEY (singerId)" ,
72- "CREATE TABLE Albums (singerId INT64 NOT NULL, albumId INT64 NOT NULL, "
73- + "albumTitle STRING(MAX) NOT NULL,) PRIMARY KEY (singerId, albumId)" ));
74-
75- op .waitFor ();
76-
77- List <Mutation > mutations = Arrays .asList (
78- Mutation .newInsertBuilder ("singers" )
79- .set ("singerId" ).to (1L )
80- .set ("firstName" ).to ("John" )
81- .set ("lastName" ).to ("Lennon" )
82- .build (),
83- Mutation .newInsertBuilder ("singers" )
84- .set ("singerId" ).to (2L )
85- .set ("firstName" ).to ("Paul" )
86- .set ("lastName" ).to ("Mccartney" )
87- .build (),
88- Mutation .newInsertBuilder ("singers" )
89- .set ("singerId" ).to (3L )
90- .set ("firstName" ).to ("George" )
91- .set ("lastName" ).to ("Harrison" )
92- .build (),
93- Mutation .newInsertBuilder ("singers" )
94- .set ("singerId" ).to (4L )
95- .set ("firstName" ).to ("Ringo" )
96- .set ("lastName" ).to ("Starr" )
97- .build (),
98-
99- Mutation .newInsertBuilder ("albums" )
100- .set ("singerId" ).to (1L )
101- .set ("albumId" ).to (1L )
102- .set ("albumTitle" ).to ("Imagine" )
103- .build (),
104- Mutation .newInsertBuilder ("albums" )
105- .set ("singerId" ).to (2L )
106- .set ("albumId" ).to (1L )
107- .set ("albumTitle" ).to ("Pipes of Peace" )
108- .build ()
109- );
110-
69+ OperationFuture <Database , CreateDatabaseMetadata > op =
70+ adminClient .createDatabase (
71+ instanceId ,
72+ databaseId ,
73+ Arrays .asList (
74+ "CREATE TABLE Singers "
75+ + "(singerId INT64 NOT NULL, firstName STRING(MAX) NOT NULL, "
76+ + "lastName STRING(MAX) NOT NULL,) PRIMARY KEY (singerId)" ,
77+ "CREATE TABLE Albums (singerId INT64 NOT NULL, albumId INT64 NOT NULL, "
78+ + "albumTitle STRING(MAX) NOT NULL,) PRIMARY KEY (singerId, albumId)" ));
79+
80+ op .get ();
81+
82+ List <Mutation > mutations =
83+ Arrays .asList (
84+ Mutation .newInsertBuilder ("singers" )
85+ .set ("singerId" )
86+ .to (1L )
87+ .set ("firstName" )
88+ .to ("John" )
89+ .set ("lastName" )
90+ .to ("Lennon" )
91+ .build (),
92+ Mutation .newInsertBuilder ("singers" )
93+ .set ("singerId" )
94+ .to (2L )
95+ .set ("firstName" )
96+ .to ("Paul" )
97+ .set ("lastName" )
98+ .to ("Mccartney" )
99+ .build (),
100+ Mutation .newInsertBuilder ("singers" )
101+ .set ("singerId" )
102+ .to (3L )
103+ .set ("firstName" )
104+ .to ("George" )
105+ .set ("lastName" )
106+ .to ("Harrison" )
107+ .build (),
108+ Mutation .newInsertBuilder ("singers" )
109+ .set ("singerId" )
110+ .to (4L )
111+ .set ("firstName" )
112+ .to ("Ringo" )
113+ .set ("lastName" )
114+ .to ("Starr" )
115+ .build (),
116+ Mutation .newInsertBuilder ("albums" )
117+ .set ("singerId" )
118+ .to (1L )
119+ .set ("albumId" )
120+ .to (1L )
121+ .set ("albumTitle" )
122+ .to ("Imagine" )
123+ .build (),
124+ Mutation .newInsertBuilder ("albums" )
125+ .set ("singerId" )
126+ .to (2L )
127+ .set ("albumId" )
128+ .to (1L )
129+ .set ("albumTitle" )
130+ .to ("Pipes of Peace" )
131+ .build ());
111132
112133 DatabaseClient dbClient = getDbClient ();
113134
114135 TransactionRunner runner = dbClient .readWriteTransaction ();
115- runner .run (new TransactionRunner .TransactionCallable <Void >() {
116- @ Nullable
117- @ Override
118- public Void run (TransactionContext tx ) {
119- tx .buffer (mutations );
120- return null ;
121- }
122- });
136+ runner .run (
137+ new TransactionRunner .TransactionCallable <Void >() {
138+ @ Nullable
139+ @ Override
140+ public Void run (TransactionContext tx ) {
141+ tx .buffer (mutations );
142+ return null ;
143+ }
144+ });
123145 }
124146
125147 @ After
@@ -137,8 +159,13 @@ public void tearDown() {
137159 @ Test
138160 public void readDbEndToEnd () throws Exception {
139161 Path outPath = Files .createTempFile ("out" , "txt" );
140- SpannerReadAll .main (new String [] { "--instanceId=" + instanceId , "--databaseId=" + databaseId ,
141- "--output=" + outPath , "--runner=DirectRunner" });
162+ SpannerReadAll .main (
163+ new String [] {
164+ "--instanceId=" + instanceId ,
165+ "--databaseId=" + databaseId ,
166+ "--output=" + outPath ,
167+ "--runner=DirectRunner"
168+ });
142169
143170 String content = Files .readAllLines (outPath ).stream ().collect (Collectors .joining ("\n " ));
144171
@@ -148,8 +175,14 @@ public void readDbEndToEnd() throws Exception {
148175 @ Test
149176 public void readTableEndToEnd () throws Exception {
150177 Path outPath = Files .createTempFile ("out" , "txt" );
151- SpannerRead .main (new String [] { "--instanceId=" + instanceId , "--databaseId=" + databaseId ,
152- "--output=" + outPath , "--table=albums" , "--runner=DirectRunner" });
178+ SpannerRead .main (
179+ new String [] {
180+ "--instanceId=" + instanceId ,
181+ "--databaseId=" + databaseId ,
182+ "--output=" + outPath ,
183+ "--table=albums" ,
184+ "--runner=DirectRunner"
185+ });
153186
154187 String content = Files .readAllLines (outPath ).stream ().collect (Collectors .joining ("\n " ));
155188
@@ -159,8 +192,13 @@ public void readTableEndToEnd() throws Exception {
159192 @ Test
160193 public void readApiEndToEnd () throws Exception {
161194 Path outPath = Files .createTempFile ("out" , "txt" );
162- SpannerReadApi .main (new String [] { "--instanceId=" + instanceId , "--databaseId=" + databaseId ,
163- "--output=" + outPath , "--runner=DirectRunner" });
195+ SpannerReadApi .main (
196+ new String [] {
197+ "--instanceId=" + instanceId ,
198+ "--databaseId=" + databaseId ,
199+ "--output=" + outPath ,
200+ "--runner=DirectRunner"
201+ });
164202
165203 String content = Files .readAllLines (outPath ).stream ().collect (Collectors .joining ("\n " ));
166204
@@ -172,17 +210,20 @@ public void reaTransactionalReadEndToEnd() throws Exception {
172210 Path singersPath = Files .createTempFile ("singers" , "txt" );
173211 Path albumsPath = Files .createTempFile ("albums" , "txt" );
174212 TransactionalRead .main (
175- new String [] { "--instanceId=" + instanceId , "--databaseId=" + databaseId ,
176- "--singersFilename=" + singersPath , "--albumsFilename=" + albumsPath ,
177- "--runner=DirectRunner" });
213+ new String [] {
214+ "--instanceId=" + instanceId ,
215+ "--databaseId=" + databaseId ,
216+ "--singersFilename=" + singersPath ,
217+ "--albumsFilename=" + albumsPath ,
218+ "--runner=DirectRunner"
219+ });
178220
179221 assertEquals (4 , Files .readAllLines (singersPath ).size ());
180222 assertEquals (2 , Files .readAllLines (albumsPath ).size ());
181223 }
182224
183225 private DatabaseClient getDbClient () {
184- return spanner
185- . getDatabaseClient ( DatabaseId .of (spannerOptions .getProjectId (), instanceId , databaseId ));
226+ return spanner . getDatabaseClient (
227+ DatabaseId .of (spannerOptions .getProjectId (), instanceId , databaseId ));
186228 }
187-
188- }
229+ }
0 commit comments