Search in sources :

Example 51 with ColumnVisibility

use of org.apache.accumulo.core.security.ColumnVisibility in project accumulo by apache.

the class ConditionalWriterIT method testBatch.

@Test
public void testBatch() throws Exception {
    Connector conn = getConnector();
    String tableName = getUniqueNames(1)[0];
    conn.tableOperations().create(tableName);
    conn.securityOperations().changeUserAuthorizations(getAdminPrincipal(), new Authorizations("A", "B"));
    ColumnVisibility cvab = new ColumnVisibility("A|B");
    ArrayList<ConditionalMutation> mutations = new ArrayList<>();
    ConditionalMutation cm0 = new ConditionalMutation("99006", new Condition("tx", "seq").setVisibility(cvab));
    cm0.put("name", "last", cvab, "doe");
    cm0.put("name", "first", cvab, "john");
    cm0.put("tx", "seq", cvab, "1");
    mutations.add(cm0);
    ConditionalMutation cm1 = new ConditionalMutation("59056", new Condition("tx", "seq").setVisibility(cvab));
    cm1.put("name", "last", cvab, "doe");
    cm1.put("name", "first", cvab, "jane");
    cm1.put("tx", "seq", cvab, "1");
    mutations.add(cm1);
    ConditionalMutation cm2 = new ConditionalMutation("19059", new Condition("tx", "seq").setVisibility(cvab));
    cm2.put("name", "last", cvab, "doe");
    cm2.put("name", "first", cvab, "jack");
    cm2.put("tx", "seq", cvab, "1");
    mutations.add(cm2);
    try (ConditionalWriter cw = conn.createConditionalWriter(tableName, new ConditionalWriterConfig().setAuthorizations(new Authorizations("A")));
        Scanner scanner = conn.createScanner(tableName, new Authorizations("A"))) {
        Iterator<Result> results = cw.write(mutations.iterator());
        int count = 0;
        while (results.hasNext()) {
            Result result = results.next();
            Assert.assertEquals(Status.ACCEPTED, result.getStatus());
            count++;
        }
        Assert.assertEquals(3, count);
        scanner.fetchColumn(new Text("tx"), new Text("seq"));
        for (String row : new String[] { "99006", "59056", "19059" }) {
            scanner.setRange(new Range(row));
            Entry<Key, Value> entry = Iterables.getOnlyElement(scanner);
            Assert.assertEquals("1", entry.getValue().toString());
        }
        TreeSet<Text> splits = new TreeSet<>();
        splits.add(new Text("7"));
        splits.add(new Text("3"));
        conn.tableOperations().addSplits(tableName, splits);
        mutations.clear();
        ConditionalMutation cm3 = new ConditionalMutation("99006", new Condition("tx", "seq").setVisibility(cvab).setValue("1"));
        cm3.put("name", "last", cvab, "Doe");
        cm3.put("tx", "seq", cvab, "2");
        mutations.add(cm3);
        ConditionalMutation cm4 = new ConditionalMutation("59056", new Condition("tx", "seq").setVisibility(cvab));
        cm4.put("name", "last", cvab, "Doe");
        cm4.put("tx", "seq", cvab, "1");
        mutations.add(cm4);
        ConditionalMutation cm5 = new ConditionalMutation("19059", new Condition("tx", "seq").setVisibility(cvab).setValue("2"));
        cm5.put("name", "last", cvab, "Doe");
        cm5.put("tx", "seq", cvab, "3");
        mutations.add(cm5);
        results = cw.write(mutations.iterator());
        int accepted = 0;
        int rejected = 0;
        while (results.hasNext()) {
            Result result = results.next();
            if (new String(result.getMutation().getRow()).equals("99006")) {
                Assert.assertEquals(Status.ACCEPTED, result.getStatus());
                accepted++;
            } else {
                Assert.assertEquals(Status.REJECTED, result.getStatus());
                rejected++;
            }
        }
        Assert.assertEquals("Expected only one accepted conditional mutation", 1, accepted);
        Assert.assertEquals("Expected two rejected conditional mutations", 2, rejected);
        for (String row : new String[] { "59056", "19059" }) {
            scanner.setRange(new Range(row));
            Entry<Key, Value> entry = Iterables.getOnlyElement(scanner);
            Assert.assertEquals("1", entry.getValue().toString());
        }
        scanner.setRange(new Range("99006"));
        Entry<Key, Value> entry = Iterables.getOnlyElement(scanner);
        Assert.assertEquals("2", entry.getValue().toString());
        scanner.clearColumns();
        scanner.fetchColumn(new Text("name"), new Text("last"));
        entry = Iterables.getOnlyElement(scanner);
        Assert.assertEquals("Doe", entry.getValue().toString());
    }
}
Also used : Condition(org.apache.accumulo.core.data.Condition) Connector(org.apache.accumulo.core.client.Connector) IsolatedScanner(org.apache.accumulo.core.client.IsolatedScanner) Scanner(org.apache.accumulo.core.client.Scanner) Authorizations(org.apache.accumulo.core.security.Authorizations) ArrayList(java.util.ArrayList) Text(org.apache.hadoop.io.Text) Range(org.apache.accumulo.core.data.Range) AlphaNumKeyConstraint(org.apache.accumulo.test.constraints.AlphaNumKeyConstraint) Result(org.apache.accumulo.core.client.ConditionalWriter.Result) ConditionalWriter(org.apache.accumulo.core.client.ConditionalWriter) ConditionalMutation(org.apache.accumulo.core.data.ConditionalMutation) TreeSet(java.util.TreeSet) Value(org.apache.accumulo.core.data.Value) ConditionalWriterConfig(org.apache.accumulo.core.client.ConditionalWriterConfig) ColumnVisibility(org.apache.accumulo.core.security.ColumnVisibility) Key(org.apache.accumulo.core.data.Key) Test(org.junit.Test)

