Search in sources :

Example 6 with CheckAndMutateResult

use of org.apache.hadoop.hbase.client.CheckAndMutateResult in project hbase by apache.

the class TestHRegion method testCheckAndDeleteThatDeleteWasWritten.

@Test
public void testCheckAndDeleteThatDeleteWasWritten() throws IOException {
    byte[] row1 = Bytes.toBytes("row1");
    byte[] fam1 = Bytes.toBytes("fam1");
    byte[] fam2 = Bytes.toBytes("fam2");
    byte[] qf1 = Bytes.toBytes("qualifier1");
    byte[] qf2 = Bytes.toBytes("qualifier2");
    byte[] qf3 = Bytes.toBytes("qualifier3");
    byte[] val1 = Bytes.toBytes("value1");
    byte[] val2 = Bytes.toBytes("value2");
    byte[] val3 = Bytes.toBytes("value3");
    byte[] emptyVal = new byte[] {};
    byte[][] families = { fam1, fam2 };
    // Setting up region
    this.region = initHRegion(tableName, method, CONF, families);
    // Put content
    Put put = new Put(row1);
    put.addColumn(fam1, qf1, val1);
    region.put(put);
    Threads.sleep(2);
    put = new Put(row1);
    put.addColumn(fam1, qf1, val2);
    put.addColumn(fam2, qf1, val3);
    put.addColumn(fam2, qf2, val2);
    put.addColumn(fam2, qf3, val1);
    put.addColumn(fam1, qf3, val1);
    region.put(put);
    LOG.info("get={}", region.get(new Get(row1).addColumn(fam1, qf1)).toString());
    // Multi-column delete
    Delete delete = new Delete(row1);
    delete.addColumn(fam1, qf1);
    delete.addColumn(fam2, qf1);
    delete.addColumn(fam1, qf3);
    CheckAndMutateResult res = region.checkAndMutate(CheckAndMutate.newBuilder(row1).ifMatches(fam1, qf1, CompareOperator.EQUAL, val2).build(delete));
    assertTrue(res.isSuccess());
    assertNull(res.getResult());
    Get get = new Get(row1);
    get.addColumn(fam1, qf1);
    get.addColumn(fam1, qf3);
    get.addColumn(fam2, qf2);
    Result r = region.get(get);
    assertEquals(2, r.size());
    assertArrayEquals(val1, r.getValue(fam1, qf1));
    assertArrayEquals(val2, r.getValue(fam2, qf2));
    // Family delete
    delete = new Delete(row1);
    delete.addFamily(fam2);
    res = region.checkAndMutate(CheckAndMutate.newBuilder(row1).ifMatches(fam2, qf1, CompareOperator.EQUAL, emptyVal).build(delete));
    assertTrue(res.isSuccess());
    assertNull(res.getResult());
    get = new Get(row1);
    r = region.get(get);
    assertEquals(1, r.size());
    assertArrayEquals(val1, r.getValue(fam1, qf1));
    // Row delete
    delete = new Delete(row1);
    res = region.checkAndMutate(CheckAndMutate.newBuilder(row1).ifMatches(fam1, qf1, CompareOperator.EQUAL, val1).build(delete));
    assertTrue(res.isSuccess());
    assertNull(res.getResult());
    get = new Get(row1);
    r = region.get(get);
    assertEquals(0, r.size());
}
Also used : Delete(org.apache.hadoop.hbase.client.Delete) CheckAndMutateResult(org.apache.hadoop.hbase.client.CheckAndMutateResult) Get(org.apache.hadoop.hbase.client.Get) Put(org.apache.hadoop.hbase.client.Put) CheckAndMutateResult(org.apache.hadoop.hbase.client.CheckAndMutateResult) Result(org.apache.hadoop.hbase.client.Result) Test(org.junit.Test)

Example 7 with CheckAndMutateResult

use of org.apache.hadoop.hbase.client.CheckAndMutateResult in project hbase by apache.

the class TestHRegion method testCheckAndRowMutations.

