Search in sources :

Example 1 with Database

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

the class ITDatabaseAdminTest method databaseOperations.

@Test
public void databaseOperations() 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 = dbAdminClient.getDatabase(instanceId, dbId);
    assertThat(db.getId().getDatabase()).isEqualTo(dbId);
    boolean foundDb = false;
    for (Database dbInList : Iterators.toArray(dbAdminClient.listDatabases(instanceId).iterateAll().iterator(), Database.class)) {
        if (dbInList.getId().getDatabase().equals(dbId)) {
            foundDb = true;
            break;
        }
    }
    assertThat(foundDb).isTrue();
    String statement2 = "CREATE TABLE T2 (\n" + "  K2 STRING(MAX),\n" + ") PRIMARY KEY(K2)";
    Operation<?, ?> op2 = dbAdminClient.updateDatabaseDdl(instanceId, dbId, ImmutableList.of(statement2), null);
    op2.waitFor();
    List<String> statementsInDb = dbAdminClient.getDatabaseDdl(instanceId, dbId);
    assertThat(statementsInDb).containsExactly(statement1, statement2);
    dbAdminClient.dropDatabase(instanceId, dbId);
    dbs.clear();
    expectedException.expect(isSpannerException(ErrorCode.NOT_FOUND));
    db = dbAdminClient.getDatabase(testHelper.getInstanceId().getInstance(), dbId);
}
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 2 with Database

use of com.google.spanner.admin.database.v1.Database in project java-docs-samples by GoogleCloudPlatform.

the class SpannerSample method createDatabase.

// [END spanner_insert_data_with_timestamp_column]
// [START spanner_create_database]
static void createDatabase(DatabaseAdminClient dbAdminClient, DatabaseId id) {
    Operation<Database, CreateDatabaseMetadata> op = dbAdminClient.createDatabase(id.getInstanceId().getInstance(), id.getDatabase(), 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 = op.waitFor().getResult();
    System.out.println("Created database [" + db.getId() + "]");
}
Also used : Database(com.google.cloud.spanner.Database) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata)

Example 3 with Database

use of com.google.spanner.admin.database.v1.Database in project java-docs-samples by GoogleCloudPlatform.

the class SpannerGroupWriteIT method setUp.

@Before
public void setUp() throws Exception {
    instanceId = System.getProperty("spanner.test.instance");
    databaseId = "df-spanner-gwrite-it-" + random.nextInt(1000000000);
    spannerOptions = SpannerOptions.getDefaultInstance();
    spanner = spannerOptions.getService();
    DatabaseAdminClient adminClient = spanner.getDatabaseAdminClient();
    try {
        adminClient.dropDatabase(instanceId, databaseId);
    } catch (SpannerException e) {
    // Does not exist, ignore.
    }
    OperationFuture<Database, CreateDatabaseMetadata> op = adminClient.createDatabase(instanceId, databaseId, Arrays.asList("CREATE TABLE users (" + "id STRING(MAX) NOT NULL, state STRING(MAX) NOT NULL) PRIMARY KEY (id)", "CREATE TABLE PendingReviews (id INT64, action STRING(MAX), " + "note STRING(MAX), userId STRING(MAX),) PRIMARY KEY (id)"));
    op.get();
    DatabaseClient dbClient = getDbClient();
    List<Mutation> mutations = new ArrayList<>();
    for (int i = 0; i < 20; i++) {
        mutations.add(Mutation.newInsertBuilder("users").set("id").to(Integer.toString(i)).set("state").to("ACTIVE").build());
    }
    TransactionRunner runner = dbClient.readWriteTransaction();
    runner.run(new TransactionRunner.TransactionCallable<Void>() {

        @Nullable
        @Override
        public Void run(TransactionContext tx) {
            tx.buffer(mutations);
            return null;
        }
    });
    String content = IntStream.range(0, 10).mapToObj(Integer::toString).collect(Collectors.joining("\n"));
    tempPath = Files.createTempFile("suspicious-ids", "txt");
    Files.write(tempPath, content.getBytes());
}
Also used : ArrayList(java.util.ArrayList) DatabaseClient(com.google.cloud.spanner.DatabaseClient) TransactionRunner(com.google.cloud.spanner.TransactionRunner) TransactionContext(com.google.cloud.spanner.TransactionContext) Database(com.google.cloud.spanner.Database) DatabaseAdminClient(com.google.cloud.spanner.DatabaseAdminClient) SpannerException(com.google.cloud.spanner.SpannerException) Mutation(com.google.cloud.spanner.Mutation) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata) Nullable(javax.annotation.Nullable) Before(org.junit.Before)

Example 4 with Database

use of com.google.spanner.admin.database.v1.Database in project java-docs-samples by GoogleCloudPlatform.

the class SpannerWriteIT method setUp.

