use of com.google.spanner.admin.database.v1.Database in project java-spanner by googleapis.
the class CreateDatabaseWithVersionRetentionPeriodSample method createDatabaseWithVersionRetentionPeriod.
static void createDatabaseWithVersionRetentionPeriod(DatabaseAdminClient adminClient, String instanceId, String databaseId, String versionRetentionPeriod) {
OperationFuture<Database, CreateDatabaseMetadata> op = adminClient.createDatabase(instanceId, databaseId, Arrays.asList("CREATE TABLE Singers (" + " SingerId INT64 NOT NULL," + " FirstName STRING(1024)," + " LastName STRING(1024)," + " SingerInfo BYTES(MAX)" + ") PRIMARY KEY (SingerId)", "CREATE TABLE Albums (" + " SingerId INT64 NOT NULL," + " AlbumId INT64 NOT NULL," + " AlbumTitle STRING(MAX)" + ") PRIMARY KEY (SingerId, AlbumId)," + " INTERLEAVE IN PARENT Singers ON DELETE CASCADE", "ALTER DATABASE " + "`" + databaseId + "`" + " SET OPTIONS ( version_retention_period = '" + versionRetentionPeriod + "' )"));
try {
Database database = op.get();
System.out.println("Created database [" + database.getId() + "]");
System.out.println("\tVersion retention period: " + database.getVersionRetentionPeriod());
System.out.println("\tEarliest version time: " + database.getEarliestVersionTime());
} catch (ExecutionException e) {
// If the operation failed during execution, expose the cause.
throw (SpannerException) e.getCause();
} catch (InterruptedException e) {
// and the thread is interrupted, either before or during the activity.
throw SpannerExceptionFactory.propagateInterrupt(e);
}
}
use of com.google.spanner.admin.database.v1.Database in project java-spanner by googleapis.
the class SpannerSample method createDatabase.
// [END spanner_insert_datatypes_data]
// [START spanner_create_database]
static void createDatabase(DatabaseAdminClient dbAdminClient, DatabaseId id) {
OperationFuture<Database, CreateDatabaseMetadata> op = dbAdminClient.createDatabase(id.getInstanceId().getInstance(), id.getDatabase(), Arrays.asList("CREATE TABLE Singers (" + " SingerId INT64 NOT NULL," + " FirstName STRING(1024)," + " LastName STRING(1024)," + " SingerInfo BYTES(MAX)" + ") PRIMARY KEY (SingerId)", "CREATE TABLE Albums (" + " SingerId INT64 NOT NULL," + " AlbumId INT64 NOT NULL," + " AlbumTitle STRING(MAX)" + ") PRIMARY KEY (SingerId, AlbumId)," + " INTERLEAVE IN PARENT Singers ON DELETE CASCADE"));
try {
// Initiate the request which returns an OperationFuture.
Database db = op.get();
System.out.println("Created database [" + db.getId() + "]");
} catch (ExecutionException e) {
// If the operation failed during execution, expose the cause.
throw (SpannerException) e.getCause();
} catch (InterruptedException e) {
// and the thread is interrupted, either before or during the activity.
throw SpannerExceptionFactory.propagateInterrupt(e);
}
}
use of com.google.spanner.admin.database.v1.Database in project java-spanner by googleapis.
the class SpannerSample method listBackupOperations.
// [END spanner_cancel_backup_create]
// [START spanner_list_backup_operations]
static void listBackupOperations(InstanceAdminClient instanceAdminClient, DatabaseId databaseId) {
Instance instance = instanceAdminClient.getInstance(databaseId.getInstanceId().getInstance());
// Get create backup operations for the sample database.
Timestamp last24Hours = Timestamp.ofTimeSecondsAndNanos(TimeUnit.SECONDS.convert(TimeUnit.HOURS.convert(Timestamp.now().getSeconds(), TimeUnit.SECONDS) - 24, TimeUnit.HOURS), 0);
String filter = String.format("(metadata.database:%s) AND " + "(metadata.@type:type.googleapis.com/" + "google.spanner.admin.database.v1.CreateBackupMetadata) AND " + "(metadata.progress.start_time > \"%s\")", databaseId.getName(), last24Hours);
Page<Operation> operations = instance.listBackupOperations(Options.filter(filter));
for (Operation op : operations.iterateAll()) {
try {
CreateBackupMetadata metadata = op.getMetadata().unpack(CreateBackupMetadata.class);
System.out.println(String.format("Backup %s on database %s pending: %d%% complete", metadata.getName(), metadata.getDatabase(), metadata.getProgress().getProgressPercent()));
} catch (InvalidProtocolBufferException e) {
// The returned operation does not contain CreateBackupMetadata.
System.err.println(e.getMessage());
}
}
}
Aggregations