Search in sources :

Example 11 with Mutation

use of com.google.cloud.spanner.Mutation in project spring-cloud-gcp by spring-cloud.

the class SpannerTemplateTests method deleteEntitiesTest.

@Test
public void deleteEntitiesTest() {
    Mutation mutation = Mutation.delete("custom_test_table", Key.of("key"));
    Iterable<TestEntity> entities = new ArrayList<TestEntity>();
    when(this.mutationFactory.delete(eq(TestEntity.class), same(entities))).thenReturn(mutation);
    this.spannerTemplate.delete(TestEntity.class, entities);
    verify(this.databaseClient, times(1)).write(eq(Arrays.asList(mutation)));
}
Also used : ArrayList(java.util.ArrayList) Mutation(com.google.cloud.spanner.Mutation) Test(org.junit.Test)

Example 12 with Mutation

use of com.google.cloud.spanner.Mutation in project spring-cloud-gcp by spring-cloud.

the class SpannerTemplateTests method upsertTest.

@Test
public void upsertTest() {
    Mutation mutation = Mutation.newInsertOrUpdateBuilder("custom_test_table").build();
    TestEntity entity = new TestEntity();
    when(this.mutationFactory.upsert(same(entity), isNull())).thenReturn(mutation);
    this.spannerTemplate.upsert(entity);
    verify(this.databaseClient, times(1)).write(eq(Arrays.asList(mutation)));
}
Also used : Mutation(com.google.cloud.spanner.Mutation) Test(org.junit.Test)

Example 13 with Mutation

use of com.google.cloud.spanner.Mutation in project spring-cloud-gcp by spring-cloud.

the class SpannerTemplateTests method upsertColumnsArrayTest.

@Test
public void upsertColumnsArrayTest() {
    Mutation mutation = Mutation.newInsertOrUpdateBuilder("custom_test_table").build();
    TestEntity entity = new TestEntity();
    when(this.mutationFactory.upsert(same(entity), eq(Optional.of(new HashSet<>(Arrays.asList(new String[] { "a", "b" })))))).thenReturn(mutation);
    this.spannerTemplate.upsert(entity, "a", "b");
    verify(this.databaseClient, times(1)).write(eq(Arrays.asList(mutation)));
}
Also used : Mutation(com.google.cloud.spanner.Mutation) Test(org.junit.Test)

Example 14 with Mutation

use of com.google.cloud.spanner.Mutation in project spring-cloud-gcp by spring-cloud.

the class SpannerTemplateTests method deleteObjectTest.

@Test
public void deleteObjectTest() {
    Mutation mutation = Mutation.delete("custom_test_table", Key.of("key"));
    TestEntity entity = new TestEntity();
    when(this.mutationFactory.delete(entity)).thenReturn(mutation);
    this.spannerTemplate.delete(entity);
    verify(this.databaseClient, times(1)).write(eq(Arrays.asList(mutation)));
}
Also used : Mutation(com.google.cloud.spanner.Mutation) Test(org.junit.Test)

Example 15 with Mutation

use of com.google.cloud.spanner.Mutation 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)

Aggregations

Mutation (com.google.cloud.spanner.Mutation)70 Test (org.junit.Test)53 ArrayList (java.util.ArrayList)11 CommitResponse (com.google.cloud.spanner.CommitResponse)4 KeySet (com.google.cloud.spanner.KeySet)4 ReadQueryUpdateTransactionOption (com.google.cloud.spanner.Options.ReadQueryUpdateTransactionOption)4 Timestamp (com.google.cloud.Timestamp)3 DatabaseClient (com.google.cloud.spanner.DatabaseClient)3 ResultSet (com.google.cloud.spanner.ResultSet)3 TransactionContext (com.google.cloud.spanner.TransactionContext)3 BatchableMutationFilterFn (org.apache.beam.sdk.io.gcp.spanner.SpannerIO.BatchableMutationFilterFn)3 Sleeper (org.apache.beam.sdk.util.Sleeper)3 Database (com.google.cloud.spanner.Database)2 DatabaseAdminClient (com.google.cloud.spanner.DatabaseAdminClient)2 Key (com.google.cloud.spanner.Key)2 WriteBuilder (com.google.cloud.spanner.Mutation.WriteBuilder)2 SpannerException (com.google.cloud.spanner.SpannerException)2 TransactionRunner (com.google.cloud.spanner.TransactionRunner)2 CreateDatabaseMetadata (com.google.spanner.admin.database.v1.CreateDatabaseMetadata)2 BigDecimal (java.math.BigDecimal)2