Search in sources :

Example 61 with Mutation

use of com.google.spanner.v1.Mutation in project java-bigtable-hbase by googleapis.

the class TestPutAdapter method testMultipleCellsInOneFamilyAreConverted.

@Test
public void testMultipleCellsInOneFamilyAreConverted() {
    byte[] row = dataHelper.randomData("rk-");
    byte[] family = dataHelper.randomData("f1");
    byte[] qualifier1 = dataHelper.randomData("qual1");
    byte[] qualifier2 = dataHelper.randomData("qual2");
    byte[] value1 = dataHelper.randomData("v1");
    byte[] value2 = dataHelper.randomData("v2");
    long timestamp1 = 1L;
    long timestamp2 = 2L;
    Put hbasePut = new Put(row);
    hbasePut.addColumn(family, qualifier1, timestamp1, value1);
    hbasePut.addColumn(family, qualifier2, timestamp2, value2);
    MutateRowRequest request = adapt(hbasePut);
    Assert.assertArrayEquals(row, request.getRowKey().toByteArray());
    Assert.assertEquals(2, request.getMutationsCount());
    Mutation mutation = request.getMutations(0);
    Assert.assertEquals(MutationCase.SET_CELL, mutation.getMutationCase());
    SetCell setCell = mutation.getSetCell();
    Assert.assertArrayEquals(family, setCell.getFamilyNameBytes().toByteArray());
    Assert.assertArrayEquals(qualifier1, setCell.getColumnQualifier().toByteArray());
    Assert.assertEquals(TimeUnit.MILLISECONDS.toMicros(timestamp1), setCell.getTimestampMicros());
    Assert.assertArrayEquals(value1, setCell.getValue().toByteArray());
    Mutation mod2 = request.getMutations(1);
    SetCell setCell2 = mod2.getSetCell();
    Assert.assertArrayEquals(family, setCell2.getFamilyNameBytes().toByteArray());
    Assert.assertArrayEquals(qualifier2, setCell2.getColumnQualifier().toByteArray());
    Assert.assertEquals(TimeUnit.MILLISECONDS.toMicros(timestamp2), setCell2.getTimestampMicros());
    Assert.assertArrayEquals(value2, setCell2.getValue().toByteArray());
}
Also used : MutateRowRequest(com.google.bigtable.v2.MutateRowRequest) SetCell(com.google.bigtable.v2.Mutation.SetCell) Mutation(com.google.bigtable.v2.Mutation) RowMutation(com.google.cloud.bigtable.data.v2.models.RowMutation) Put(org.apache.hadoop.hbase.client.Put) Test(org.junit.Test)

Example 62 with Mutation

use of com.google.spanner.v1.Mutation in project java-bigtable-hbase by googleapis.

the class TestPutAdapter method testUnsetTimestampsAreNotPopulated.

@Test
public void testUnsetTimestampsAreNotPopulated() {
    PutAdapter adapter = new PutAdapter(-1, false);
    byte[] row = dataHelper.randomData("rk-");
    byte[] family1 = dataHelper.randomData("f1");
    byte[] qualifier1 = dataHelper.randomData("qual1");
    byte[] value1 = dataHelper.randomData("v1");
    Put hbasePut = new Put(row).addColumn(family1, qualifier1, value1);
    com.google.cloud.bigtable.data.v2.models.Mutation unsafeMutation = com.google.cloud.bigtable.data.v2.models.Mutation.createUnsafe();
    adapter.adapt(hbasePut, unsafeMutation);
    RowMutation rowMutation = RowMutation.create(TABLE_ID, ByteString.copyFrom(hbasePut.getRow()), unsafeMutation);
    MutateRowRequest request = rowMutation.toProto(REQUEST_CONTEXT);
    Assert.assertArrayEquals(row, request.getRowKey().toByteArray());
    Assert.assertEquals(1, request.getMutationsCount());
    Mutation mutation = request.getMutations(0);
    Assert.assertEquals(MutationCase.SET_CELL, mutation.getMutationCase());
    SetCell setCell = mutation.getSetCell();
    Assert.assertArrayEquals(family1, setCell.getFamilyNameBytes().toByteArray());
    Assert.assertArrayEquals(qualifier1, setCell.getColumnQualifier().toByteArray());
    Assert.assertEquals(-1, setCell.getTimestampMicros());
    Assert.assertArrayEquals(value1, setCell.getValue().toByteArray());
}
Also used : MutateRowRequest(com.google.bigtable.v2.MutateRowRequest) SetCell(com.google.bigtable.v2.Mutation.SetCell) RowMutation(com.google.cloud.bigtable.data.v2.models.RowMutation) Mutation(com.google.bigtable.v2.Mutation) RowMutation(com.google.cloud.bigtable.data.v2.models.RowMutation) Put(org.apache.hadoop.hbase.client.Put) Test(org.junit.Test)