@Test
public void testCheckAndRowMutations() throws Throwable {
    final byte[] row = Bytes.toBytes("row");
    final byte[] q1 = Bytes.toBytes("q1");
    final byte[] q2 = Bytes.toBytes("q2");
    final byte[] q3 = Bytes.toBytes("q3");
    final byte[] q4 = Bytes.toBytes("q4");
    final String v1 = "v1";
    region = initHRegion(tableName, method, CONF, fam1);
    // Initial values
    region.batchMutate(new Mutation[] { new Put(row).addColumn(fam1, q2, Bytes.toBytes("toBeDeleted")), new Put(row).addColumn(fam1, q3, Bytes.toBytes(5L)), new Put(row).addColumn(fam1, q4, Bytes.toBytes("a")) });
    // Do CheckAndRowMutations
    CheckAndMutate checkAndMutate = CheckAndMutate.newBuilder(row).ifNotExists(fam1, q1).build(new RowMutations(row).add(Arrays.asList(new Put(row).addColumn(fam1, q1, Bytes.toBytes(v1)), new Delete(row).addColumns(fam1, q2), new Increment(row).addColumn(fam1, q3, 1), new Append(row).addColumn(fam1, q4, Bytes.toBytes("b")))));
    CheckAndMutateResult result = region.checkAndMutate(checkAndMutate);
    assertTrue(result.isSuccess());
    assertEquals(6L, Bytes.toLong(result.getResult().getValue(fam1, q3)));
    assertEquals("ab", Bytes.toString(result.getResult().getValue(fam1, q4)));
    // Verify the value
    Result r = region.get(new Get(row));
    assertEquals(v1, Bytes.toString(r.getValue(fam1, q1)));
    assertNull(r.getValue(fam1, q2));
    assertEquals(6L, Bytes.toLong(r.getValue(fam1, q3)));
    assertEquals("ab", Bytes.toString(r.getValue(fam1, q4)));
    // Do CheckAndRowMutations again
    checkAndMutate = CheckAndMutate.newBuilder(row).ifNotExists(fam1, q1).build(new RowMutations(row).add(Arrays.asList(new Delete(row).addColumns(fam1, q1), new Put(row).addColumn(fam1, q2, Bytes.toBytes(v1)), new Increment(row).addColumn(fam1, q3, 1), new Append(row).addColumn(fam1, q4, Bytes.toBytes("b")))));
    result = region.checkAndMutate(checkAndMutate);
    assertFalse(result.isSuccess());
    assertNull(result.getResult());
    // Verify the value
    r = region.get(new Get(row));
    assertEquals(v1, Bytes.toString(r.getValue(fam1, q1)));
    assertNull(r.getValue(fam1, q2));
    assertEquals(6L, Bytes.toLong(r.getValue(fam1, q3)));
    assertEquals("ab", Bytes.toString(r.getValue(fam1, q4)));
}
Also used : Delete(org.apache.hadoop.hbase.client.Delete) Append(org.apache.hadoop.hbase.client.Append) CheckAndMutateResult(org.apache.hadoop.hbase.client.CheckAndMutateResult) Increment(org.apache.hadoop.hbase.client.Increment) Get(org.apache.hadoop.hbase.client.Get) CheckAndMutate(org.apache.hadoop.hbase.client.CheckAndMutate) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ByteString(org.apache.hbase.thirdparty.com.google.protobuf.ByteString) Put(org.apache.hadoop.hbase.client.Put) RowMutations(org.apache.hadoop.hbase.client.RowMutations) CheckAndMutateResult(org.apache.hadoop.hbase.client.CheckAndMutateResult) Result(org.apache.hadoop.hbase.client.Result) Test(org.junit.Test)

Example 8 with CheckAndMutateResult

use of org.apache.hadoop.hbase.client.CheckAndMutateResult in project hbase by apache.

the class TestHRegion method testCheckAndIncrement.

