Search in sources :

Example 76 with Mutation

use of org.apache.accumulo.core.data.Mutation in project accumulo by apache.

the class MockNamespacesTest method testNamespaceIterators.

/**
 * This tests adding iterators to a namespace, listing them, and removing them
 */
@Test
public void testNamespaceIterators() throws Exception {
    String namespace = "iterator";
    String tableName = namespace + ".table";
    String iter = "thing";
    conn.namespaceOperations().create(namespace);
    conn.tableOperations().create(tableName);
    IteratorSetting setting = new IteratorSetting(250, iter, SimpleFilter.class.getName());
    HashSet<IteratorScope> scope = new HashSet<>();
    scope.add(IteratorScope.scan);
    conn.namespaceOperations().attachIterator(namespace, setting, EnumSet.copyOf(scope));
    BatchWriter bw = conn.createBatchWriter(tableName, new BatchWriterConfig());
    Mutation m = new Mutation("r");
    m.put("a", "b", new Value("abcde".getBytes(UTF_8)));
    bw.addMutation(m);
    bw.flush();
    Scanner s = conn.createScanner(tableName, Authorizations.EMPTY);
    System.out.println(s.iterator().next());
    // do scanners work correctly in mock?
    // assertTrue(!s.iterator().hasNext());
    assertTrue(conn.namespaceOperations().listIterators(namespace).containsKey(iter));
    conn.namespaceOperations().removeIterator(namespace, iter, EnumSet.copyOf(scope));
}
Also used : Scanner(org.apache.accumulo.core.client.Scanner) IteratorSetting(org.apache.accumulo.core.client.IteratorSetting) Value(org.apache.accumulo.core.data.Value) BatchWriterConfig(org.apache.accumulo.core.client.BatchWriterConfig) IteratorScope(org.apache.accumulo.core.iterators.IteratorUtil.IteratorScope) BatchWriter(org.apache.accumulo.core.client.BatchWriter) Mutation(org.apache.accumulo.core.data.Mutation) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 77 with Mutation

use of org.apache.accumulo.core.data.Mutation in project accumulo by apache.

the class MockTableOperationsTest method testDeleteRows.

@Test
public void testDeleteRows() throws Exception {
    TableOperations to = conn.tableOperations();
    to.create("test");
    BatchWriter bw = conn.createBatchWriter("test", new BatchWriterConfig());
    for (int r = 0; r < 20; r++) {
        Mutation m = new Mutation("" + r);
        for (int c = 0; c < 5; c++) {
            m.put(new Text("cf"), new Text("" + c), new Value(("" + c).getBytes()));
        }
        bw.addMutation(m);
    }
    bw.flush();
    to.deleteRows("test", new Text("1"), new Text("2"));
    Scanner s = conn.createScanner("test", Authorizations.EMPTY);
    int oneCnt = 0;
    for (Entry<Key, Value> entry : s) {
        char rowStart = entry.getKey().getRow().toString().charAt(0);
        Assert.assertTrue(rowStart != '2');
        oneCnt += rowStart == '1' ? 1 : 0;
    }
    Assert.assertEquals(5, oneCnt);
}
Also used : BatchScanner(org.apache.accumulo.core.client.BatchScanner) Scanner(org.apache.accumulo.core.client.Scanner) TableOperations(org.apache.accumulo.core.client.admin.TableOperations) Value(org.apache.accumulo.core.data.Value) BatchWriterConfig(org.apache.accumulo.core.client.BatchWriterConfig) Text(org.apache.hadoop.io.Text) BatchWriter(org.apache.accumulo.core.client.BatchWriter) Mutation(org.apache.accumulo.core.data.Mutation) Key(org.apache.accumulo.core.data.Key) Test(org.junit.Test)

Example 78 with Mutation

use of org.apache.accumulo.core.data.Mutation in project accumulo by apache.

the class TestBatchScanner821 method test.

