Search in sources :

Example 51 with Mutation

use of com.google.cloud.spanner.Mutation in project beam by apache.

the class SpannerReadIT method makeTestData.

private void makeTestData() {
    DatabaseClient databaseClient = getDatabaseClient();
    List<Mutation> mutations = new ArrayList<>();
    for (int i = 0; i < 5L; i++) {
        mutations.add(Mutation.newInsertOrUpdateBuilder(options.getTable()).set("key").to((long) i).set("value").to(RandomUtils.randomAlphaNumeric(100)).build());
    }
    databaseClient.writeAtLeastOnce(mutations);
}
Also used : DatabaseClient(com.google.cloud.spanner.DatabaseClient) ArrayList(java.util.ArrayList) Mutation(com.google.cloud.spanner.Mutation)

Example 52 with Mutation

use of com.google.cloud.spanner.Mutation in project beam by apache.

the class MutationSizeEstimatorTest method strings.

@Test
public void strings() throws Exception {
    Mutation emptyString = Mutation.newInsertOrUpdateBuilder("test").set("one").to("").build();
    Mutation nullString = Mutation.newInsertOrUpdateBuilder("test").set("one").to((String) null).build();
    Mutation sampleString = Mutation.newInsertOrUpdateBuilder("test").set("one").to("abc").build();
    Mutation sampleArray = Mutation.newInsertOrUpdateBuilder("test").set("one").toStringArray(Arrays.asList("one", "two", null)).build();
    Mutation nullArray = Mutation.newInsertOrUpdateBuilder("test").set("one").toStringArray(null).build();
    assertThat(MutationSizeEstimator.sizeOf(emptyString), is(0L));
    assertThat(MutationSizeEstimator.sizeOf(nullString), is(0L));
    assertThat(MutationSizeEstimator.sizeOf(sampleString), is(3L));
    assertThat(MutationSizeEstimator.sizeOf(sampleArray), is(6L));
    assertThat(MutationSizeEstimator.sizeOf(nullArray), is(0L));
}
Also used : Mutation(com.google.cloud.spanner.Mutation) Test(org.junit.Test)

Example 53 with Mutation

use of com.google.cloud.spanner.Mutation in project beam by apache.

the class MutationSizeEstimatorTest method primitives.

@Test
public void primitives() throws Exception {
    Mutation int64 = Mutation.newInsertOrUpdateBuilder("test").set("one").to(1).build();
    Mutation float64 = Mutation.newInsertOrUpdateBuilder("test").set("one").to(2.9).build();
    Mutation bool = Mutation.newInsertOrUpdateBuilder("test").set("one").to(false).build();
    Mutation numeric = Mutation.newInsertOrUpdateBuilder("test").set("one").to(new BigDecimal("12345678901234567890.123456789")).build();
    Mutation json = Mutation.newInsertOrUpdateBuilder("test").set("one").to(Value.json("{\"key1\":\"value1\", \"key2\":\"value2\"}")).build();
    assertThat(MutationSizeEstimator.sizeOf(int64), is(8L));
    assertThat(MutationSizeEstimator.sizeOf(float64), is(8L));
    assertThat(MutationSizeEstimator.sizeOf(bool), is(1L));
    assertThat(MutationSizeEstimator.sizeOf(numeric), is(30L));
    assertThat(MutationSizeEstimator.sizeOf(json), is(34L));
}
Also used : Mutation(com.google.cloud.spanner.Mutation) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 54 with Mutation

use of com.google.cloud.spanner.Mutation in project beam by apache.

the class MutationSizeEstimatorTest method dates.

@Test
public void dates() throws Exception {
    Mutation timestamp = Mutation.newInsertOrUpdateBuilder("test").set("one").to(Timestamp.now()).build();
    Mutation nullTimestamp = Mutation.newInsertOrUpdateBuilder("test").set("one").to((Timestamp) null).build();
    Mutation date = Mutation.newInsertOrUpdateBuilder("test").set("one").to(Date.fromYearMonthDay(2017, 10, 10)).build();
    Mutation nullDate = Mutation.newInsertOrUpdateBuilder("test").set("one").to((Date) null).build();
    Mutation timestampArray = Mutation.newInsertOrUpdateBuilder("test").set("one").toTimestampArray(Arrays.asList(Timestamp.now(), null)).build();
    Mutation dateArray = Mutation.newInsertOrUpdateBuilder("test").set("one").toDateArray(Arrays.asList(null, Date.fromYearMonthDay(2017, 1, 1), null, Date.fromYearMonthDay(2017, 1, 2))).build();
    Mutation nullTimestampArray = Mutation.newInsertOrUpdateBuilder("test").set("one").toTimestampArray(null).build();
    Mutation nullDateArray = Mutation.newInsertOrUpdateBuilder("test").set("one").toDateArray(null).build();
    assertThat(MutationSizeEstimator.sizeOf(timestamp), is(12L));
    assertThat(MutationSizeEstimator.sizeOf(date), is(12L));
    assertThat(MutationSizeEstimator.sizeOf(nullTimestamp), is(12L));
    assertThat(MutationSizeEstimator.sizeOf(nullDate), is(12L));
    assertThat(MutationSizeEstimator.sizeOf(timestampArray), is(24L));
    assertThat(MutationSizeEstimator.sizeOf(dateArray), is(48L));
    assertThat(MutationSizeEstimator.sizeOf(nullTimestampArray), is(0L));
    assertThat(MutationSizeEstimator.sizeOf(nullDateArray), is(0L));
}
Also used : Mutation(com.google.cloud.spanner.Mutation) Timestamp(com.google.cloud.Timestamp) Date(com.google.cloud.Date) Test(org.junit.Test)

Example 55 with Mutation

use of com.google.cloud.spanner.Mutation in project beam by apache.

the class MutationSizeEstimatorTest method primitiveArrays.

@Test
public void primitiveArrays() throws Exception {
    Mutation int64 = Mutation.newInsertOrUpdateBuilder("test").set("one").toInt64Array(new long[] { 1L, 2L, 3L }).build();
    Mutation float64 = Mutation.newInsertOrUpdateBuilder("test").set("one").toFloat64Array(new double[] { 1., 2. }).build();
    Mutation bool = Mutation.newInsertOrUpdateBuilder("test").set("one").toBoolArray(new boolean[] { true, true, false, true }).build();
    Mutation numeric = Mutation.newInsertOrUpdateBuilder("test").set("one").toNumericArray(ImmutableList.of(new BigDecimal("12345678901234567890.123456789"), new BigDecimal("12345678901234567890123.1234567890123"), new BigDecimal("123456789012345678901234.1234567890123456"), new BigDecimal("1234567890123456789012345.1234567890123456789"))).build();
    Mutation json = Mutation.newInsertOrUpdateBuilder("test").set("one").toJsonArray(ImmutableList.of("{\"key1\":\"value1\", \"key2\":\"value2\"}", "{\"key1\":\"value1\", \"key2\":20}")).build();
    assertThat(MutationSizeEstimator.sizeOf(int64), is(24L));
    assertThat(MutationSizeEstimator.sizeOf(float64), is(16L));
    assertThat(MutationSizeEstimator.sizeOf(bool), is(4L));
    assertThat(MutationSizeEstimator.sizeOf(numeric), is(153L));
    assertThat(MutationSizeEstimator.sizeOf(json), is(62L));
}
Also used : Mutation(com.google.cloud.spanner.Mutation) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

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