@Before
public void setUp() throws Exception {
    instanceId = System.getProperty("spanner.test.instance");
    databaseId = "df-spanner-write-it-" + random.nextInt(1000000000);
    spannerOptions = SpannerOptions.getDefaultInstance();
    spanner = spannerOptions.getService();
    DatabaseAdminClient adminClient = spanner.getDatabaseAdminClient();
    try {
        adminClient.dropDatabase(instanceId, databaseId);
    } catch (SpannerException e) {
    // Does not exist, ignore.
    }
    OperationFuture<Database, CreateDatabaseMetadata> op = adminClient.createDatabase(instanceId, databaseId, Arrays.asList("CREATE TABLE Singers " + "(singerId INT64 NOT NULL, " + "firstName STRING(MAX) NOT NULL, lastName STRING(MAX) NOT NULL,) " + "PRIMARY KEY (singerId)", "CREATE TABLE Albums (singerId INT64 NOT NULL, " + "albumId INT64 NOT NULL, albumTitle STRING(MAX) NOT NULL,) " + "PRIMARY KEY (singerId, albumId)"));
    op.get();
    String singers = Stream.of("1\tJohn\tLennon", "2\tPaul\tMccartney", "3\tGeorge\tHarrison", "4\tRingo\tStarr").collect(Collectors.joining("\n"));
    singersPath = Files.createTempFile("singers", "txt");
    Files.write(singersPath, singers.getBytes());
    String albums = Stream.of("1\t1\tImagine", "2\t1\tPipes of Peace", "3\t1\tDark Horse").collect(Collectors.joining("\n"));
    albumsPath = Files.createTempFile("albums", "txt");
    Files.write(albumsPath, albums.getBytes());
}
Also used : Database(com.google.cloud.spanner.Database) DatabaseAdminClient(com.google.cloud.spanner.DatabaseAdminClient) SpannerException(com.google.cloud.spanner.SpannerException) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata) Before(org.junit.Before)

Example 5 with Database

use of com.google.spanner.admin.database.v1.Database in project java-docs-samples by GoogleCloudPlatform.

the class SpannerReadIT method setUp.

@Before
public void setUp() throws InterruptedException, ExecutionException {
    instanceId = System.getProperty("spanner.test.instance");
    databaseId = "df-spanner-read-it-" + random.nextInt(1000000000);
    spannerOptions = SpannerOptions.getDefaultInstance();
    spanner = spannerOptions.getService();
    DatabaseAdminClient adminClient = spanner.getDatabaseAdminClient();
    try {
        adminClient.dropDatabase(instanceId, databaseId);
    } catch (SpannerException e) {
    // Does not exist, ignore.
    }
    OperationFuture<Database, CreateDatabaseMetadata> op = adminClient.createDatabase(instanceId, databaseId, Arrays.asList("CREATE TABLE Singers " + "(singerId INT64 NOT NULL, firstName STRING(MAX) NOT NULL, " + "lastName STRING(MAX) NOT NULL,) PRIMARY KEY (singerId)", "CREATE TABLE Albums (singerId INT64 NOT NULL, albumId INT64 NOT NULL, " + "albumTitle STRING(MAX) NOT NULL,) PRIMARY KEY (singerId, albumId)"));
    op.get();
    List<Mutation> mutations = Arrays.asList(Mutation.newInsertBuilder("singers").set("singerId").to(1L).set("firstName").to("John").set("lastName").to("Lennon").build(), Mutation.newInsertBuilder("singers").set("singerId").to(2L).set("firstName").to("Paul").set("lastName").to("Mccartney").build(), Mutation.newInsertBuilder("singers").set("singerId").to(3L).set("firstName").to("George").set("lastName").to("Harrison").build(), Mutation.newInsertBuilder("singers").set("singerId").to(4L).set("firstName").to("Ringo").set("lastName").to("Starr").build(), Mutation.newInsertBuilder("albums").set("singerId").to(1L).set("albumId").to(1L).set("albumTitle").to("Imagine").build(), Mutation.newInsertBuilder("albums").set("singerId").to(2L).set("albumId").to(1L).set("albumTitle").to("Pipes of Peace").build());
    DatabaseClient dbClient = getDbClient();
    TransactionRunner runner = dbClient.readWriteTransaction();
    runner.run(new TransactionRunner.TransactionCallable<Void>() {

        @Nullable
        @Override
        public Void run(TransactionContext tx) {
            tx.buffer(mutations);
            return null;
        }
    });
}
Also used : DatabaseClient(com.google.cloud.spanner.DatabaseClient) TransactionRunner(com.google.cloud.spanner.TransactionRunner) TransactionContext(com.google.cloud.spanner.TransactionContext) Database(com.google.cloud.spanner.Database) DatabaseAdminClient(com.google.cloud.spanner.DatabaseAdminClient) SpannerException(com.google.cloud.spanner.SpannerException) Mutation(com.google.cloud.spanner.Mutation) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata) Nullable(javax.annotation.Nullable) Before(org.junit.Before)

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