Search in sources :

Example 1 with Mutation

use of com.google.cloud.bigtable.data.v2.models.Mutation in project java-docs-samples by GoogleCloudPlatform.

the class CassandraMigrationCodelab method delete.

public void delete() {
    try {
        String rowKey = "phone#4c410523#20190501";
        RowMutation mutation = RowMutation.create(tableId, rowKey).deleteRow();
        dataClient.mutateRow(mutation);
    } catch (Exception e) {
        System.out.println("Error during Delete: \n" + e.toString());
    }
}
Also used : RowMutation(com.google.cloud.bigtable.data.v2.models.RowMutation) ByteString(com.google.protobuf.ByteString)

Example 2 with Mutation

use of com.google.cloud.bigtable.data.v2.models.Mutation in project java-bigtable-hbase by googleapis.

the class TestPutAdapter method testSingleCellIsConverted.

@Test
public void testSingleCellIsConverted() {
    byte[] row = dataHelper.randomData("rk-");
    byte[] family = dataHelper.randomData("f");
    byte[] qualifier = dataHelper.randomData("qual");
    byte[] value = dataHelper.randomData("v1");
    long timestamp = 2L;
    Put hbasePut = new Put(row);
    hbasePut.addColumn(family, qualifier, timestamp, value);
    MutateRowRequest request = adapt(hbasePut);
    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(family, setCell.getFamilyNameBytes().toByteArray());
    Assert.assertArrayEquals(qualifier, setCell.getColumnQualifier().toByteArray());
    Assert.assertEquals(TimeUnit.MILLISECONDS.toMicros(timestamp), setCell.getTimestampMicros());
    Assert.assertArrayEquals(value, setCell.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 3 with Mutation

use of com.google.cloud.bigtable.data.v2.models.Mutation in project java-bigtable-hbase by googleapis.

the class TestPutAdapter method testUnsetTimestampsArePopulated.

@Test
public void testUnsetTimestampsArePopulated() {
    byte[] row = dataHelper.randomData("rk-");
    byte[] family1 = dataHelper.randomData("f1");
    byte[] qualifier1 = dataHelper.randomData("qual1");
    byte[] value1 = dataHelper.randomData("v1");
    long startTimeMillis = System.currentTimeMillis();
    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);
    MutateRowRequest request = RowMutation.create(TABLE_ID, ByteString.copyFrom(hbasePut.getRow()), unsafeMutation).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.assertTrue(startTimeMillis * 1000 <= setCell.getTimestampMicros());
    Assert.assertTrue(setCell.getTimestampMicros() <= System.currentTimeMillis() * 1000);
    Assert.assertArrayEquals(value1, setCell.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 4 with Mutation

use of com.google.cloud.bigtable.data.v2.models.Mutation in project java-bigtable-hbase by googleapis.

the class TestPutAdapter method testMultipleCellsInMultipleFamiliesAreConverted.

@Test
public void testMultipleCellsInMultipleFamiliesAreConverted() {
    byte[] row = dataHelper.randomData("rk-");
    byte[] family1 = dataHelper.randomData("f1");
    byte[] family2 = dataHelper.randomData("f2");
    byte[] qualifier1 = dataHelper.randomData("qual1");
    byte[] qualifier2 = dataHelper.randomData("qual2");
    byte[] value1 = dataHelper.randomData("v1");
    byte[] value2 = dataHelper.randomData("v1");
    long timestamp1 = 1L;
    long timestamp2 = 2L;
    Put hbasePut = new Put(row);
    hbasePut.addColumn(family1, qualifier1, timestamp1, value1);
    hbasePut.addColumn(family2, qualifier2, timestamp2, value2);
    MutateRowRequest request = adapt(hbasePut);
    Assert.assertArrayEquals(row, request.getRowKey().toByteArray());
    Assert.assertEquals(2, request.getMutationsCount());
    Mutation mutation1 = request.getMutations(0);
    Assert.assertEquals(MutationCase.SET_CELL, mutation1.getMutationCase());
    SetCell setCell = mutation1.getSetCell();
    Assert.assertArrayEquals(family1, setCell.getFamilyNameBytes().toByteArray());
    Assert.assertArrayEquals(qualifier1, setCell.getColumnQualifier().toByteArray());
    Assert.assertEquals(TimeUnit.MILLISECONDS.toMicros(timestamp1), setCell.getTimestampMicros());
    Assert.assertArrayEquals(value1, setCell.getValue().toByteArray());
    Mutation mutation2 = request.getMutations(1);
    SetCell setCell2 = mutation2.getSetCell();
    Assert.assertArrayEquals(family2, 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 5 with Mutation

use of com.google.cloud.bigtable.data.v2.models.Mutation in project java-bigtable by googleapis.

the class MutateRowsBatchingDescriptorTest method splitExceptionWithFailedMutationsTest.

@Test
public void splitExceptionWithFailedMutationsTest() {
    MutateRowsBatchingDescriptor underTest = new MutateRowsBatchingDescriptor();
    Throwable actualThrowable = null;
    BatchEntry<RowMutationEntry, Void> batchEntry1 = BatchEntry.create(RowMutationEntry.create("key1").deleteRow(), SettableApiFuture.<Void>create());
    BatchEntry<RowMutationEntry, Void> batchEntry2 = BatchEntry.create(RowMutationEntry.create("key2").deleteRow(), SettableApiFuture.<Void>create());
    BatchEntry<RowMutationEntry, Void> batchEntry3 = BatchEntry.create(RowMutationEntry.create("key3").deleteRow(), SettableApiFuture.<Void>create());
    // Threw an exception at 1st and 3rd entry
    MutateRowsException serverError = new MutateRowsException(null, ImmutableList.of(MutateRowsException.FailedMutation.create(0, new UnavailableException(null, GrpcStatusCode.of(Status.Code.UNAVAILABLE), true)), MutateRowsException.FailedMutation.create(2, new DeadlineExceededException(null, GrpcStatusCode.of(Status.Code.DEADLINE_EXCEEDED), true))), true);
    underTest.splitException(serverError, ImmutableList.of(batchEntry1, batchEntry2, batchEntry3));
    try {
        batchEntry1.getResultFuture().get();
    } catch (ExecutionException | InterruptedException e) {
        actualThrowable = e;
    }
    assertThat(actualThrowable).hasCauseThat().isEqualTo(serverError.getFailedMutations().get(0).getError());
    // As there is no exception for 2nd entry so it should not throw any exception
    actualThrowable = null;
    try {
        batchEntry2.getResultFuture().get();
    } catch (ExecutionException | InterruptedException e) {
        actualThrowable = e;
    }
    assertThat(actualThrowable).isNull();
    actualThrowable = null;
    try {
        batchEntry3.getResultFuture().get();
    } catch (ExecutionException | InterruptedException e) {
        actualThrowable = e;
    }
    // The third response should has the last found failed mutation error.
    assertThat(actualThrowable).hasCauseThat().isEqualTo(serverError.getFailedMutations().get(1).getError());
}
Also used : MutateRowsException(com.google.cloud.bigtable.data.v2.models.MutateRowsException) RowMutationEntry(com.google.cloud.bigtable.data.v2.models.RowMutationEntry) UnavailableException(com.google.api.gax.rpc.UnavailableException) DeadlineExceededException(com.google.api.gax.rpc.DeadlineExceededException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)10 RowMutation (com.google.cloud.bigtable.data.v2.models.RowMutation)9 MutateRowRequest (com.google.bigtable.v2.MutateRowRequest)7 Put (org.apache.hadoop.hbase.client.Put)6 Mutation (com.google.bigtable.v2.Mutation)5 SetCell (com.google.bigtable.v2.Mutation.SetCell)5 ConditionalRowMutation (com.google.cloud.bigtable.data.v2.models.ConditionalRowMutation)4 MutateRowsException (com.google.cloud.bigtable.data.v2.models.MutateRowsException)3 MutateRowsRequest (com.google.bigtable.v2.MutateRowsRequest)2 Builder (com.google.bigtable.v2.MutateRowsRequest.Builder)2 BigtableDataClient (com.google.cloud.bigtable.data.v2.BigtableDataClient)2 FailedMutation (com.google.cloud.bigtable.data.v2.models.MutateRowsException.FailedMutation)2 ApiException (com.google.api.gax.rpc.ApiException)1 DeadlineExceededException (com.google.api.gax.rpc.DeadlineExceededException)1 UnavailableException (com.google.api.gax.rpc.UnavailableException)1 MutateRowsResponse (com.google.bigtable.v2.MutateRowsResponse)1 Entry (com.google.bigtable.v2.MutateRowsResponse.Entry)1 Filter (com.google.cloud.bigtable.data.v2.models.Filters.Filter)1 Mutation (com.google.cloud.bigtable.data.v2.models.Mutation)1 ReadModifyWriteRow (com.google.cloud.bigtable.data.v2.models.ReadModifyWriteRow)1