Search in sources :

Example 11 with Database

use of com.google.cloud.spanner.Database in project google-cloud-java by GoogleCloudPlatform.

the class ITDatabaseAdminTest method databaseOperationsViaEntity.

@Test
public void databaseOperationsViaEntity() throws Exception {
    String dbId = testHelper.getUniqueDatabaseId();
    String instanceId = testHelper.getInstanceId().getInstance();
    String statement1 = "CREATE TABLE T (\n" + "  K STRING(MAX),\n" + ") PRIMARY KEY(K)";
    Operation<Database, CreateDatabaseMetadata> op = dbAdminClient.createDatabase(instanceId, dbId, ImmutableList.of(statement1));
    op = op.waitFor();
    Database db = op.getResult();
    dbs.add(db);
    assertThat(db.getId().getDatabase()).isEqualTo(dbId);
    db = db.reload();
    assertThat(db.getId().getDatabase()).isEqualTo(dbId);
    String statement2 = "CREATE TABLE T2 (\n" + "  K2 STRING(MAX),\n" + ") PRIMARY KEY(K2)";
    Operation<?, ?> op2 = db.updateDdl(ImmutableList.of(statement2), null);
    op2.waitFor();
    Iterable<String> statementsInDb = db.getDdl();
    assertThat(statementsInDb).containsExactly(statement1, statement2);
    db.drop();
    dbs.clear();
    expectedException.expect(isSpannerException(ErrorCode.NOT_FOUND));
    db.reload();
}
Also used : Database(com.google.cloud.spanner.Database) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Example 12 with Database

use of com.google.cloud.spanner.Database in project google-cloud-java by GoogleCloudPlatform.

the class ITDatabaseAdminTest method updateDdlRetry.

@Test
public void updateDdlRetry() throws Exception {
    String dbId = testHelper.getUniqueDatabaseId();
    String instanceId = testHelper.getInstanceId().getInstance();
    String statement1 = "CREATE TABLE T (\n" + "  K STRING(MAX),\n" + ") PRIMARY KEY(K)";
    Operation<Database, CreateDatabaseMetadata> op = dbAdminClient.createDatabase(instanceId, dbId, ImmutableList.of(statement1));
    op = op.waitFor();
    Database db = op.getResult();
    dbs.add(db);
    String statement2 = "CREATE TABLE T2 (\n" + "  K2 STRING(MAX),\n" + ") PRIMARY KEY(K2)";
    Operation<Void, UpdateDatabaseDdlMetadata> op1 = dbAdminClient.updateDatabaseDdl(instanceId, dbId, ImmutableList.of(statement2), "myop");
    Operation<Void, UpdateDatabaseDdlMetadata> op2 = dbAdminClient.updateDatabaseDdl(instanceId, dbId, ImmutableList.of(statement2), "myop");
    op1 = op1.waitFor();
    op2 = op2.waitFor();
    assertThat(op1.getMetadata()).isEqualTo(op2.getMetadata());
}
Also used : UpdateDatabaseDdlMetadata(com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata) Database(com.google.cloud.spanner.Database) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Example 13 with Database

use of com.google.cloud.spanner.Database in project java-docs-samples by GoogleCloudPlatform.

the class SpannerTasks method createDatabase.

private static void createDatabase(PrintWriter pw) {
    Iterable<String> statements = Arrays.asList("CREATE TABLE Singers (\n" + "  SingerId   INT64 NOT NULL,\n" + "  FirstName  STRING(1024),\n" + "  LastName   STRING(1024),\n" + "  SingerInfo BYTES(MAX)\n" + ") PRIMARY KEY (SingerId)", "CREATE TABLE Albums (\n" + "  SingerId     INT64 NOT NULL,\n" + "  AlbumId      INT64 NOT NULL,\n" + "  AlbumTitle   STRING(MAX)\n" + ") PRIMARY KEY (SingerId, AlbumId),\n" + "  INTERLEAVE IN PARENT Singers ON DELETE CASCADE");
    Database db = SpannerClient.getDatabaseAdminClient().createDatabase(SpannerClient.getInstanceId(), SpannerClient.getDatabaseId(), statements).waitFor().getResult();
    pw.println("Created database [" + db.getId() + "]");
}
Also used : Database(com.google.cloud.spanner.Database)

