2121
2222import com .google .api .gax .rpc .InvalidArgumentException ;
2323import com .google .cloud .Date ;
24+ import com .google .cloud .bigtable .admin .v2 .BigtableTableAdminClient ;
2425import com .google .cloud .bigtable .admin .v2 .models .CreateSchemaBundleRequest ;
26+ import com .google .cloud .bigtable .admin .v2 .models .CreateTableRequest ;
27+ import com .google .cloud .bigtable .admin .v2 .models .Table ;
2528import com .google .cloud .bigtable .data .v2 .BigtableDataClient ;
2629import com .google .cloud .bigtable .data .v2 .models .RowMutation ;
27- import com .google .cloud .bigtable .data .v2 .models .TableId ;
2830import com .google .cloud .bigtable .data .v2 .models .sql .BoundStatement ;
2931import com .google .cloud .bigtable .data .v2 .models .sql .PreparedStatement ;
3032import com .google .cloud .bigtable .data .v2 .models .sql .ResultSet ;
3537import com .google .cloud .bigtable .data .v2 .test .SingerProto .Singer ;
3638import com .google .cloud .bigtable .test_helpers .env .AbstractTestEnv ;
3739import com .google .cloud .bigtable .test_helpers .env .EmulatorEnv ;
40+ import com .google .cloud .bigtable .test_helpers .env .PrefixGenerator ;
3841import com .google .cloud .bigtable .test_helpers .env .TestEnvRule ;
3942import com .google .protobuf .ByteString ;
4043import com .google .protobuf .DescriptorProtos .FileDescriptorSet ;
4750import java .util .UUID ;
4851import java .util .logging .Level ;
4952import java .util .logging .Logger ;
53+ import org .junit .AfterClass ;
5054import org .junit .BeforeClass ;
5155import org .junit .ClassRule ;
5256import org .junit .Test ;
@@ -58,7 +62,9 @@ public class ExecuteQueryIT {
5862
5963 @ ClassRule public static TestEnvRule testEnvRule = new TestEnvRule ();
6064 private static BigtableDataClient dataClient ;
61- private static TableId tableId ;
65+ private static BigtableTableAdminClient adminClient ;
66+ private static String tableId ;
67+ private static Table table ;
6268 private static String schemaBundleId ;
6369 private static String cf ;
6470 private static String uniquePrefix ;
@@ -77,11 +83,22 @@ public static void setUpAll() throws IOException {
7783 AbstractTestEnv .ConnectionMode .REQUIRE_DIRECT_PATH ,
7884 AbstractTestEnv .ConnectionMode .REQUIRE_DIRECT_PATH_IPV4 );
7985
80- tableId = testEnvRule . env (). getTableId ( );
86+ tableId = PrefixGenerator . newPrefix ( "ExecuteQueryIT" );
8187 dataClient = testEnvRule .env ().getDataClient ();
88+ adminClient = testEnvRule .env ().getTableAdminClient ();
8289 cf = testEnvRule .env ().getFamilyId ();
8390 uniquePrefix = UUID .randomUUID () + "-execute-query-it-" ;
84- schemaBundleId = UUID .randomUUID () + "-bundle" ;
91+ schemaBundleId = PrefixGenerator .newPrefix ("ExecuteQueryIT#bundle" );
92+
93+ table = adminClient .createTable (CreateTableRequest .of (tableId ).addFamily (cf ));
94+ FileDescriptorSet fileDescriptorSet =
95+ FileDescriptorSet .newBuilder ()
96+ .addFile (Singer .getDescriptor ().getFile ().toProto ())
97+ .addFile (Album .getDescriptor ().getFile ().toProto ())
98+ .build ();
99+ adminClient .createSchemaBundle (
100+ CreateSchemaBundleRequest .of (tableId , schemaBundleId )
101+ .setProtoSchema (fileDescriptorSet .toByteString ()));
85102
86103 dataClient .mutateRow (
87104 RowMutation .create (tableId , uniquePrefix + "a" )
@@ -100,11 +117,19 @@ public static void setUpAll() throws IOException {
100117 cf , ByteString .copyFromUtf8 ("qual2" ), 10000 , ByteString .copyFromUtf8 ("bval2" )));
101118 }
102119
120+ @ AfterClass
121+ public static void tearDownAll () {
122+ if (table != null ) {
123+ // Deleting the table will also clean up all the schema bundles under it.
124+ adminClient .deleteTable (tableId );
125+ }
126+ }
127+
103128 @ Test
104129 public void selectStar () {
105130 PreparedStatement preparedStatement =
106131 dataClient .prepareStatement (
107- "SELECT * FROM " + tableId . getTableId () + " WHERE _key LIKE '" + uniquePrefix + "%'" ,
132+ "SELECT * FROM ` " + tableId + "` WHERE _key LIKE '" + uniquePrefix + "%'" ,
108133 new HashMap <>());
109134 BoundStatement statement = preparedStatement .bind ().build ();
110135 try (ResultSet rs = dataClient .executeQuery (statement )) {
@@ -131,7 +156,7 @@ public void withHistoryQuery() {
131156 PreparedStatement preparedStatement =
132157 dataClient .prepareStatement (
133158 "SELECT * FROM `"
134- + tableId . getTableId ()
159+ + tableId
135160 + "`(with_history => true) WHERE _key LIKE '"
136161 + uniquePrefix
137162 + "%'" ,
@@ -166,7 +191,6 @@ public void withHistoryQuery() {
166191 @ SuppressWarnings ("DoubleBraceInitialization" )
167192 @ Test
168193 public void allTypes () throws Exception {
169- createTestSchemaBundle ();
170194 Album album = Album .newBuilder ().setTitle ("Lover" ).build ();
171195
172196 // For some reason the ExecuteQuery data path sometimes cannot resolve a newly-created schema
@@ -191,7 +215,7 @@ public void allTypes() throws Exception {
191215 + " `"
192216 + schemaBundleId
193217 + ".com.google.cloud.bigtable.data.v2.test.Genre`) as enumCol FROM `"
194- + tableId . getTableId ()
218+ + tableId
195219 + "` WHERE _key='"
196220 + uniquePrefix
197221 + "a' LIMIT 1" ,
@@ -256,8 +280,6 @@ public void allTypes() throws Exception {
256280 assertThat (rs .getProtoEnum ("enumCol" , Genre ::forNumber )).isEqualTo (Genre .JAZZ );
257281 assertThat (rs .getProtoEnum (12 , Genre ::forNumber )).isEqualTo (Genre .JAZZ );
258282 assertThat (rs .next ()).isFalse ();
259- } finally {
260- deleteTestSchemaBundle ();
261283 }
262284 }
263285
@@ -400,9 +422,9 @@ public void allQueryParamsTypes() {
400422 public void testNullColumns () {
401423 PreparedStatement preparedStatement =
402424 dataClient .prepareStatement (
403- "SELECT cf['qual'] AS neverNull, cf['qual3'] AS maybeNull FROM "
404- + tableId . getTableId ()
405- + " WHERE _key LIKE '"
425+ "SELECT cf['qual'] AS neverNull, cf['qual3'] AS maybeNull FROM ` "
426+ + tableId
427+ + "` WHERE _key LIKE '"
406428 + uniquePrefix
407429 + "%'" ,
408430 new HashMap <>());
@@ -423,23 +445,4 @@ public void testNullColumns() {
423445 assertThat (rs .next ()).isFalse ();
424446 }
425447 }
426-
427- private static void deleteTestSchemaBundle () {
428- testEnvRule
429- .env ()
430- .getTableAdminClient ()
431- .deleteSchemaBundle (tableId .getTableId (), schemaBundleId );
432- }
433-
434- private static void createTestSchemaBundle () throws Exception {
435- FileDescriptorSet fileDescriptorSet =
436- FileDescriptorSet .newBuilder ()
437- .addFile (Singer .getDescriptor ().getFile ().toProto ())
438- .addFile (Album .getDescriptor ().getFile ().toProto ())
439- .build ();
440- CreateSchemaBundleRequest request =
441- CreateSchemaBundleRequest .of (tableId .getTableId (), schemaBundleId )
442- .setProtoSchema (fileDescriptorSet .toByteString ());
443- testEnvRule .env ().getTableAdminClient ().createSchemaBundle (request );
444- }
445448}
0 commit comments