Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: replace client creation methods for all new samples with auto-generated admin clients #2927

Merged
merged 24 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
edc5bbf
fix: prevent illegal negative timeout values into thread sleep() meth…
arpan14 Feb 6, 2023
49a85df
Merge pull request #1 from arpan14/retryerror
arpan14 Feb 8, 2023
4cd497b
Fixing lint issues.
arpan14 Feb 8, 2023
4a6aa8e
Merge branch 'googleapis:main' into main
arpan14 Mar 13, 2023
b2aa09d
Merge branch 'googleapis:main' into main
arpan14 Mar 15, 2023
8d6d71e
Merge branch 'googleapis:main' into main
arpan14 May 9, 2023
77e6e7d
Merge branch 'googleapis:main' into main
arpan14 Jul 17, 2023
e8b7fad
Merge branch 'googleapis:main' into main
arpan14 Jul 25, 2023
8aa84e1
Merge branch 'googleapis:main' into main
arpan14 Oct 10, 2023
57fd405
Merge branch 'googleapis:main' into main
arpan14 Oct 27, 2023
1253563
Merge branch 'googleapis:main' into main
arpan14 Nov 20, 2023
d4f6a60
Merge branch 'googleapis:main' into main
arpan14 Dec 15, 2023
3efaf7c
Merge branch 'googleapis:main' into main
arpan14 Dec 26, 2023
f41b39f
Merge branch 'googleapis:main' into main
arpan14 Jan 3, 2024
7e3287f
Merge branch 'googleapis:main' into main
arpan14 Jan 13, 2024
7edd24d
Merge branch 'googleapis:main' into main
arpan14 Feb 13, 2024
fe3649b
Merge branch 'googleapis:main' into main
arpan14 Feb 20, 2024
ae0dbc9
Merge branch 'googleapis:main' into main
arpan14 Feb 23, 2024
99cd112
Merge branch 'googleapis:main' into main
arpan14 Feb 23, 2024
fd4fa00
chore: remove unused imports.
arpan14 Feb 23, 2024
65d7943
chore: replace with new client creation methods.
arpan14 Feb 23, 2024
7743b14
Update samples/snippets/src/main/java/com/example/spanner/admin/gener…
arpan14 Feb 23, 2024
48f100e
chore: address comments.
arpan14 Feb 24, 2024
fdd7217
chore: replace with new client creation methods.
arpan14 Feb 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@

// [START spanner_add_and_drop_database_role]

