Search in sources :

Example 71 with Increment

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

the class TestHRegion method testCheckAndIncrementAndAppend.

@Test
public void testCheckAndIncrementAndAppend() throws Throwable {
    // Setting up region
    this.region = initHRegion(tableName, method, CONF, fam1);
    // CheckAndMutate with Increment and Append
    CheckAndMutate checkAndMutate = CheckAndMutate.newBuilder(row).ifNotExists(fam1, qual).build(new RowMutations(row).add((Mutation) new Increment(row).addColumn(fam1, qual1, 1L)).add((Mutation) new Append(row).addColumn(fam1, qual2, Bytes.toBytes("a"))));
    CheckAndMutateResult result = region.checkAndMutate(checkAndMutate);
    assertTrue(result.isSuccess());
    assertEquals(1L, Bytes.toLong(result.getResult().getValue(fam1, qual1)));
    assertEquals("a", Bytes.toString(result.getResult().getValue(fam1, qual2)));
    Result r = region.get(new Get(row));
    assertEquals(1L, Bytes.toLong(r.getValue(fam1, qual1)));
    assertEquals("a", Bytes.toString(r.getValue(fam1, qual2)));
    // Set return results to false
    checkAndMutate = CheckAndMutate.newBuilder(row).ifNotExists(fam1, qual).build(new RowMutations(row).add((Mutation) new Increment(row).addColumn(fam1, qual1, 1L).setReturnResults(false)).add((Mutation) new Append(row).addColumn(fam1, qual2, Bytes.toBytes("a")).setReturnResults(false)));
    result = region.checkAndMutate(checkAndMutate);
    assertTrue(result.isSuccess());
    assertNull(result.getResult().getValue(fam1, qual1));
    assertNull(result.getResult().getValue(fam1, qual2));
    r = region.get(new Get(row));
    assertEquals(2L, Bytes.toLong(r.getValue(fam1, qual1)));
    assertEquals("aa", Bytes.toString(r.getValue(fam1, qual2)));
    checkAndMutate = CheckAndMutate.newBuilder(row).ifNotExists(fam1, qual).build(new RowMutations(row).add((Mutation) new Increment(row).addColumn(fam1, qual1, 1L)).add((Mutation) new Append(row).addColumn(fam1, qual2, Bytes.toBytes("a")).setReturnResults(false)));
    result = region.checkAndMutate(checkAndMutate);
    assertTrue(result.isSuccess());
    assertEquals(3L, Bytes.toLong(result.getResult().getValue(fam1, qual1)));
    assertNull(result.getResult().getValue(fam1, qual2));
    r = region.get(new Get(row));
    assertEquals(3L, Bytes.toLong(r.getValue(fam1, qual1)));
    assertEquals("aaa", Bytes.toString(r.getValue(fam1, qual2)));
}
Also used : 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) Mutation(org.apache.hadoop.hbase.client.Mutation) 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 72 with Increment

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

the class TestHRegion method testMutateRow.