@Test
public void testCheckAndIncrement() throws Throwable {
    final byte[] FAMILY = Bytes.toBytes("fam");
    // Setting up region
    this.region = initHRegion(tableName, method, CONF, FAMILY);
    region.put(new Put(row).addColumn(FAMILY, Bytes.toBytes("A"), Bytes.toBytes("a")));
    // CheckAndIncrement with correct value
    CheckAndMutateResult res = region.checkAndMutate(CheckAndMutate.newBuilder(row).ifEquals(FAMILY, Bytes.toBytes("A"), Bytes.toBytes("a")).build(new Increment(row).addColumn(FAMILY, Bytes.toBytes("B"), 1)));
    assertTrue(res.isSuccess());
    assertEquals(1, Bytes.toLong(res.getResult().getValue(FAMILY, Bytes.toBytes("B"))));
    Result result = region.get(new Get(row).addColumn(FAMILY, Bytes.toBytes("B")));
    assertEquals(1, Bytes.toLong(result.getValue(FAMILY, Bytes.toBytes("B"))));
    // CheckAndIncrement with wrong value
    res = region.checkAndMutate(CheckAndMutate.newBuilder(row).ifEquals(FAMILY, Bytes.toBytes("A"), Bytes.toBytes("b")).build(new Increment(row).addColumn(FAMILY, Bytes.toBytes("B"), 1)));
    assertFalse(res.isSuccess());
    assertNull(res.getResult());
    result = region.get(new Get(row).addColumn(FAMILY, Bytes.toBytes("B")));
    assertEquals(1, Bytes.toLong(result.getValue(FAMILY, Bytes.toBytes("B"))));
    region.put(new Put(row).addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("c")));
    // CheckAndIncrement with a filter and correct value
    res = region.checkAndMutate(CheckAndMutate.newBuilder(row).ifMatches(new FilterList(new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"), CompareOperator.EQUAL, Bytes.toBytes("a")), new SingleColumnValueFilter(FAMILY, Bytes.toBytes("C"), CompareOperator.EQUAL, Bytes.toBytes("c")))).build(new Increment(row).addColumn(FAMILY, Bytes.toBytes("B"), 2)));
    assertTrue(res.isSuccess());
    assertEquals(3, Bytes.toLong(res.getResult().getValue(FAMILY, Bytes.toBytes("B"))));
    result = region.get(new Get(row).addColumn(FAMILY, Bytes.toBytes("B")));
    assertEquals(3, Bytes.toLong(result.getValue(FAMILY, Bytes.toBytes("B"))));
    // CheckAndIncrement with a filter and correct value
    res = region.checkAndMutate(CheckAndMutate.newBuilder(row).ifMatches(new FilterList(new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"), CompareOperator.EQUAL, Bytes.toBytes("b")), new SingleColumnValueFilter(FAMILY, Bytes.toBytes("C"), CompareOperator.EQUAL, Bytes.toBytes("d")))).build(new Increment(row).addColumn(FAMILY, Bytes.toBytes("B"), 2)));
    assertFalse(res.isSuccess());
    assertNull(res.getResult());
    result = region.get(new Get(row).addColumn(FAMILY, Bytes.toBytes("B")));
    assertEquals(3, Bytes.toLong(result.getValue(FAMILY, Bytes.toBytes("B"))));
}
Also used : SingleColumnValueFilter(org.apache.hadoop.hbase.filter.SingleColumnValueFilter) CheckAndMutateResult(org.apache.hadoop.hbase.client.CheckAndMutateResult) Increment(org.apache.hadoop.hbase.client.Increment) Get(org.apache.hadoop.hbase.client.Get) FilterList(org.apache.hadoop.hbase.filter.FilterList) Put(org.apache.hadoop.hbase.client.Put) CheckAndMutateResult(org.apache.hadoop.hbase.client.CheckAndMutateResult) Result(org.apache.hadoop.hbase.client.Result) Test(org.junit.Test)

Example 9 with CheckAndMutateResult

use of org.apache.hadoop.hbase.client.CheckAndMutateResult in project hbase by apache.

the class TestHRegion method testCheckAndMutateWithFiltersAndTimeRange.

@Test
public void testCheckAndMutateWithFiltersAndTimeRange() throws Throwable {
    final byte[] FAMILY = Bytes.toBytes("fam");
    // Setting up region
    this.region = initHRegion(tableName, method, CONF, FAMILY);
    // Put with specifying the timestamp
    region.put(new Put(row).addColumn(FAMILY, Bytes.toBytes("A"), 100, Bytes.toBytes("a")));
    // Put with success
    CheckAndMutateResult res = region.checkAndMutate(CheckAndMutate.newBuilder(row).ifMatches(new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"), CompareOperator.EQUAL, Bytes.toBytes("a"))).timeRange(TimeRange.between(0, 101)).build(new Put(row).addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b"))));
    assertTrue(res.isSuccess());
    assertNull(res.getResult());
    Result result = region.get(new Get(row).addColumn(FAMILY, Bytes.toBytes("B")));
    assertEquals("b", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("B"))));
    // Put with failure
    res = region.checkAndMutate(CheckAndMutate.newBuilder(row).ifMatches(new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"), CompareOperator.EQUAL, Bytes.toBytes("a"))).timeRange(TimeRange.between(0, 100)).build(new Put(row).addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("c"))));
    assertFalse(res.isSuccess());
    assertNull(res.getResult());
    assertTrue(region.get(new Get(row).addColumn(FAMILY, Bytes.toBytes("C"))).isEmpty());
    // RowMutations with success
    res = region.checkAndMutate(CheckAndMutate.newBuilder(row).ifMatches(new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"), CompareOperator.EQUAL, Bytes.toBytes("a"))).timeRange(TimeRange.between(0, 101)).build(new RowMutations(row).add((Mutation) new Put(row).addColumn(FAMILY, Bytes.toBytes("D"), Bytes.toBytes("d"))).add((Mutation) new Delete(row).addColumns(FAMILY, Bytes.toBytes("A")))));
    assertTrue(res.isSuccess());
    assertNull(res.getResult());
    result = region.get(new Get(row).addColumn(FAMILY, Bytes.toBytes("D")));
    assertEquals("d", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("D"))));
    assertTrue(region.get(new Get(row).addColumn(FAMILY, Bytes.toBytes("A"))).isEmpty());
}
Also used : Delete(org.apache.hadoop.hbase.client.Delete) SingleColumnValueFilter(org.apache.hadoop.hbase.filter.SingleColumnValueFilter) CheckAndMutateResult(org.apache.hadoop.hbase.client.CheckAndMutateResult) Get(org.apache.hadoop.hbase.client.Get) Mutation(org.apache.hadoop.hbase.client.Mutation) Put(org.apache.hadoop.hbase.client.Put) CheckAndMutateResult(org.apache.hadoop.hbase.client.CheckAndMutateResult) Result(org.apache.hadoop.hbase.client.Result) RowMutations(org.apache.hadoop.hbase.client.RowMutations) Test(org.junit.Test)

Example 10 with CheckAndMutateResult

use of org.apache.hadoop.hbase.client.CheckAndMutateResult in project hbase by apache.

the class TestHRegion method testCheckAndMutateWithFilters.

@Test
public void testCheckAndMutateWithFilters() throws Throwable {
    final byte[] FAMILY = Bytes.toBytes("fam");
    // Setting up region
    this.region = initHRegion(tableName, method, CONF, FAMILY);
    // Put one row
    Put put = new Put(row);
    put.addColumn(FAMILY, Bytes.toBytes("A"), Bytes.toBytes("a"));
    put.addColumn(FAMILY, Bytes.toBytes("B"), Bytes.toBytes("b"));
    put.addColumn(FAMILY, Bytes.toBytes("C"), Bytes.toBytes("c"));
    region.put(put);
    // Put with success
    CheckAndMutateResult res = region.checkAndMutate(CheckAndMutate.newBuilder(row).ifMatches(new FilterList(new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"), CompareOperator.EQUAL, Bytes.toBytes("a")), new SingleColumnValueFilter(FAMILY, Bytes.toBytes("B"), CompareOperator.EQUAL, Bytes.toBytes("b")))).build(new Put(row).addColumn(FAMILY, Bytes.toBytes("D"), Bytes.toBytes("d"))));
    assertTrue(res.isSuccess());
    assertNull(res.getResult());
    Result result = region.get(new Get(row).addColumn(FAMILY, Bytes.toBytes("D")));
    assertEquals("d", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("D"))));
    // Put with failure
    res = region.checkAndMutate(CheckAndMutate.newBuilder(row).ifMatches(new FilterList(new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"), CompareOperator.EQUAL, Bytes.toBytes("a")), new SingleColumnValueFilter(FAMILY, Bytes.toBytes("B"), CompareOperator.EQUAL, Bytes.toBytes("c")))).build(new Put(row).addColumn(FAMILY, Bytes.toBytes("E"), Bytes.toBytes("e"))));
    assertFalse(res.isSuccess());
    assertNull(res.getResult());
    assertTrue(region.get(new Get(row).addColumn(FAMILY, Bytes.toBytes("E"))).isEmpty());
    // Delete with success
    res = region.checkAndMutate(CheckAndMutate.newBuilder(row).ifMatches(new FilterList(new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"), CompareOperator.EQUAL, Bytes.toBytes("a")), new SingleColumnValueFilter(FAMILY, Bytes.toBytes("B"), CompareOperator.EQUAL, Bytes.toBytes("b")))).build(new Delete(row).addColumns(FAMILY, Bytes.toBytes("D"))));
    assertTrue(res.isSuccess());
    assertNull(res.getResult());
    assertTrue(region.get(new Get(row).addColumn(FAMILY, Bytes.toBytes("D"))).isEmpty());
    // Mutate with success
    res = region.checkAndMutate(CheckAndMutate.newBuilder(row).ifMatches(new FilterList(new SingleColumnValueFilter(FAMILY, Bytes.toBytes("A"), CompareOperator.EQUAL, Bytes.toBytes("a")), new SingleColumnValueFilter(FAMILY, Bytes.toBytes("B"), CompareOperator.EQUAL, Bytes.toBytes("b")))).build(new RowMutations(row).add((Mutation) new Put(row).addColumn(FAMILY, Bytes.toBytes("E"), Bytes.toBytes("e"))).add((Mutation) new Delete(row).addColumns(FAMILY, Bytes.toBytes("A")))));
    assertTrue(res.isSuccess());
    assertNull(res.getResult());
    result = region.get(new Get(row).addColumn(FAMILY, Bytes.toBytes("E")));
    assertEquals("e", Bytes.toString(result.getValue(FAMILY, Bytes.toBytes("E"))));
    assertTrue(region.get(new Get(row).addColumn(FAMILY, Bytes.toBytes("A"))).isEmpty());
}
Also used : Delete(org.apache.hadoop.hbase.client.Delete) SingleColumnValueFilter(org.apache.hadoop.hbase.filter.SingleColumnValueFilter) CheckAndMutateResult(org.apache.hadoop.hbase.client.CheckAndMutateResult) Get(org.apache.hadoop.hbase.client.Get) FilterList(org.apache.hadoop.hbase.filter.FilterList) Mutation(org.apache.hadoop.hbase.client.Mutation) Put(org.apache.hadoop.hbase.client.Put) CheckAndMutateResult(org.apache.hadoop.hbase.client.CheckAndMutateResult) Result(org.apache.hadoop.hbase.client.Result) RowMutations(org.apache.hadoop.hbase.client.RowMutations) Test(org.junit.Test)

Aggregations

CheckAndMutateResult (org.apache.hadoop.hbase.client.CheckAndMutateResult)20 Put (org.apache.hadoop.hbase.client.Put)13 Test (org.junit.Test)12 Result (org.apache.hadoop.hbase.client.Result)10 Get (org.apache.hadoop.hbase.client.Get)9 Delete (org.apache.hadoop.hbase.client.Delete)8 Mutation (org.apache.hadoop.hbase.client.Mutation)5 RowMutations (org.apache.hadoop.hbase.client.RowMutations)5 IOException (java.io.IOException)4 Append (org.apache.hadoop.hbase.client.Append)4 CheckAndMutate (org.apache.hadoop.hbase.client.CheckAndMutate)4 Increment (org.apache.hadoop.hbase.client.Increment)4 SingleColumnValueFilter (org.apache.hadoop.hbase.filter.SingleColumnValueFilter)4 DoNotRetryIOException (org.apache.hadoop.hbase.DoNotRetryIOException)3 FilterList (org.apache.hadoop.hbase.filter.FilterList)3 MutationType (org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MutationProto.MutationType)3 UncheckedIOException (java.io.UncheckedIOException)2 BigDecimal (java.math.BigDecimal)2 ArrayList (java.util.ArrayList)2 Cell (org.apache.hadoop.hbase.Cell)2