17
17
package com .example .spanner .admin .generated ;
18
18
19
19
// [START spanner_postgresql_jsonb_add_column]
20
+
21
+ import com .google .cloud .spanner .Spanner ;
22
+ import com .google .cloud .spanner .SpannerOptions ;
20
23
import com .google .cloud .spanner .admin .database .v1 .DatabaseAdminClient ;
21
24
import com .google .common .collect .ImmutableList ;
22
25
import com .google .spanner .admin .database .v1 .DatabaseName ;
23
- import java .io .IOException ;
24
26
import java .util .concurrent .ExecutionException ;
25
27
26
28
class AddJsonbColumnSample {
27
29
28
- static void addJsonbColumn () throws InterruptedException , ExecutionException , IOException {
30
+ static void addJsonbColumn () throws InterruptedException , ExecutionException {
29
31
// TODO(developer): Replace these variables before running the sample.
30
32
String projectId = "my-project" ;
31
33
String instanceId = "my-instance" ;
@@ -35,16 +37,21 @@ static void addJsonbColumn() throws InterruptedException, ExecutionException, IO
35
37
}
36
38
37
39
static void addJsonbColumn (String projectId , String instanceId , String databaseId )
38
- throws InterruptedException , ExecutionException , IOException {
39
- final DatabaseAdminClient databaseAdminClient = DatabaseAdminClient .create ();
40
-
41
- // JSONB datatype is only supported with PostgreSQL-dialect databases.
42
- // Wait for the operation to finish.
43
- // This will throw an ExecutionException if the operation fails.
44
- databaseAdminClient .updateDatabaseDdlAsync (
45
- DatabaseName .of (projectId , instanceId , databaseId ),
46
- ImmutableList .of ("ALTER TABLE Venues ADD COLUMN VenueDetails JSONB" )).get ();
47
- System .out .printf ("Successfully added column `VenueDetails`%n" );
40
+ throws InterruptedException , ExecutionException {
41
+ try (Spanner spanner =
42
+ SpannerOptions .newBuilder ()
43
+ .setProjectId (projectId )
44
+ .build ()
45
+ .getService ();
46
+ DatabaseAdminClient databaseAdminClient = spanner .createDatabaseAdminClient ()) {
47
+ // JSONB datatype is only supported with PostgreSQL-dialect databases.
48
+ // Wait for the operation to finish.
49
+ // This will throw an ExecutionException if the operation fails.
50
+ databaseAdminClient .updateDatabaseDdlAsync (
51
+ DatabaseName .of (projectId , instanceId , databaseId ),
52
+ ImmutableList .of ("ALTER TABLE Venues ADD COLUMN VenueDetails JSONB" )).get ();
53
+ System .out .printf ("Successfully added column `VenueDetails`%n" );
54
+ }
48
55
}
49
56
}
50
57
// [END spanner_postgresql_jsonb_add_column]
0 commit comments