@Test
public void test() throws Exception {
    MockInstance inst = new MockInstance();
    Connector conn = inst.getConnector("root", new PasswordToken(""));
    conn.tableOperations().create("test");
    BatchWriter bw = conn.createBatchWriter("test", new BatchWriterConfig());
    for (String row : "A,B,C,D".split(",")) {
        Mutation m = new Mutation(row);
        m.put("cf", "cq", "");
        bw.addMutation(m);
    }
    bw.flush();
    BatchScanner bs = conn.createBatchScanner("test", Authorizations.EMPTY, 1);
    IteratorSetting cfg = new IteratorSetting(100, TransformIterator.class);
    bs.addScanIterator(cfg);
    bs.setRanges(Collections.singletonList(new Range("A", "Z")));
    StringBuilder sb = new StringBuilder();
    String comma = "";
    for (Entry<Key, Value> entry : bs) {
        sb.append(comma);
        sb.append(entry.getKey().getRow());
        comma = ",";
    }
    assertEquals("a,b,c,d", sb.toString());
}
Also used : Connector(org.apache.accumulo.core.client.Connector) BatchScanner(org.apache.accumulo.core.client.BatchScanner) Range(org.apache.accumulo.core.data.Range) PasswordToken(org.apache.accumulo.core.client.security.tokens.PasswordToken) IteratorSetting(org.apache.accumulo.core.client.IteratorSetting) Value(org.apache.accumulo.core.data.Value) BatchWriterConfig(org.apache.accumulo.core.client.BatchWriterConfig) BatchWriter(org.apache.accumulo.core.client.BatchWriter) Mutation(org.apache.accumulo.core.data.Mutation) Key(org.apache.accumulo.core.data.Key) Test(org.junit.Test)

Example 79 with Mutation

use of org.apache.accumulo.core.data.Mutation in project accumulo by apache.

the class DefaultKeySizeConstraintTest method testConstraint.

@Test
public void testConstraint() {
    // pass constraints
    Mutation m = new Mutation("rowId");
    m.put("colf", "colq", new Value(new byte[] {}));
    assertEquals(Collections.emptyList(), constraint.check(null, m));
    // test with row id > 1mb
    m = new Mutation(oversized);
    m.put("colf", "colq", new Value(new byte[] {}));
    assertEquals(Collections.singletonList(DefaultKeySizeConstraint.MAX__KEY_SIZE_EXCEEDED_VIOLATION), constraint.check(null, m));
    // test with colf > 1mb
    m = new Mutation("rowid");
    m.put(new Text(oversized), new Text("colq"), new Value(new byte[] {}));
    assertEquals(Collections.singletonList(DefaultKeySizeConstraint.MAX__KEY_SIZE_EXCEEDED_VIOLATION), constraint.check(null, m));
    // test with colf > 1mb
    m = new Mutation("rowid");
    m.put(new Text(oversized), new Text("colq"), new Value(new byte[] {}));
    assertEquals(Collections.singletonList(DefaultKeySizeConstraint.MAX__KEY_SIZE_EXCEEDED_VIOLATION), constraint.check(null, m));
    // test sum of smaller sizes violates 1mb constraint
    m = new Mutation(large);
    m.put(new Text(large), new Text(large), new Value(new byte[] {}));
    assertEquals(Collections.singletonList(DefaultKeySizeConstraint.MAX__KEY_SIZE_EXCEEDED_VIOLATION), constraint.check(null, m));
}
Also used : Value(org.apache.accumulo.core.data.Value) Text(org.apache.hadoop.io.Text) Mutation(org.apache.accumulo.core.data.Mutation) Test(org.junit.Test)

Example 80 with Mutation

use of org.apache.accumulo.core.data.Mutation in project accumulo by apache.

the class KerberosIT method testUserPrivilegesForTable.