Example 63 with Mutation

use of com.google.spanner.v1.Mutation in project java-bigtable-hbase by googleapis.

the class TestCheckAndMutateUtil method testDelete.

@Test
public /**
 * Tests that a CheckAndMutate with a {@link Delete} works correctly for the filter and mutation
 * aspects
 */
void testDelete() throws DoNotRetryIOException {
    CheckAndMutateUtil.RequestBuilder underTest = createRequestBuilder();
    CheckAndMutateRowRequest result = underTest.qualifier(qual).ifMatches(CompareOp.EQUAL, checkValue).withDelete(new Delete(rowKey).addColumns(family, qual)).build().toProto(REQUEST_CONTEXT);
    Assert.assertEquals(1, result.getTrueMutationsCount());
    Mutation.DeleteFromColumn delete = result.getTrueMutations(0).getDeleteFromColumn();
    Assert.assertArrayEquals(family, delete.getFamilyNameBytes().toByteArray());
    Assert.assertArrayEquals(qual, delete.getColumnQualifier().toByteArray());
    checkPredicate(result);
}
Also used : Delete(org.apache.hadoop.hbase.client.Delete) Mutation(com.google.bigtable.v2.Mutation) CheckAndMutateRowRequest(com.google.bigtable.v2.CheckAndMutateRowRequest) Test(org.junit.Test)

Example 64 with Mutation

use of com.google.spanner.v1.Mutation in project pgadapter by GoogleCloudPlatform.

the class MutationWriterTest method testWritePartials.

@Test
public void testWritePartials() throws Exception {
    Map<String, TypeCode> tableColumns = ImmutableMap.of("number", TypeCode.INT64, "name", TypeCode.STRING);
    CSVFormat format = CSVFormat.POSTGRESQL_TEXT.builder().setHeader(tableColumns.keySet().toArray(new String[0])).build();
    Connection connection = mock(Connection.class);
    DatabaseClient databaseClient = mock(DatabaseClient.class);
    MutationWriter mutationWriter = new MutationWriter(CopyTransactionMode.ImplicitNonAtomic, connection, databaseClient, "numbers", tableColumns, /* indexedColumnsCount = */
    1, format, false);
    mutationWriter.addCopyData("1\t\"One\"\n2\t\"Two\"\n3\t\"Three\"\n4\t\"Four\"\n5\t\"Five\"\n".getBytes(StandardCharsets.UTF_8));
    mutationWriter.close();
    ExecutorService executor = Executors.newFixedThreadPool(2);
    executor.submit(() -> {
        mutationWriter.addCopyData("1\t\"One\"\n".getBytes(StandardCharsets.UTF_8));
        mutationWriter.addCopyData("2\t\"Two".getBytes(StandardCharsets.UTF_8));
        mutationWriter.addCopyData("\"".getBytes(StandardCharsets.UTF_8));
        mutationWriter.addCopyData("\n3\t".getBytes(StandardCharsets.UTF_8));
        mutationWriter.addCopyData("\"Three\"\n4\t\"Four\"\n5\t".getBytes(StandardCharsets.UTF_8));
        mutationWriter.addCopyData("\"Five\"\n".getBytes(StandardCharsets.UTF_8));
        mutationWriter.close();
        return null;
    });
    Future<Long> updateCount = executor.submit(mutationWriter);
    assertEquals(5L, updateCount.get().longValue());
    List<Mutation> expectedMutations = ImmutableList.of(Mutation.newInsertBuilder("numbers").set("number").to(1L).set("name").to("One").build(), Mutation.newInsertBuilder("numbers").set("number").to(2L).set("name").to("Two").build(), Mutation.newInsertBuilder("numbers").set("number").to(3L).set("name").to("Three").build(), Mutation.newInsertBuilder("numbers").set("number").to(4L).set("name").to("Four").build(), Mutation.newInsertBuilder("numbers").set("number").to(5L).set("name").to("Five").build());
    verify(databaseClient).write(expectedMutations);
    executor.shutdown();
}
Also used : DatabaseClient(com.google.cloud.spanner.DatabaseClient) TypeCode(com.google.spanner.v1.TypeCode) Connection(com.google.cloud.spanner.connection.Connection) ExecutorService(java.util.concurrent.ExecutorService) CSVFormat(org.apache.commons.csv.CSVFormat) Mutation(com.google.cloud.spanner.Mutation) Test(org.junit.Test)