Example 52 with ColumnVisibility

use of org.apache.accumulo.core.security.ColumnVisibility in project accumulo-examples by apache.

the class CountIT method setupInstance.

@Before
public void setupInstance() throws Exception {
    tableName = getUniqueNames(1)[0];
    conn = getConnector();
    conn.tableOperations().create(tableName);
    BatchWriter bw = conn.createBatchWriter(tableName, new BatchWriterConfig());
    ColumnVisibility cv = new ColumnVisibility();
    // / has 1 dir
    // /local has 2 dirs 1 file
    // /local/user1 has 2 files
    bw.addMutation(Ingest.buildMutation(cv, "/local", true, false, true, 272, 12345, null));
    bw.addMutation(Ingest.buildMutation(cv, "/local/user1", true, false, true, 272, 12345, null));
    bw.addMutation(Ingest.buildMutation(cv, "/local/user2", true, false, true, 272, 12345, null));
    bw.addMutation(Ingest.buildMutation(cv, "/local/file", false, false, false, 1024, 12345, null));
    bw.addMutation(Ingest.buildMutation(cv, "/local/file", false, false, false, 1024, 23456, null));
    bw.addMutation(Ingest.buildMutation(cv, "/local/user1/file1", false, false, false, 2024, 12345, null));
    bw.addMutation(Ingest.buildMutation(cv, "/local/user1/file2", false, false, false, 1028, 23456, null));
    bw.close();
}
Also used : BatchWriterConfig(org.apache.accumulo.core.client.BatchWriterConfig) BatchWriter(org.apache.accumulo.core.client.BatchWriter) ColumnVisibility(org.apache.accumulo.core.security.ColumnVisibility) Before(org.junit.Before)

Example 53 with ColumnVisibility

use of org.apache.accumulo.core.security.ColumnVisibility in project accumulo-examples by apache.

the class ChunkInputFormatIT method testInfoWithoutChunks.

@Test
public void testInfoWithoutChunks() throws Exception {
    conn.tableOperations().create(tableName);
    BatchWriter bw = conn.createBatchWriter(tableName, new BatchWriterConfig());
    for (Entry<Key, Value> e : baddata) {
        Key k = e.getKey();
        Mutation m = new Mutation(k.getRow());
        m.put(k.getColumnFamily(), k.getColumnQualifier(), new ColumnVisibility(k.getColumnVisibility()), k.getTimestamp(), e.getValue());
        bw.addMutation(m);
    }
    bw.close();
    assertEquals(0, CIFTester.main(tableName, CIFTester.TestBadData.class.getName()));
    assertEquals(1, assertionErrors.get(tableName).size());
}
Also used : 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) Key(org.apache.accumulo.core.data.Key) Test(org.junit.Test)

Example 54 with ColumnVisibility

use of org.apache.accumulo.core.security.ColumnVisibility in project accumulo-examples by apache.

the class ChunkInputStreamIT method testWithAccumulo.

@Test
public void testWithAccumulo() throws AccumuloException, AccumuloSecurityException, TableExistsException, TableNotFoundException, IOException {
    conn.tableOperations().create(tableName);
    BatchWriter bw = conn.createBatchWriter(tableName, new BatchWriterConfig());
    for (Entry<Key, Value> e : data) {
        Key k = e.getKey();
        Mutation m = new Mutation(k.getRow());
        m.put(k.getColumnFamily(), k.getColumnQualifier(), new ColumnVisibility(k.getColumnVisibility()), e.getValue());
        bw.addMutation(m);
    }
    bw.close();
    Scanner scan = conn.createScanner(tableName, AUTHS);
    ChunkInputStream cis = new ChunkInputStream();
    byte[] b = new byte[20];
    int read;
    PeekingIterator<Entry<Key, Value>> pi = new PeekingIterator<>(scan.iterator());
    cis.setSource(pi);
    assertEquals(read = cis.read(b), 8);
    assertEquals(new String(b, 0, read), "asdfjkl;");
    assertEquals(read = cis.read(b), -1);
    cis.setSource(pi);
    assertEquals(read = cis.read(b), 10);
    assertEquals(new String(b, 0, read), "qwertyuiop");
    assertEquals(read = cis.read(b), -1);
    assertEquals(cis.getVisibilities().toString(), "[A&B, B&C, D]");
    cis.close();
    cis.setSource(pi);
    assertEquals(read = cis.read(b), 16);
    assertEquals(new String(b, 0, read), "asdfjkl;asdfjkl;");
    assertEquals(read = cis.read(b), -1);
    assertEquals(cis.getVisibilities().toString(), "[A&B]");
    cis.close();
    cis.setSource(pi);
    assertEquals(read = cis.read(b), -1);
    cis.close();
    cis.setSource(pi);
    assertEquals(read = cis.read(b), 8);
    assertEquals(new String(b, 0, read), "asdfjkl;");
    assertEquals(read = cis.read(b), -1);
    cis.close();
    assertFalse(pi.hasNext());
}
Also used : Scanner(org.apache.accumulo.core.client.Scanner) PeekingIterator(org.apache.accumulo.core.util.PeekingIterator) Entry(java.util.Map.Entry) KeyValue(org.apache.accumulo.core.data.KeyValue) 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) Key(org.apache.accumulo.core.data.Key) Test(org.junit.Test)