import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.SpannerOptions;
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient;
import com.google.common.collect.ImmutableList;
import com.google.spanner.admin.database.v1.DatabaseName;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class AddAndDropDatabaseRole {

static void addAndDropDatabaseRole() throws IOException {
static void addAndDropDatabaseRole() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project";
String instanceId = "my-instance";
Expand All @@ -39,10 +40,14 @@ static void addAndDropDatabaseRole() throws IOException {
}

static void addAndDropDatabaseRole(
String projectId, String instanceId, String databaseId, String parentRole, String childRole)
throws IOException {
final DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create();
try {
String projectId, String instanceId, String databaseId,
String parentRole, String childRole) {
try (Spanner spanner =
arpan14 marked this conversation as resolved.
Show resolved Hide resolved
SpannerOptions.newBuilder()
.setProjectId(projectId)
.build()
.getService();
DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) {
System.out.println("Waiting for role create operation to complete...");
databaseAdminClient.updateDatabaseDdlAsync(
DatabaseName.of(projectId, instanceId, databaseId),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@
package com.example.spanner.admin.generated;

// [START spanner_add_json_column]

import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.SpannerOptions;
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient;
import com.google.common.collect.ImmutableList;
import com.google.spanner.admin.database.v1.DatabaseName;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

class AddJsonColumnSample {

static void addJsonColumn() throws InterruptedException, ExecutionException, IOException {
static void addJsonColumn() throws InterruptedException, ExecutionException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project";
String instanceId = "my-instance";
Expand All @@ -35,15 +37,20 @@ static void addJsonColumn() throws InterruptedException, ExecutionException, IOE
}

static void addJsonColumn(String projectId, String instanceId, String databaseId)
throws InterruptedException, ExecutionException, IOException {
final DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create();

// Wait for the operation to finish.
// This will throw an ExecutionException if the operation fails.
databaseAdminClient.updateDatabaseDdlAsync(
DatabaseName.of(projectId, instanceId, databaseId),
ImmutableList.of("ALTER TABLE Venues ADD COLUMN VenueDetails JSON")).get();
System.out.printf("Successfully added column `VenueDetails`%n");
throws InterruptedException, ExecutionException {
try (Spanner spanner =
SpannerOptions.newBuilder()
.setProjectId(projectId)
.build()
.getService();
DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) {
// Wait for the operation to finish.
// This will throw an ExecutionException if the operation fails.
databaseAdminClient.updateDatabaseDdlAsync(
DatabaseName.of(projectId, instanceId, databaseId),
ImmutableList.of("ALTER TABLE Venues ADD COLUMN VenueDetails JSON")).get();
System.out.printf("Successfully added column `VenueDetails`%n");
}
}
}
// [END spanner_add_json_column]
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@
package com.example.spanner.admin.generated;

// [START spanner_postgresql_jsonb_add_column]

import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.SpannerOptions;
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient;
import com.google.common.collect.ImmutableList;
import com.google.spanner.admin.database.v1.DatabaseName;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

class AddJsonbColumnSample {

static void addJsonbColumn() throws InterruptedException, ExecutionException, IOException {
static void addJsonbColumn() throws InterruptedException, ExecutionException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project";
String instanceId = "my-instance";
Expand All @@ -35,16 +37,21 @@ static void addJsonbColumn() throws InterruptedException, ExecutionException, IO
}

static void addJsonbColumn(String projectId, String instanceId, String databaseId)
throws InterruptedException, ExecutionException, IOException {
final DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create();

// JSONB datatype is only supported with PostgreSQL-dialect databases.
// Wait for the operation to finish.
// This will throw an ExecutionException if the operation fails.
databaseAdminClient.updateDatabaseDdlAsync(
DatabaseName.of(projectId, instanceId, databaseId),
ImmutableList.of("ALTER TABLE Venues ADD COLUMN VenueDetails JSONB")).get();
System.out.printf("Successfully added column `VenueDetails`%n");
throws InterruptedException, ExecutionException {
try (Spanner spanner =
SpannerOptions.newBuilder()
.setProjectId(projectId)
.build()
.getService();
DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) {
// JSONB datatype is only supported with PostgreSQL-dialect databases.
// Wait for the operation to finish.
// This will throw an ExecutionException if the operation fails.
databaseAdminClient.updateDatabaseDdlAsync(
DatabaseName.of(projectId, instanceId, databaseId),
ImmutableList.of("ALTER TABLE Venues ADD COLUMN VenueDetails JSONB")).get();
System.out.printf("Successfully added column `VenueDetails`%n");
}
}
}
// [END spanner_postgresql_jsonb_add_column]
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@

// [START spanner_add_numeric_column]

import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.SpannerOptions;
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient;
import com.google.common.collect.ImmutableList;
import com.google.spanner.admin.database.v1.DatabaseName;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

class AddNumericColumnSample {

static void addNumericColumn() throws InterruptedException, ExecutionException, IOException {
static void addNumericColumn() throws InterruptedException, ExecutionException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project";
String instanceId = "my-instance";
Expand All @@ -36,15 +37,20 @@ static void addNumericColumn() throws InterruptedException, ExecutionException,
}

static void addNumericColumn(String projectId, String instanceId, String databaseId)
throws InterruptedException, ExecutionException, IOException {
final DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create();

// Wait for the operation to finish.
// This will throw an ExecutionException if the operation fails.
databaseAdminClient.updateDatabaseDdlAsync(
DatabaseName.of(projectId, instanceId, databaseId),
ImmutableList.of("ALTER TABLE Venues ADD COLUMN Revenue NUMERIC")).get();
System.out.printf("Successfully added column `Revenue`%n");
throws InterruptedException, ExecutionException {
try (Spanner spanner =
SpannerOptions.newBuilder()
.setProjectId(projectId)
.build()
.getService();
DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) {
// Wait for the operation to finish.
// This will throw an ExecutionException if the operation fails.
databaseAdminClient.updateDatabaseDdlAsync(
DatabaseName.of(projectId, instanceId, databaseId),
ImmutableList.of("ALTER TABLE Venues ADD COLUMN Revenue NUMERIC")).get();
System.out.printf("Successfully added column `Revenue`%n");
}
}
}
// [END spanner_add_numeric_column]
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,25 @@
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient;
import com.google.common.collect.ImmutableList;
import com.google.spanner.admin.database.v1.DatabaseName;
import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class AlterSequenceSample {

static void alterSequence() throws IOException {
static void alterSequence() {
// TODO(developer): Replace these variables before running the sample.
final String projectId = "my-project";
final String instanceId = "my-instance";
final String databaseId = "my-database";
alterSequence(projectId, instanceId, databaseId);
}

static void alterSequence(String projectId, String instanceId, String databaseId)
throws IOException {
DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create();
static void alterSequence(String projectId, String instanceId, String databaseId) {
try (Spanner spanner =
SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) {
SpannerOptions.newBuilder().setProjectId(projectId).build().getService();
DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) {

databaseAdminClient
.updateDatabaseDdlAsync(DatabaseName.of(projectId, instanceId, databaseId),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@

// [START spanner_alter_table_with_foreign_key_delete_cascade]

import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.SpannerOptions;
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient;
import com.google.common.collect.ImmutableList;
import com.google.spanner.admin.database.v1.DatabaseName;
import java.io.IOException;

class AlterTableWithForeignKeyDeleteCascadeSample {

static void alterForeignKeyDeleteCascadeConstraint() throws IOException {
static void alterForeignKeyDeleteCascadeConstraint() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project";
String instanceId = "my-instance";
Expand All @@ -35,22 +36,24 @@ static void alterForeignKeyDeleteCascadeConstraint() throws IOException {
}

static void alterForeignKeyDeleteCascadeConstraint(
String projectId, String instanceId, String databaseId) throws IOException {
DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create();

databaseAdminClient.updateDatabaseDdlAsync(DatabaseName.of(projectId, instanceId,
databaseId),
ImmutableList.of(
"ALTER TABLE ShoppingCarts\n"
+ " ADD CONSTRAINT FKShoppingCartsCustomerName\n"
+ " FOREIGN KEY (CustomerName)\n"
+ " REFERENCES Customers(CustomerName)\n"
+ " ON DELETE CASCADE\n"));
System.out.printf(
String.format(
"Altered ShoppingCarts table with FKShoppingCartsCustomerName\n"
+ "foreign key constraint on database %s on instance %s",
databaseId, instanceId));
String projectId, String instanceId, String databaseId) {
try (Spanner spanner =
SpannerOptions.newBuilder().setProjectId(projectId).build().getService();
DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) {
databaseAdminClient.updateDatabaseDdlAsync(DatabaseName.of(projectId, instanceId,
databaseId),
ImmutableList.of(
"ALTER TABLE ShoppingCarts\n"
+ " ADD CONSTRAINT FKShoppingCartsCustomerName\n"
+ " FOREIGN KEY (CustomerName)\n"
+ " REFERENCES Customers(CustomerName)\n"
+ " ON DELETE CASCADE\n"));
System.out.printf(
String.format(
"Altered ShoppingCarts table with FKShoppingCartsCustomerName\n"
+ "foreign key constraint on database %s on instance %s",
databaseId, instanceId));
}
}
}
// [END spanner_alter_table_with_foreign_key_delete_cascade]
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
// [START spanner_copy_backup]

import com.google.cloud.Timestamp;
import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.SpannerException;
import com.google.cloud.spanner.SpannerExceptionFactory;
import com.google.cloud.spanner.SpannerOptions;
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient;
import com.google.spanner.admin.database.v1.Backup;
import com.google.spanner.admin.database.v1.BackupName;
import com.google.spanner.admin.database.v1.InstanceName;
import java.io.IOException;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneId;
Expand All @@ -34,14 +35,16 @@

public class CopyBackupSample {

static void copyBackup() throws IOException {
static void copyBackup() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project";
String instanceId = "my-instance";
String sourceBackupId = "my-backup";
String destinationBackupId = "my-destination-backup";

try (DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create()) {
try (Spanner spanner =
SpannerOptions.newBuilder().setProjectId(projectId).build().getService();
DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) {
copyBackup(databaseAdminClient, projectId, instanceId, sourceBackupId, destinationBackupId);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,18 @@

// [START spanner_create_backup_with_encryption_key]

import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.SpannerExceptionFactory;
import com.google.cloud.spanner.SpannerOptions;
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient;
import com.google.cloud.spanner.encryption.EncryptionConfigs;
import com.google.protobuf.Timestamp;
import com.google.spanner.admin.database.v1.Backup;
import com.google.spanner.admin.database.v1.BackupName;
import com.google.spanner.admin.database.v1.CreateBackupEncryptionConfig;
import com.google.spanner.admin.database.v1.CreateBackupEncryptionConfig.EncryptionType;
import com.google.spanner.admin.database.v1.CreateBackupMetadata;
import com.google.spanner.admin.database.v1.CreateBackupRequest;
import com.google.spanner.admin.database.v1.DatabaseName;
import com.google.spanner.admin.database.v1.InstanceName;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
Expand All @@ -39,7 +38,7 @@

public class CreateBackupWithEncryptionKey {

static void createBackupWithEncryptionKey() throws IOException {
static void createBackupWithEncryptionKey() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project";
String instanceId = "my-instance";
Expand All @@ -48,7 +47,9 @@ static void createBackupWithEncryptionKey() throws IOException {
String kmsKeyName =
"projects/" + projectId + "/locations/<location>/keyRings/<keyRing>/cryptoKeys/<keyId>";

try (DatabaseAdminClient adminClient = DatabaseAdminClient.create()) {
try (Spanner spanner =
SpannerOptions.newBuilder().setProjectId(projectId).build().getService();
DatabaseAdminClient adminClient = spanner.createDatabaseAdminClient()) {
createBackupWithEncryptionKey(
adminClient,
projectId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,32 @@

// [START spanner_create_database_with_encryption_key]

import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.SpannerExceptionFactory;
import com.google.cloud.spanner.SpannerOptions;
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient;
import com.google.common.collect.ImmutableList;
import com.google.spanner.admin.database.v1.CreateDatabaseRequest;
import com.google.spanner.admin.database.v1.Database;
import com.google.spanner.admin.database.v1.EncryptionConfig;
import com.google.spanner.admin.database.v1.InstanceName;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CreateDatabaseWithEncryptionKey {

static void createDatabaseWithEncryptionKey() throws IOException {
static void createDatabaseWithEncryptionKey() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project";
String instanceId = "my-instance";
String databaseId = "my-database";
String kmsKeyName =
"projects/" + projectId + "/locations/<location>/keyRings/<keyRing>/cryptoKeys/<keyId>";

try (DatabaseAdminClient adminClient = DatabaseAdminClient.create()) {
try (Spanner spanner =
SpannerOptions.newBuilder().setProjectId(projectId).build().getService();
DatabaseAdminClient adminClient = spanner.createDatabaseAdminClient()) {
createDatabaseWithEncryptionKey(
adminClient,
projectId,
Expand Down
Loading
Loading