Example 65 with Mutation

use of com.google.spanner.v1.Mutation in project pgadapter by GoogleCloudPlatform.

the class MutationWriterTest method testWriteMutations.

@Test
public void testWriteMutations() throws Exception {
    Map<String, TypeCode> tableColumns = ImmutableMap.of("number", TypeCode.INT64, "name", TypeCode.STRING);
    CSVFormat format = CSVFormat.POSTGRESQL_TEXT.builder().setHeader(tableColumns.keySet().toArray(new String[0])).build();
    Connection connection = mock(Connection.class);
    DatabaseClient databaseClient = mock(DatabaseClient.class);
    MutationWriter mutationWriter = new MutationWriter(CopyTransactionMode.ImplicitAtomic, connection, databaseClient, "numbers", tableColumns, /* indexedColumnsCount = */
    1, format, false);
    mutationWriter.addCopyData("1\t\"One\"\n2\t\"Two\"\n".getBytes(StandardCharsets.UTF_8));
    mutationWriter.close();
    long updateCount = mutationWriter.call();
    assertEquals(2L, updateCount);
    List<Mutation> expectedMutations = ImmutableList.of(Mutation.newInsertBuilder("numbers").set("number").to(1L).set("name").to("One").build(), Mutation.newInsertBuilder("numbers").set("number").to(2L).set("name").to("Two").build());
    verify(databaseClient).write(expectedMutations);
}
Also used : DatabaseClient(com.google.cloud.spanner.DatabaseClient) TypeCode(com.google.spanner.v1.TypeCode) Connection(com.google.cloud.spanner.connection.Connection) CSVFormat(org.apache.commons.csv.CSVFormat) Mutation(com.google.cloud.spanner.Mutation) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)70 Mutation (com.google.bigtable.v2.Mutation)62 ArrayList (java.util.ArrayList)59 ByteString (com.google.protobuf.ByteString)56 CommitRequest (com.google.spanner.v1.CommitRequest)15 KV (org.apache.beam.sdk.values.KV)15 CheckAndMutateRowResponse (com.google.bigtable.v2.CheckAndMutateRowResponse)14 AbstractMessage (com.google.protobuf.AbstractMessage)13 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)12 RowFilter (com.google.bigtable.v2.RowFilter)12 TableName (com.google.bigtable.v2.TableName)12 StatusRuntimeException (io.grpc.StatusRuntimeException)12 CheckAndMutateRowRequest (com.google.bigtable.v2.CheckAndMutateRowRequest)11 MutateRowRequest (com.google.bigtable.v2.MutateRowRequest)11 Mutation (com.google.spanner.v1.Mutation)11 BaseBigtableDataClient (com.google.cloud.bigtable.data.v2.BaseBigtableDataClient)10 MutateRowResponse (com.google.bigtable.v2.MutateRowResponse)9 SetCell (com.google.bigtable.v2.Mutation.SetCell)9 Mutation (com.google.datastore.v1.Mutation)9 CommitResponse (com.google.spanner.v1.CommitResponse)9