@Test
public void testMutateRow() throws Exception {
    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 mutateRow
    Result result = region.mutateRow(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")))));
    assertNotNull(result);
    assertEquals(6L, Bytes.toLong(result.getValue(fam1, q3)));
    assertEquals("ab", Bytes.toString(result.getValue(fam1, q4)));
    // Verify the value
    result = region.get(new Get(row));
    assertEquals(v1, Bytes.toString(result.getValue(fam1, q1)));
    assertNull(result.getValue(fam1, q2));
    assertEquals(6L, Bytes.toLong(result.getValue(fam1, q3)));
    assertEquals("ab", Bytes.toString(result.getValue(fam1, q4)));
}
Also used : Delete(org.apache.hadoop.hbase.client.Delete) Append(org.apache.hadoop.hbase.client.Append) Increment(org.apache.hadoop.hbase.client.Increment) Get(org.apache.hadoop.hbase.client.Get) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ByteString(org.apache.hbase.thirdparty.com.google.protobuf.ByteString) 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 73 with Increment

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

the class TestHRegion method testIncrementTimestampsAreMonotonic.

@Test
public void testIncrementTimestampsAreMonotonic() throws IOException {
    region = initHRegion(tableName, method, CONF, fam1);
    ManualEnvironmentEdge edge = new ManualEnvironmentEdge();
    EnvironmentEdgeManager.injectEdge(edge);
    edge.setValue(10);
    Increment inc = new Increment(row);
    inc.setDurability(Durability.SKIP_WAL);
    inc.addColumn(fam1, qual1, 1L);
    region.increment(inc);
    Result result = region.get(new Get(row));
    Cell c = result.getColumnLatestCell(fam1, qual1);
    assertNotNull(c);
    assertEquals(10L, c.getTimestamp());
    // clock goes back
    edge.setValue(1);
    region.increment(inc);
    result = region.get(new Get(row));
    c = result.getColumnLatestCell(fam1, qual1);
    assertEquals(11L, c.getTimestamp());
    assertEquals(2L, Bytes.toLong(c.getValueArray(), c.getValueOffset(), c.getValueLength()));
}
Also used : Increment(org.apache.hadoop.hbase.client.Increment) Get(org.apache.hadoop.hbase.client.Get) Cell(org.apache.hadoop.hbase.Cell) ManualEnvironmentEdge(org.apache.hadoop.hbase.util.ManualEnvironmentEdge) CheckAndMutateResult(org.apache.hadoop.hbase.client.CheckAndMutateResult) Result(org.apache.hadoop.hbase.client.Result) Test(org.junit.Test)

Example 74 with Increment

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

the class TestCellACLWithMultipleVersions method testCellPermissionsForIncrementWithMultipleVersions.

@Test
public void testCellPermissionsForIncrementWithMultipleVersions() throws Exception {
    final byte[] TEST_ROW1 = Bytes.toBytes("r1");
    final byte[] TEST_Q1 = Bytes.toBytes("q1");
    final byte[] TEST_Q2 = Bytes.toBytes("q2");
    final byte[] ZERO = Bytes.toBytes(0L);
    final User user1 = User.createUserForTesting(conf, "user1", new String[0]);
    final User user2 = User.createUserForTesting(conf, "user2", new String[0]);
    verifyAllowed(new AccessTestAction() {

        @Override
        public Object run() throws Exception {
            try (Connection connection = ConnectionFactory.createConnection(conf)) {
                try (Table t = connection.getTable(testTable.getTableName())) {
                    Map<String, Permission> permsU1andOwner = prepareCellPermissions(new String[] { user1.getShortName(), USER_OWNER.getShortName() }, Action.READ, Action.WRITE);
                    Map<String, Permission> permsU2andGUandOwner = prepareCellPermissions(new String[] { user2.getShortName(), AuthUtil.toGroupEntry(GROUP), USER_OWNER.getShortName() }, Action.READ, Action.WRITE);
                    Put p = new Put(TEST_ROW1);
                    p.addColumn(TEST_FAMILY1, TEST_Q1, 123, ZERO);
                    p.setACL(permsU1andOwner);
                    t.put(p);
                    p = new Put(TEST_ROW1);
                    p.addColumn(TEST_FAMILY1, TEST_Q2, 123, ZERO);
                    p.setACL(permsU2andGUandOwner);
                    t.put(p);
                    p = new Put(TEST_ROW1);
                    p.addColumn(TEST_FAMILY1, TEST_Q1, 127, ZERO);
                    p.setACL(permsU2andGUandOwner);
                    t.put(p);
                    p = new Put(TEST_ROW1);
                    p.addColumn(TEST_FAMILY1, TEST_Q2, 127, ZERO);
                    p.setACL(permsU1andOwner);
                    t.put(p);
                }
            }
            return null;
        }
    }, USER_OWNER);
    // Increment considers the TimeRange set on it.
    user1.runAs(new PrivilegedExceptionAction<Void>() {

        @Override
        public Void run() throws Exception {
            try (Connection connection = ConnectionFactory.createConnection(conf)) {
                try (Table t = connection.getTable(testTable.getTableName())) {
                    Increment inc = new Increment(TEST_ROW1);
                    inc.setTimeRange(0, 123);
                    inc.addColumn(TEST_FAMILY1, TEST_Q1, 2L);
                    t.increment(inc);
                    t.incrementColumnValue(TEST_ROW1, TEST_FAMILY1, TEST_Q2, 1L);
                }
            }
            return null;
        }
    });
    verifyUserDeniedForIncrementMultipleVersions(user2, TEST_ROW1, TEST_Q2);
    verifyUserDeniedForIncrementMultipleVersions(GROUP_USER, TEST_ROW1, TEST_Q2);
}
Also used : User(org.apache.hadoop.hbase.security.User) Table(org.apache.hadoop.hbase.client.Table) Connection(org.apache.hadoop.hbase.client.Connection) TableNotFoundException(org.apache.hadoop.hbase.TableNotFoundException) IOException(java.io.IOException) Put(org.apache.hadoop.hbase.client.Put) Increment(org.apache.hadoop.hbase.client.Increment) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 75 with Increment

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

the class TestCellACLs method testCellPermissions.

@Test
public void testCellPermissions() throws Exception {
    // store two sets of values, one store with a cell level ACL, and one without
    verifyAllowed(new AccessTestAction() {

        @Override
        public Object run() throws Exception {
            try (Connection connection = ConnectionFactory.createConnection(conf);
                Table t = connection.getTable(testTable.getTableName())) {
                Put p;
                // with ro ACL
                p = new Put(TEST_ROW).addColumn(TEST_FAMILY, TEST_Q1, ZERO);
                p.setACL(prepareCellPermissions(usersAndGroups, Action.READ));
                t.put(p);
                // with rw ACL
                p = new Put(TEST_ROW).addColumn(TEST_FAMILY, TEST_Q2, ZERO);
                p.setACL(prepareCellPermissions(usersAndGroups, Action.READ, Action.WRITE));
                t.put(p);
                // no ACL
                p = new Put(TEST_ROW).addColumn(TEST_FAMILY, TEST_Q3, ZERO).addColumn(TEST_FAMILY, TEST_Q4, ZERO);
                t.put(p);
            }
            return null;
        }
    }, USER_OWNER);
    /* ---- Gets ---- */
    AccessTestAction getQ1 = new AccessTestAction() {

        @Override
        public Object run() throws Exception {
            Get get = new Get(TEST_ROW).addColumn(TEST_FAMILY, TEST_Q1);
            try (Connection connection = ConnectionFactory.createConnection(conf);
                Table t = connection.getTable(testTable.getTableName())) {
                return t.get(get).listCells();
            }
        }
    };
    AccessTestAction getQ2 = new AccessTestAction() {

        @Override
        public Object run() throws Exception {
            Get get = new Get(TEST_ROW).addColumn(TEST_FAMILY, TEST_Q2);
            try (Connection connection = ConnectionFactory.createConnection(conf);
                Table t = connection.getTable(testTable.getTableName())) {
                return t.get(get).listCells();
            }
        }
    };
    AccessTestAction getQ3 = new AccessTestAction() {

        @Override
        public Object run() throws Exception {
            Get get = new Get(TEST_ROW).addColumn(TEST_FAMILY, TEST_Q3);
            try (Connection connection = ConnectionFactory.createConnection(conf);
                Table t = connection.getTable(testTable.getTableName())) {
                return t.get(get).listCells();
            }
        }
    };
    AccessTestAction getQ4 = new AccessTestAction() {

        @Override
        public Object run() throws Exception {
            Get get = new Get(TEST_ROW).addColumn(TEST_FAMILY, TEST_Q4);
            try (Connection connection = ConnectionFactory.createConnection(conf);
                Table t = connection.getTable(testTable.getTableName())) {
                return t.get(get).listCells();
            }
        }
    };
    // Confirm special read access set at cell level
    verifyAllowed(getQ1, USER_OTHER, GROUP_USER);
    verifyAllowed(getQ2, USER_OTHER, GROUP_USER);
    // Confirm this access does not extend to other cells
    verifyIfNull(getQ3, USER_OTHER, GROUP_USER);
    verifyIfNull(getQ4, USER_OTHER, GROUP_USER);
    /* ---- Scans ---- */
    // check that a scan over the test data returns the expected number of KVs
    final List<Cell> scanResults = Lists.newArrayList();
    AccessTestAction scanAction = new AccessTestAction() {

        @Override
        public List<Cell> run() throws Exception {
            Scan scan = new Scan();
            scan.withStartRow(TEST_ROW);
            scan.withStopRow(Bytes.add(TEST_ROW, new byte[] { 0 }));
            scan.addFamily(TEST_FAMILY);
            Connection connection = ConnectionFactory.createConnection(conf);
            Table t = connection.getTable(testTable.getTableName());
            try {
                ResultScanner scanner = t.getScanner(scan);
                Result result = null;
                do {
                    result = scanner.next();
                    if (result != null) {
                        scanResults.addAll(result.listCells());
                    }
                } while (result != null);
            } finally {
                t.close();
                connection.close();
            }
            return scanResults;
        }
    };
    // owner will see all values
    scanResults.clear();
    verifyAllowed(scanAction, USER_OWNER);
    assertEquals(4, scanResults.size());
    // other user will see 2 values
    scanResults.clear();
    verifyAllowed(scanAction, USER_OTHER);
    assertEquals(2, scanResults.size());
    scanResults.clear();
    verifyAllowed(scanAction, GROUP_USER);
    assertEquals(2, scanResults.size());
    /* ---- Increments ---- */
    AccessTestAction incrementQ1 = new AccessTestAction() {

        @Override
        public Object run() throws Exception {
            Increment i = new Increment(TEST_ROW).addColumn(TEST_FAMILY, TEST_Q1, 1L);
            try (Connection connection = ConnectionFactory.createConnection(conf);
                Table t = connection.getTable(testTable.getTableName())) {
                t.increment(i);
            }
            return null;
        }
    };
    AccessTestAction incrementQ2 = new AccessTestAction() {

        @Override
        public Object run() throws Exception {
            Increment i = new Increment(TEST_ROW).addColumn(TEST_FAMILY, TEST_Q2, 1L);
            try (Connection connection = ConnectionFactory.createConnection(conf);
                Table t = connection.getTable(testTable.getTableName())) {
                t.increment(i);
            }
            return null;
        }
    };
    AccessTestAction incrementQ2newDenyACL = new AccessTestAction() {

        @Override
        public Object run() throws Exception {
            Increment i = new Increment(TEST_ROW).addColumn(TEST_FAMILY, TEST_Q2, 1L);
            // Tag this increment with an ACL that denies write permissions to USER_OTHER and GROUP
            i.setACL(prepareCellPermissions(usersAndGroups, Action.READ));
            try (Connection connection = ConnectionFactory.createConnection(conf);
                Table t = connection.getTable(testTable.getTableName())) {
                t.increment(i);
            }
            return null;
        }
    };
    AccessTestAction incrementQ3 = new AccessTestAction() {

        @Override
        public Object run() throws Exception {
            Increment i = new Increment(TEST_ROW).addColumn(TEST_FAMILY, TEST_Q3, 1L);
            try (Connection connection = ConnectionFactory.createConnection(conf);
                Table t = connection.getTable(testTable.getTableName())) {
                t.increment(i);
            }
            return null;
        }
    };
    verifyDenied(incrementQ1, USER_OTHER, GROUP_USER);
    verifyDenied(incrementQ3, USER_OTHER, GROUP_USER);
    // We should be able to increment until the permissions are revoked (including the action in
    // which permissions are revoked, the previous ACL will be carried forward)
    verifyAllowed(incrementQ2, USER_OTHER, GROUP_USER);
    verifyAllowed(incrementQ2newDenyACL, USER_OTHER);
    // But not again after we denied ourselves write permission with an ACL
    // update
    verifyDenied(incrementQ2, USER_OTHER, GROUP_USER);
    /* ---- Deletes ---- */
    AccessTestAction deleteFamily = new AccessTestAction() {

        @Override
        public Object run() throws Exception {
            Delete delete = new Delete(TEST_ROW).addFamily(TEST_FAMILY);
            try (Connection connection = ConnectionFactory.createConnection(conf);
                Table t = connection.getTable(testTable.getTableName())) {
                t.delete(delete);
            }
            return null;
        }
    };
    AccessTestAction deleteQ1 = new AccessTestAction() {

        @Override
        public Object run() throws Exception {
            Delete delete = new Delete(TEST_ROW).addColumn(TEST_FAMILY, TEST_Q1);
            try (Connection connection = ConnectionFactory.createConnection(conf);
                Table t = connection.getTable(testTable.getTableName())) {
                t.delete(delete);
            }
            return null;
        }
    };
    verifyDenied(deleteFamily, USER_OTHER, GROUP_USER);
    verifyDenied(deleteQ1, USER_OTHER, GROUP_USER);
    verifyAllowed(deleteQ1, USER_OWNER);
}
Also used : Delete(org.apache.hadoop.hbase.client.Delete) Table(org.apache.hadoop.hbase.client.Table) ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) Connection(org.apache.hadoop.hbase.client.Connection) TableNotFoundException(org.apache.hadoop.hbase.TableNotFoundException) Put(org.apache.hadoop.hbase.client.Put) Result(org.apache.hadoop.hbase.client.Result) Get(org.apache.hadoop.hbase.client.Get) Increment(org.apache.hadoop.hbase.client.Increment) Scan(org.apache.hadoop.hbase.client.Scan) Cell(org.apache.hadoop.hbase.Cell) Test(org.junit.Test)

Aggregations

Increment (org.apache.hadoop.hbase.client.Increment)81 Test (org.junit.Test)42 Put (org.apache.hadoop.hbase.client.Put)31 Append (org.apache.hadoop.hbase.client.Append)25 Result (org.apache.hadoop.hbase.client.Result)25 Delete (org.apache.hadoop.hbase.client.Delete)21 Get (org.apache.hadoop.hbase.client.Get)19 IOException (java.io.IOException)16 TableName (org.apache.hadoop.hbase.TableName)15 Table (org.apache.hadoop.hbase.client.Table)15 ArrayList (java.util.ArrayList)14 Cell (org.apache.hadoop.hbase.Cell)11 DoNotRetryIOException (org.apache.hadoop.hbase.DoNotRetryIOException)11 CheckAndMutateResult (org.apache.hadoop.hbase.client.CheckAndMutateResult)9 Mutation (org.apache.hadoop.hbase.client.Mutation)9 RowMutations (org.apache.hadoop.hbase.client.RowMutations)9 List (java.util.List)8 Map (java.util.Map)8 Scan (org.apache.hadoop.hbase.client.Scan)7 KeyValue (org.apache.hadoop.hbase.KeyValue)5