Search in sources :

Example 1 with ConditionalRowMutation

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

the class AbstractBigtableTable method checkAndDelete.

/**
 * {@inheritDoc}
 */
@Override
public boolean checkAndDelete(byte[] row, byte[] family, byte[] qualifier, CompareFilter.CompareOp compareOp, byte[] value, Delete delete) throws IOException {
    LOG.trace("checkAndDelete(byte[], byte[], byte[], CompareOp, byte[], Delete)");
    ConditionalRowMutation request = new CheckAndMutateUtil.RequestBuilder(hbaseAdapter, row, family).qualifier(qualifier).ifMatches(compareOp, value).withDelete(delete).build();
    return checkAndMutate(row, request, "checkAndDelete");
}
Also used : ConditionalRowMutation(com.google.cloud.bigtable.data.v2.models.ConditionalRowMutation)

Example 2 with ConditionalRowMutation

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

the class TestDataClientVeneerApi method testCheckAndMutateRowAsync.

@Test
public void testCheckAndMutateRowAsync() throws Exception {
    ConditionalRowMutation conditionalRowM = ConditionalRowMutation.create(TABLE_ID, ROW_KEY);
    when(mockDataClient.checkAndMutateRowAsync(conditionalRowM)).thenReturn(ApiFutures.immediateFuture(Boolean.TRUE));
    assertTrue(dataClientWrapper.checkAndMutateRowAsync(conditionalRowM).get());
    verify(mockDataClient).checkAndMutateRowAsync(conditionalRowM);
}
Also used : ConditionalRowMutation(com.google.cloud.bigtable.data.v2.models.ConditionalRowMutation) Test(org.junit.Test)

Example 3 with ConditionalRowMutation

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

the class BigtableTable method checkAndMutate.