Example 14 with Database

use of com.google.cloud.spanner.Database in project google-cloud-java by GoogleCloudPlatform.

the class DatabaseAdminClientSnippets method createDatabase.

/**
 * Example to create database.
 */
// [TARGET createDatabase(String, String, Iterable)]
// [VARIABLE my_instance_id]
// [VARIABLE my_database_id]
public Database createDatabase(String instanceId, String databaseId) {
    // [START createDatabase]
    OperationFuture<Database, CreateDatabaseMetadata> op = dbAdminClient.createDatabase(instanceId, databaseId, Arrays.asList("CREATE TABLE Singers (\n" + "  SingerId   INT64 NOT NULL,\n" + "  FirstName  STRING(1024),\n" + "  LastName   STRING(1024),\n" + "  SingerInfo BYTES(MAX)\n" + ") PRIMARY KEY (SingerId)", "CREATE TABLE Albums (\n" + "  SingerId     INT64 NOT NULL,\n" + "  AlbumId      INT64 NOT NULL,\n" + "  AlbumTitle   STRING(MAX)\n" + ") PRIMARY KEY (SingerId, AlbumId),\n" + "  INTERLEAVE IN PARENT Singers ON DELETE CASCADE"));
    Database db;
    try {
        db = op.get();
    } catch (ExecutionException e) {
        throw (SpannerException) e.getCause();
    } catch (InterruptedException e) {
        throw SpannerExceptionFactory.propagateInterrupt(e);
    }
    // [END createDatabase]
    return db;
}
Also used : Database(com.google.cloud.spanner.Database) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata) ExecutionException(java.util.concurrent.ExecutionException)

Example 15 with Database

use of com.google.cloud.spanner.Database in project google-cloud-java by GoogleCloudPlatform.

the class DatabaseAdminClientSnippets method listDatabases.

/**
 * Example to get the list of Cloud Spanner database in the given instance.
 */
// [TARGET listDatabases(String, ListOption...)]
// [VARIABLE my_instance_id]
public List<Database> listDatabases(String instanceId) {
    // [START listDatabases]
    Page<Database> page = dbAdminClient.listDatabases(instanceId, Options.pageSize(1));
    List<Database> dbs = new ArrayList<>();
    while (page != null) {
        Database db = Iterables.getOnlyElement(page.getValues());
        dbs.add(db);
        page = page.getNextPage();
    }
    // [END listDatabases]
    return dbs;
}
Also used : Database(com.google.cloud.spanner.Database) ArrayList(java.util.ArrayList)

Aggregations

Database (com.google.cloud.spanner.Database)15 CreateDatabaseMetadata (com.google.spanner.admin.database.v1.CreateDatabaseMetadata)9 IntegrationTest (com.google.cloud.spanner.IntegrationTest)5 Test (org.junit.Test)5 DatabaseClient (com.google.cloud.spanner.DatabaseClient)4 SpannerException (com.google.cloud.spanner.SpannerException)4 DatabaseAdminClient (com.google.cloud.spanner.DatabaseAdminClient)3 ArrayList (java.util.ArrayList)3 Before (org.junit.Before)3 Mutation (com.google.cloud.spanner.Mutation)2 TransactionContext (com.google.cloud.spanner.TransactionContext)2 TransactionRunner (com.google.cloud.spanner.TransactionRunner)2 Nullable (javax.annotation.Nullable)2 ResultSet (com.google.cloud.spanner.ResultSet)1 Statement (com.google.cloud.spanner.Statement)1 UpdateDatabaseDdlMetadata (com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata)1 ExecutionException (java.util.concurrent.ExecutionException)1