Search in sources :

Example 1 with DatabaseAdminClient

use of com.google.cloud.spanner.DatabaseAdminClient 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";
    spannerOptions = SpannerOptions.getDefaultInstance();
    spanner = spannerOptions.getService();
    DatabaseAdminClient adminClient = spanner.getDatabaseAdminClient();
    try {
        adminClient.dropDatabase(instanceId, databaseId);
    } catch (SpannerException e) {
    // Does not exist, ignore.
    }
    Operation<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.waitFor();
    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 2 with DatabaseAdminClient

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

the class SpannerReadIT method setUp.

@Before
public void setUp() {
    instanceId = System.getProperty("spanner.test.instance");
    databaseId = "df-spanner-read-it";
    spannerOptions = SpannerOptions.getDefaultInstance();
    spanner = spannerOptions.getService();
    DatabaseAdminClient adminClient = spanner.getDatabaseAdminClient();
    try {
        adminClient.dropDatabase(instanceId, databaseId);
    } catch (SpannerException e) {
    // Does not exist, ignore.
    }
    Operation<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.waitFor();
    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)

Example 3 with DatabaseAdminClient

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

the class SpannerGroupWriteIT method tearDown.

@After
public void tearDown() {
    DatabaseAdminClient adminClient = spanner.getDatabaseAdminClient();
    try {
        adminClient.dropDatabase(instanceId, databaseId);
    } catch (SpannerException e) {
    // Failed to cleanup.
    }
    spanner.close();
}
Also used : DatabaseAdminClient(com.google.cloud.spanner.DatabaseAdminClient) SpannerException(com.google.cloud.spanner.SpannerException) After(org.junit.After)

Example 4 with DatabaseAdminClient

use of com.google.cloud.spanner.DatabaseAdminClient 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-groupwrite-it";
    spannerOptions = SpannerOptions.getDefaultInstance();
    spanner = spannerOptions.getService();
    DatabaseAdminClient adminClient = spanner.getDatabaseAdminClient();
    try {
        adminClient.dropDatabase(instanceId, databaseId);
    } catch (SpannerException e) {
    // Does not exist, ignore.
    }
    Operation<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.waitFor();
    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 5 with DatabaseAdminClient

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

the class SpannerSample method main.

public static void main(String[] args) throws Exception {
    if (args.length != 3) {
        printUsageAndExit();
    }
    // [START init_client]
    SpannerOptions options = SpannerOptions.newBuilder().build();
    Spanner spanner = options.getService();
    try {
        String command = args[0];
        DatabaseId db = DatabaseId.of(options.getProjectId(), args[1], args[2]);
        // [END init_client]
        // This will return the default project id based on the environment.
        String clientProject = spanner.getOptions().getProjectId();
        if (!db.getInstanceId().getProject().equals(clientProject)) {
            System.err.println("Invalid project specified. Project in the database id should match" + "the project name set in the environment variable GCLOUD_PROJECT. Expected: " + clientProject);
            printUsageAndExit();
        }
        // [START init_client]
        DatabaseClient dbClient = spanner.getDatabaseClient(db);
        DatabaseAdminClient dbAdminClient = spanner.getDatabaseAdminClient();
        // [END init_client]
        run(dbClient, dbAdminClient, command, db);
    } finally {
        spanner.close();
    }
    System.out.println("Closed client");
}
Also used : DatabaseClient(com.google.cloud.spanner.DatabaseClient) DatabaseAdminClient(com.google.cloud.spanner.DatabaseAdminClient) DatabaseId(com.google.cloud.spanner.DatabaseId) SpannerOptions(com.google.cloud.spanner.SpannerOptions) Spanner(com.google.cloud.spanner.Spanner)

Aggregations

DatabaseAdminClient (com.google.cloud.spanner.DatabaseAdminClient)11 SpannerException (com.google.cloud.spanner.SpannerException)6 DatabaseClient (com.google.cloud.spanner.DatabaseClient)4 Database (com.google.cloud.spanner.Database)3 Spanner (com.google.cloud.spanner.Spanner)3 SpannerOptions (com.google.cloud.spanner.SpannerOptions)3 CreateDatabaseMetadata (com.google.spanner.admin.database.v1.CreateDatabaseMetadata)3 After (org.junit.After)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 RetrySettings (com.google.api.gax.retrying.RetrySettings)1 ServerStreamingCallSettings (com.google.api.gax.rpc.ServerStreamingCallSettings)1 UnaryCallSettings (com.google.api.gax.rpc.UnaryCallSettings)1 BatchClient (com.google.cloud.spanner.BatchClient)1 DatabaseId (com.google.cloud.spanner.DatabaseId)1 UpdateDatabaseDdlMetadata (com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata)1 CommitRequest (com.google.spanner.v1.CommitRequest)1