@Override
public CheckAndMutateBuilder checkAndMutate(byte[] row, byte[] family) {
    final CheckAndMutateUtil.RequestBuilder builder = new CheckAndMutateUtil.RequestBuilder(hbaseAdapter, row, family);
    return new CheckAndMutateBuilder() {

        /**
         * {@inheritDoc}
         */
        @Override
        public CheckAndMutateBuilder qualifier(byte[] qualifier) {
            builder.qualifier(qualifier);
            return this;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public CheckAndMutateBuilder ifNotExists() {
            builder.ifNotExists();
            return this;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public CheckAndMutateBuilder ifMatches(CompareOperator compareOp, byte[] value) {
            Preconditions.checkNotNull(compareOp, "compareOp is null");
            if (compareOp != CompareOperator.NOT_EQUAL) {
                Preconditions.checkNotNull(value, "value is null for compareOperator: " + compareOp);
            }
            builder.ifMatches(BigtableTable.toCompareOp(compareOp), value);
            return this;
        }

        /**
         * {@inheritDoc}
         */
        public CheckAndMutateBuilder timeRange(TimeRange timeRange) {
            builder.timeRange(timeRange.getMin(), timeRange.getMax());
            return this;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean thenPut(Put put) throws IOException {
            try {
                builder.withPut(put);
                return call();
            } catch (Exception e) {
                throw new IOException("Could not CheckAndMutate.thenPut: " + e.getMessage(), e);
            }
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean thenDelete(Delete delete) throws IOException {
            try {
                builder.withDelete(delete);
                return call();
            } catch (Exception e) {
                throw new IOException("Could not CheckAndMutate.thenDelete: " + e.getMessage(), e);
            }
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean thenMutate(RowMutations rowMutations) throws IOException {
            try {
                builder.withMutations(rowMutations);
                return call();
            } catch (Exception e) {
                throw new IOException("Could not CheckAndMutate.thenMutate: " + e.getMessage(), e);
            }
        }

        private boolean call() throws IOException {
            ConditionalRowMutation conditionalRowMutation = builder.build();
            Boolean response = FutureUtil.unwrap(clientWrapper.checkAndMutateRowAsync(conditionalRowMutation));
            return CheckAndMutateUtil.wasMutationApplied(conditionalRowMutation, response);
        }
    };
}
Also used : Delete(org.apache.hadoop.hbase.client.Delete) IOException(java.io.IOException) Put(org.apache.hadoop.hbase.client.Put) IOException(java.io.IOException) RowMutations(org.apache.hadoop.hbase.client.RowMutations) CompareOperator(org.apache.hadoop.hbase.CompareOperator) CheckAndMutateUtil(com.google.cloud.bigtable.hbase.adapters.CheckAndMutateUtil) TimeRange(org.apache.hadoop.hbase.io.TimeRange) ConditionalRowMutation(com.google.cloud.bigtable.data.v2.models.ConditionalRowMutation)

Example 4 with ConditionalRowMutation

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

the class HeaderTracerCallableTest method testGFELatencyCheckAndMutateRow.

@Test
public void testGFELatencyCheckAndMutateRow() throws InterruptedException {
    ConditionalRowMutation mutation = ConditionalRowMutation.create(TABLE_ID, "fake-key").then(Mutation.create().setCell("fake-family", "fake-qualifier", "fake-value"));
    stub.checkAndMutateRowCallable().call(mutation);
    Thread.sleep(WAIT_FOR_METRICS_TIME_MS);
    long latency = StatsTestUtils.getAggregationValueAsLong(localStats, RpcViewConstants.BIGTABLE_GFE_LATENCY_VIEW, ImmutableMap.of(RpcMeasureConstants.BIGTABLE_OP, TagValue.create("Bigtable.CheckAndMutateRow"), RpcMeasureConstants.BIGTABLE_STATUS, TagValue.create("OK")), PROJECT_ID, INSTANCE_ID, APP_PROFILE_ID);
    assertThat(latency).isEqualTo(fakeServerTiming.get());
}
Also used : ConditionalRowMutation(com.google.cloud.bigtable.data.v2.models.ConditionalRowMutation) Test(org.junit.Test)

Example 5 with ConditionalRowMutation

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

the class WriteConditionally method writeConditionally.

public static void writeConditionally(String projectId, String instanceId, String tableId) {
    try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
        long timestamp = System.currentTimeMillis() * 1000;
        String rowkey = "phone#4c410523#20190501";
        Mutation mutation = Mutation.create().setCell(COLUMN_FAMILY_NAME, "os_name", timestamp, "android");
        Filter filter = FILTERS.chain().filter(FILTERS.family().exactMatch(COLUMN_FAMILY_NAME)).filter(FILTERS.qualifier().exactMatch("os_build")).filter(FILTERS.value().regex("PQ2A\\..*"));
        ConditionalRowMutation conditionalRowMutation = ConditionalRowMutation.create(tableId, rowkey).condition(filter).then(mutation);
        boolean success = dataClient.checkAndMutateRow(conditionalRowMutation);
        System.out.printf("Successfully updated row's os_name: %b", success);
    } catch (Exception e) {
        System.out.println("Error during WriteConditionally: \n" + e.toString());
        e.printStackTrace();
    }
}
Also used : ConditionalRowMutation(com.google.cloud.bigtable.data.v2.models.ConditionalRowMutation) Filter(com.google.cloud.bigtable.data.v2.models.Filters.Filter) Mutation(com.google.cloud.bigtable.data.v2.models.Mutation) ConditionalRowMutation(com.google.cloud.bigtable.data.v2.models.ConditionalRowMutation) BigtableDataClient(com.google.cloud.bigtable.data.v2.BigtableDataClient)

Aggregations

ConditionalRowMutation (com.google.cloud.bigtable.data.v2.models.ConditionalRowMutation)9 Test (org.junit.Test)4 CheckAndMutateRowRequest (com.google.bigtable.v2.CheckAndMutateRowRequest)1 CheckAndMutateRowResponse (com.google.bigtable.v2.CheckAndMutateRowResponse)1 BigtableDataClient (com.google.cloud.bigtable.data.v2.BigtableDataClient)1 Filter (com.google.cloud.bigtable.data.v2.models.Filters.Filter)1 Mutation (com.google.cloud.bigtable.data.v2.models.Mutation)1 HeaderTracerUnaryCallable (com.google.cloud.bigtable.data.v2.stub.metrics.HeaderTracerUnaryCallable)1 StatsHeadersUnaryCallable (com.google.cloud.bigtable.data.v2.stub.metrics.StatsHeadersUnaryCallable)1 CheckAndMutateUtil (com.google.cloud.bigtable.hbase.adapters.CheckAndMutateUtil)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ByteString (com.google.protobuf.ByteString)1 IOException (java.io.IOException)1 Map (java.util.Map)1 CompareOperator (org.apache.hadoop.hbase.CompareOperator)1 Delete (org.apache.hadoop.hbase.client.Delete)1 Put (org.apache.hadoop.hbase.client.Put)1 RowMutations (org.apache.hadoop.hbase.client.RowMutations)1 TimeRange (org.apache.hadoop.hbase.io.TimeRange)1