Example 55 with ColumnVisibility

use of org.apache.accumulo.core.security.ColumnVisibility in project presto by prestodb.

the class Indexer method index.

/**
 * Index the given mutation, adding mutations to the index and metrics table
 * <p>
 * Like typical use of a BatchWriter, this method does not flush mutations to the underlying index table.
 * For higher throughput the modifications to the metrics table are tracked in memory and added to the metrics table when the indexer is flushed or closed.
 *
 * @param mutation Mutation to index
 */
public void index(Mutation mutation) {
    // Increment the cardinality for the number of rows in the table
    metrics.get(METRICS_TABLE_ROW_COUNT).incrementAndGet();
    // Set the first and last row values of the table based on existing row IDs
    if (firstRow == null || byteArrayComparator.compare(mutation.getRow(), firstRow) < 0) {
        firstRow = mutation.getRow();
    }
    if (lastRow == null || byteArrayComparator.compare(mutation.getRow(), lastRow) > 0) {
        lastRow = mutation.getRow();
    }
    // For each column update in this mutation
    for (ColumnUpdate columnUpdate : mutation.getUpdates()) {
        // Get the column qualifiers we want to index for this column family (if any)
        ByteBuffer family = wrap(columnUpdate.getColumnFamily());
        Collection<ByteBuffer> indexQualifiers = indexColumns.get(family);
        // If we have column qualifiers we want to index for this column family
        if (indexQualifiers != null) {
            // Check if we want to index this particular qualifier
            ByteBuffer qualifier = wrap(columnUpdate.getColumnQualifier());
            if (indexQualifiers.contains(qualifier)) {
                // If so, create a mutation using the following mapping:
                // Row ID = column value
                // Column Family = columnqualifier_columnfamily
                // Column Qualifier = row ID
                // Value = empty
                ByteBuffer indexFamily = getIndexColumnFamily(columnUpdate.getColumnFamily(), columnUpdate.getColumnQualifier());
                Type type = indexColumnTypes.get(family).get(qualifier);
                ColumnVisibility visibility = new ColumnVisibility(columnUpdate.getColumnVisibility());
                // If this is an array type, then index each individual element in the array
                if (Types.isArrayType(type)) {
                    Type elementType = Types.getElementType(type);
                    List<?> elements = serializer.decode(type, columnUpdate.getValue());
                    for (Object element : elements) {
                        addIndexMutation(wrap(serializer.encode(elementType, element)), indexFamily, visibility, mutation.getRow());
                    }
                } else {
                    addIndexMutation(wrap(columnUpdate.getValue()), indexFamily, visibility, mutation.getRow());
                }
            }
        }
    }
}
Also used : Type(com.facebook.presto.common.type.Type) ColumnUpdate(org.apache.accumulo.core.data.ColumnUpdate) ColumnVisibility(org.apache.accumulo.core.security.ColumnVisibility) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ColumnVisibility (org.apache.accumulo.core.security.ColumnVisibility)131 Mutation (org.apache.accumulo.core.data.Mutation)57 Text (org.apache.hadoop.io.Text)57 Value (org.apache.accumulo.core.data.Value)52 Key (org.apache.accumulo.core.data.Key)39 Test (org.junit.Test)37 BatchWriter (org.apache.accumulo.core.client.BatchWriter)28 BatchWriterConfig (org.apache.accumulo.core.client.BatchWriterConfig)20 Authorizations (org.apache.accumulo.core.security.Authorizations)17 ArrayList (java.util.ArrayList)16 Entry (java.util.Map.Entry)16 TableNotFoundException (org.apache.accumulo.core.client.TableNotFoundException)16 Scanner (org.apache.accumulo.core.client.Scanner)14 AccumuloException (org.apache.accumulo.core.client.AccumuloException)12 MutationsRejectedException (org.apache.accumulo.core.client.MutationsRejectedException)12 Configuration (org.apache.hadoop.conf.Configuration)12 Connector (org.apache.accumulo.core.client.Connector)10 IteratorSetting (org.apache.accumulo.core.client.IteratorSetting)10 TMutation (org.apache.accumulo.core.data.thrift.TMutation)10 Element (uk.gov.gchq.gaffer.data.element.Element)9