Search in sources :

Example 96 with Database

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);
    }
}
Also used : Database(com.google.cloud.spanner.Database) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata) ExecutionException(java.util.concurrent.ExecutionException)

Example 97 with Database

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);
    }
}
Also used : Database(com.google.cloud.spanner.Database) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata) ExecutionException(java.util.concurrent.ExecutionException)

Example 98 with Database

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());
        }
    }
}
Also used : Instance(com.google.cloud.spanner.Instance) CreateBackupMetadata(com.google.spanner.admin.database.v1.CreateBackupMetadata) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) Operation(com.google.longrunning.Operation) Timestamp(com.google.cloud.Timestamp)

Aggregations

Test (org.junit.Test)53 CreateDatabaseMetadata (com.google.spanner.admin.database.v1.CreateDatabaseMetadata)31 Database (com.google.cloud.spanner.Database)29 AbstractMessage (com.google.protobuf.AbstractMessage)24 ArrayList (java.util.ArrayList)24 ExecutionException (java.util.concurrent.ExecutionException)22 Database (com.google.spanner.admin.database.v1.Database)21 Operation (com.google.longrunning.Operation)18 ByteString (com.google.protobuf.ByteString)16 DatabaseAdminClient (com.google.cloud.spanner.DatabaseAdminClient)12 SpannerException (com.google.cloud.spanner.SpannerException)12 Database (org.molgenis.emx2.Database)12 CreateBackupMetadata (com.google.spanner.admin.database.v1.CreateBackupMetadata)11 DatabaseName (com.google.spanner.admin.database.v1.DatabaseName)10 RestoreDatabaseMetadata (com.google.spanner.admin.database.v1.RestoreDatabaseMetadata)9 Timestamp (com.google.cloud.Timestamp)8 Spanner (com.google.cloud.spanner.Spanner)8 RestoreDatabaseRequest (com.google.spanner.admin.database.v1.RestoreDatabaseRequest)7 StatusRuntimeException (io.grpc.StatusRuntimeException)7 Backup (com.google.cloud.spanner.Backup)6