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 ;
2527import com .google .cloud .bigtable .data .v2 .BigtableDataClient ;
2628import com .google .cloud .bigtable .data .v2 .models .RowMutation ;
27- import com .google .cloud .bigtable .data .v2 .models .TableId ;
2829import com .google .cloud .bigtable .data .v2 .models .sql .BoundStatement ;
2930import com .google .cloud .bigtable .data .v2 .models .sql .PreparedStatement ;
3031import com .google .cloud .bigtable .data .v2 .models .sql .ResultSet ;
3536import com .google .cloud .bigtable .data .v2 .test .SingerProto .Singer ;
3637import com .google .cloud .bigtable .test_helpers .env .AbstractTestEnv ;
3738import com .google .cloud .bigtable .test_helpers .env .EmulatorEnv ;
39+ import com .google .cloud .bigtable .test_helpers .env .PrefixGenerator ;
3840import com .google .cloud .bigtable .test_helpers .env .TestEnvRule ;
3941import com .google .protobuf .ByteString ;
4042import com .google .protobuf .DescriptorProtos .FileDescriptorSet ;
4749import java .util .UUID ;
4850import java .util .logging .Level ;
4951import java .util .logging .Logger ;
52+ import org .junit .AfterClass ;
5053import org .junit .BeforeClass ;
5154import org .junit .ClassRule ;
5255import org .junit .Test ;
@@ -58,7 +61,8 @@ public class ExecuteQueryIT {
5861
5962 @ ClassRule public static TestEnvRule testEnvRule = new TestEnvRule ();
6063 private static BigtableDataClient dataClient ;
61- private static TableId tableId ;
64+ private static BigtableTableAdminClient adminClient ;
65+ private static String tableId ;
6266 private static String schemaBundleId ;
6367 private static String cf ;
6468 private static String uniquePrefix ;
@@ -77,11 +81,22 @@ public static void setUpAll() throws IOException {
7781 AbstractTestEnv .ConnectionMode .REQUIRE_DIRECT_PATH ,
7882 AbstractTestEnv .ConnectionMode .REQUIRE_DIRECT_PATH_IPV4 );
7983
80- tableId = testEnvRule . env (). getTableId ( );
84+ tableId = PrefixGenerator . newPrefix ( "ExecuteQueryIT" );
8185 dataClient = testEnvRule .env ().getDataClient ();
86+ adminClient = testEnvRule .env ().getTableAdminClient ();
8287 cf = testEnvRule .env ().getFamilyId ();
8388 uniquePrefix = UUID .randomUUID () + "-execute-query-it-" ;
84- schemaBundleId = UUID .randomUUID () + "-bundle" ;
89+ schemaBundleId = PrefixGenerator .newPrefix ("ExecuteQueryIT#bundle" );
90+
91+ adminClient .createTable (CreateTableRequest .of (tableId ).addFamily (cf ));
92+ FileDescriptorSet fileDescriptorSet =
93+ FileDescriptorSet .newBuilder ()
94+ .addFile (Singer .getDescriptor ().getFile ().toProto ())
95+ .addFile (Album .getDescriptor ().getFile ().toProto ())
96+ .build ();
97+ adminClient .createSchemaBundle (
98+ CreateSchemaBundleRequest .of (tableId , schemaBundleId )
99+ .setProtoSchema (fileDescriptorSet .toByteString ()));
85100
86101 dataClient .mutateRow (
87102 RowMutation .create (tableId , uniquePrefix + "a" )
@@ -100,11 +115,17 @@ public static void setUpAll() throws IOException {
100115 cf , ByteString .copyFromUtf8 ("qual2" ), 10000 , ByteString .copyFromUtf8 ("bval2" )));
101116 }
102117
118+ @ AfterClass
119+ public static void tearDownAll () {
120+ adminClient .deleteSchemaBundle (tableId , schemaBundleId );
121+ adminClient .deleteTable (tableId );
122+ }
123+
103124 @ Test
104125 public void selectStar () {
105126 PreparedStatement preparedStatement =
106127 dataClient .prepareStatement (
107- "SELECT * FROM " + tableId . getTableId () + " WHERE _key LIKE '" + uniquePrefix + "%'" ,
128+ "SELECT * FROM ` " + tableId + "` WHERE _key LIKE '" + uniquePrefix + "%'" ,
108129 new HashMap <>());
109130 BoundStatement statement = preparedStatement .bind ().build ();
110131 try (ResultSet rs = dataClient .executeQuery (statement )) {
@@ -131,7 +152,7 @@ public void withHistoryQuery() {
131152 PreparedStatement preparedStatement =
132153 dataClient .prepareStatement (
133154 "SELECT * FROM `"
134- + tableId . getTableId ()
155+ + tableId
135156 + "`(with_history => true) WHERE _key LIKE '"
136157 + uniquePrefix
137158 + "%'" ,
@@ -166,7 +187,6 @@ public void withHistoryQuery() {
166187 @ SuppressWarnings ("DoubleBraceInitialization" )
167188 @ Test
168189 public void allTypes () throws Exception {
169- createTestSchemaBundle ();
170190 Album album = Album .newBuilder ().setTitle ("Lover" ).build ();
171191
172192 // For some reason the ExecuteQuery data path sometimes cannot resolve a newly-created schema
@@ -191,7 +211,7 @@ public void allTypes() throws Exception {
191211 + " `"
192212 + schemaBundleId
193213 + ".com.google.cloud.bigtable.data.v2.test.Genre`) as enumCol FROM `"
194- + tableId . getTableId ()
214+ + tableId
195215 + "` WHERE _key='"
196216 + uniquePrefix
197217 + "a' LIMIT 1" ,
@@ -256,8 +276,6 @@ public void allTypes() throws Exception {
256276 assertThat (rs .getProtoEnum ("enumCol" , Genre ::forNumber )).isEqualTo (Genre .JAZZ );
257277 assertThat (rs .getProtoEnum (12 , Genre ::forNumber )).isEqualTo (Genre .JAZZ );
258278 assertThat (rs .next ()).isFalse ();
259- } finally {
260- deleteTestSchemaBundle ();
261279 }
262280 }
263281
@@ -400,9 +418,9 @@ public void allQueryParamsTypes() {
400418 public void testNullColumns () {
401419 PreparedStatement preparedStatement =
402420 dataClient .prepareStatement (
403- "SELECT cf['qual'] AS neverNull, cf['qual3'] AS maybeNull FROM "
404- + tableId . getTableId ()
405- + " WHERE _key LIKE '"
421+ "SELECT cf['qual'] AS neverNull, cf['qual3'] AS maybeNull FROM ` "
422+ + tableId
423+ + "` WHERE _key LIKE '"
406424 + uniquePrefix
407425 + "%'" ,
408426 new HashMap <>());
@@ -423,23 +441,4 @@ public void testNullColumns() {
423441 assertThat (rs .next ()).isFalse ();
424442 }
425443 }
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- }
445444}
0 commit comments