@Test
public void testUserPrivilegesForTable() throws Exception {
    String user1 = testName.getMethodName();
    final File user1Keytab = new File(kdc.getKeytabDir(), user1 + ".keytab");
    if (user1Keytab.exists() && !user1Keytab.delete()) {
        log.warn("Unable to delete {}", user1Keytab);
    }
    // Create some new users -- cannot contain realm
    kdc.createPrincipal(user1Keytab, user1);
    final String qualifiedUser1 = kdc.qualifyUser(user1);
    // Log in as user1
    UserGroupInformation ugi = UserGroupInformation.loginUserFromKeytabAndReturnUGI(qualifiedUser1, user1Keytab.getAbsolutePath());
    log.info("Logged in as {}", user1);
    ugi.doAs(new PrivilegedExceptionAction<Void>() {

        @Override
        public Void run() throws Exception {
            // Indirectly creates this user when we use it
            Connector conn = mac.getConnector(qualifiedUser1, new KerberosToken());
            log.info("Created connector as {}", qualifiedUser1);
            // The new user should have no system permissions
            for (SystemPermission perm : SystemPermission.values()) {
                assertFalse(conn.securityOperations().hasSystemPermission(qualifiedUser1, perm));
            }
            return null;
        }
    });
    final String table = testName.getMethodName() + "_user_table";
    final String viz = "viz";
    ugi = UserGroupInformation.loginUserFromKeytabAndReturnUGI(rootUser.getPrincipal(), rootUser.getKeytab().getAbsolutePath());
    ugi.doAs(new PrivilegedExceptionAction<Void>() {

        @Override
        public Void run() throws Exception {
            Connector conn = mac.getConnector(rootUser.getPrincipal(), new KerberosToken());
            conn.tableOperations().create(table);
            // Give our unprivileged user permission on the table we made for them
            conn.securityOperations().grantTablePermission(qualifiedUser1, table, TablePermission.READ);
            conn.securityOperations().grantTablePermission(qualifiedUser1, table, TablePermission.WRITE);
            conn.securityOperations().grantTablePermission(qualifiedUser1, table, TablePermission.ALTER_TABLE);
            conn.securityOperations().grantTablePermission(qualifiedUser1, table, TablePermission.DROP_TABLE);
            conn.securityOperations().changeUserAuthorizations(qualifiedUser1, new Authorizations(viz));
            return null;
        }
    });
    // Switch back to the original user
    ugi = UserGroupInformation.loginUserFromKeytabAndReturnUGI(qualifiedUser1, user1Keytab.getAbsolutePath());
    ugi.doAs(new PrivilegedExceptionAction<Void>() {

        @Override
        public Void run() throws Exception {
            Connector conn = mac.getConnector(qualifiedUser1, new KerberosToken());
            // Make sure we can actually use the table we made
            // Write data
            final long ts = 1000l;
            BatchWriter bw = conn.createBatchWriter(table, new BatchWriterConfig());
            Mutation m = new Mutation("a");
            m.put("b", "c", new ColumnVisibility(viz.getBytes()), ts, "d");
            bw.addMutation(m);
            bw.close();
            // Compact
            conn.tableOperations().compact(table, new CompactionConfig().setWait(true).setFlush(true));
            // Alter
            conn.tableOperations().setProperty(table, Property.TABLE_BLOOM_ENABLED.getKey(), "true");
            // Read (and proper authorizations)
            try (Scanner s = conn.createScanner(table, new Authorizations(viz))) {
                Iterator<Entry<Key, Value>> iter = s.iterator();
                assertTrue("No results from iterator", iter.hasNext());
                Entry<Key, Value> entry = iter.next();
                assertEquals(new Key("a", "b", "c", viz, ts), entry.getKey());
                assertEquals(new Value("d".getBytes()), entry.getValue());
                assertFalse("Had more results from iterator", iter.hasNext());
                return null;
            }
        }
    });
}
Also used : Connector(org.apache.accumulo.core.client.Connector) BatchScanner(org.apache.accumulo.core.client.BatchScanner) Scanner(org.apache.accumulo.core.client.Scanner) Authorizations(org.apache.accumulo.core.security.Authorizations) KerberosToken(org.apache.accumulo.core.client.security.tokens.KerberosToken) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) TableExistsException(org.apache.accumulo.core.client.TableExistsException) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) AccumuloException(org.apache.accumulo.core.client.AccumuloException) SystemPermission(org.apache.accumulo.core.security.SystemPermission) Entry(java.util.Map.Entry) CompactionConfig(org.apache.accumulo.core.client.admin.CompactionConfig) Iterator(java.util.Iterator) Value(org.apache.accumulo.core.data.Value) BatchWriterConfig(org.apache.accumulo.core.client.BatchWriterConfig) BatchWriter(org.apache.accumulo.core.client.BatchWriter) Mutation(org.apache.accumulo.core.data.Mutation) ColumnVisibility(org.apache.accumulo.core.security.ColumnVisibility) File(java.io.File) Key(org.apache.accumulo.core.data.Key) UserGroupInformation(org.apache.hadoop.security.UserGroupInformation) Test(org.junit.Test)

Aggregations

Mutation (org.apache.accumulo.core.data.Mutation)601 BatchWriter (org.apache.accumulo.core.client.BatchWriter)358 Value (org.apache.accumulo.core.data.Value)341 Test (org.junit.Test)311 Text (org.apache.hadoop.io.Text)303 BatchWriterConfig (org.apache.accumulo.core.client.BatchWriterConfig)223 Key (org.apache.accumulo.core.data.Key)197 Scanner (org.apache.accumulo.core.client.Scanner)161 Connector (org.apache.accumulo.core.client.Connector)150 IteratorSetting (org.apache.accumulo.core.client.IteratorSetting)77 Authorizations (org.apache.accumulo.core.security.Authorizations)70 Range (org.apache.accumulo.core.data.Range)61 ArrayList (java.util.ArrayList)60 ColumnVisibility (org.apache.accumulo.core.security.ColumnVisibility)59 Entry (java.util.Map.Entry)57 MutationsRejectedException (org.apache.accumulo.core.client.MutationsRejectedException)55 Map (java.util.Map)53 HashMap (java.util.HashMap)44 TableNotFoundException (org.apache.accumulo.core.client.TableNotFoundException)43 BatchScanner (org.apache.accumulo.core.client.BatchScanner)41