Search in sources :

Example 1 with SecurityErrorCode

use of org.apache.accumulo.core.client.security.SecurityErrorCode in project accumulo by apache.

the class InsertCommand method execute.

@Override
public int execute(final String fullCommand, final CommandLine cl, final Shell shellState) throws AccumuloException, AccumuloSecurityException, TableNotFoundException, IOException, ConstraintViolationException {
    shellState.checkTableState();
    final Mutation m = new Mutation(new Text(cl.getArgs()[0].getBytes(Shell.CHARSET)));
    final Text colf = new Text(cl.getArgs()[1].getBytes(Shell.CHARSET));
    final Text colq = new Text(cl.getArgs()[2].getBytes(Shell.CHARSET));
    final Value val = new Value(cl.getArgs()[3].getBytes(Shell.CHARSET));
    if (cl.hasOption(insertOptAuths.getOpt())) {
        final ColumnVisibility le = new ColumnVisibility(cl.getOptionValue(insertOptAuths.getOpt()));
        Shell.log.debug("Authorization label will be set to: " + le.toString());
        if (cl.hasOption(timestampOpt.getOpt()))
            m.put(colf, colq, le, Long.parseLong(cl.getOptionValue(timestampOpt.getOpt())), val);
        else
            m.put(colf, colq, le, val);
    } else if (cl.hasOption(timestampOpt.getOpt()))
        m.put(colf, colq, Long.parseLong(cl.getOptionValue(timestampOpt.getOpt())), val);
    else
        m.put(colf, colq, val);
    final BatchWriterConfig cfg = new BatchWriterConfig().setMaxMemory(Math.max(m.estimatedMemoryUsed(), 1024)).setMaxWriteThreads(1).setTimeout(getTimeout(cl), TimeUnit.MILLISECONDS);
    if (cl.hasOption(durabilityOption.getOpt())) {
        String userDurability = cl.getOptionValue(durabilityOption.getOpt());
        switch(userDurability) {
            case "sync":
                cfg.setDurability(Durability.SYNC);
                break;
            case "flush":
                cfg.setDurability(Durability.FLUSH);
                break;
            case "none":
                cfg.setDurability(Durability.NONE);
                break;
            case "log":
                cfg.setDurability(Durability.NONE);
                break;
            default:
                throw new IllegalArgumentException("Unknown durability: " + userDurability);
        }
    }
    final BatchWriter bw = shellState.getConnector().createBatchWriter(shellState.getTableName(), cfg);
    bw.addMutation(m);
    try {
        bw.close();
    } catch (MutationsRejectedException e) {
        final ArrayList<String> lines = new ArrayList<>();
        if (!e.getSecurityErrorCodes().isEmpty()) {
            lines.add("\tAuthorization Failures:");
        }
        for (Entry<TabletId, Set<SecurityErrorCode>> entry : e.getSecurityErrorCodes().entrySet()) {
            lines.add("\t\t" + entry);
        }
        if (!e.getConstraintViolationSummaries().isEmpty()) {
            lines.add("\tConstraint Failures:");
        }
        for (ConstraintViolationSummary cvs : e.getConstraintViolationSummaries()) {
            lines.add("\t\t" + cvs.toString());
        }
        if (lines.size() == 0 || e.getUnknownExceptions() > 0) {
            // must always print something
            lines.add(" " + e.getClass().getName() + " : " + e.getMessage());
            if (e.getCause() != null)
                lines.add("   Caused by : " + e.getCause().getClass().getName() + " : " + e.getCause().getMessage());
        }
        shellState.printLines(lines.iterator(), false);
        return 1;
    }
    return 0;
}
Also used : SecurityErrorCode(org.apache.accumulo.core.client.security.SecurityErrorCode) ArrayList(java.util.ArrayList) Text(org.apache.hadoop.io.Text) Entry(java.util.Map.Entry) Value(org.apache.accumulo.core.data.Value) BatchWriterConfig(org.apache.accumulo.core.client.BatchWriterConfig) ConstraintViolationSummary(org.apache.accumulo.core.data.ConstraintViolationSummary) Mutation(org.apache.accumulo.core.data.Mutation) ColumnVisibility(org.apache.accumulo.core.security.ColumnVisibility) BatchWriter(org.apache.accumulo.core.client.BatchWriter) MutationsRejectedException(org.apache.accumulo.core.client.MutationsRejectedException)

Example 2 with SecurityErrorCode

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

the class RandomBatchWriter method main.

/**
 * Writes a specified number of entries to Accumulo using a {@link BatchWriter}.
 */
public static void main(String[] args) throws AccumuloException, AccumuloSecurityException, TableNotFoundException {
    Opts opts = new Opts();
    BatchWriterOpts bwOpts = new BatchWriterOpts();
    opts.parseArgs(RandomBatchWriter.class.getName(), args, bwOpts);
    if ((opts.max - opts.min) < 1L * opts.num) {
        // right-side multiplied by 1L to convert to long in a way that doesn't trigger FindBugs
        System.err.println(String.format("You must specify a min and a max that allow for at least num possible values. " + "For example, you requested %d rows, but a min of %d and a max of %d (exclusive), which only allows for %d rows.", opts.num, opts.min, opts.max, (opts.max - opts.min)));
        System.exit(1);
    }
    Random r;
    if (opts.seed == null)
        r = new Random();
    else {
        r = new Random(opts.seed);
    }
    Connector connector = opts.getConnector();
    BatchWriter bw = connector.createBatchWriter(opts.getTableName(), bwOpts.getBatchWriterConfig());
    // reuse the ColumnVisibility object to improve performance
    ColumnVisibility cv = opts.visiblity;
    // Generate num unique row ids in the given range
    HashSet<Long> rowids = new HashSet<>(opts.num);
    while (rowids.size() < opts.num) {
        rowids.add((abs(r.nextLong()) % (opts.max - opts.min)) + opts.min);
    }
    for (long rowid : rowids) {
        Mutation m = createMutation(rowid, opts.size, cv);
        bw.addMutation(m);
    }
    try {
        bw.close();
    } catch (MutationsRejectedException e) {
        if (e.getSecurityErrorCodes().size() > 0) {
            HashMap<String, Set<SecurityErrorCode>> tables = new HashMap<>();
            for (Entry<TabletId, Set<SecurityErrorCode>> ke : e.getSecurityErrorCodes().entrySet()) {
                String tableId = ke.getKey().getTableId().toString();
                Set<SecurityErrorCode> secCodes = tables.get(tableId);
                if (secCodes == null) {
                    secCodes = new HashSet<>();
                    tables.put(tableId, secCodes);
                }
                secCodes.addAll(ke.getValue());
            }
            System.err.println("ERROR : Not authorized to write to tables : " + tables);
        }
        if (e.getConstraintViolationSummaries().size() > 0) {
            System.err.println("ERROR : Constraint violations occurred : " + e.getConstraintViolationSummaries());
        }
        System.exit(1);
    }
}
Also used : Connector(org.apache.accumulo.core.client.Connector) Set(java.util.Set) HashSet(java.util.HashSet) BatchWriterOpts(org.apache.accumulo.examples.cli.BatchWriterOpts) HashMap(java.util.HashMap) SecurityErrorCode(org.apache.accumulo.core.client.security.SecurityErrorCode) Entry(java.util.Map.Entry) Random(java.util.Random) BatchWriterOpts(org.apache.accumulo.examples.cli.BatchWriterOpts) BatchWriter(org.apache.accumulo.core.client.BatchWriter) ColumnVisibility(org.apache.accumulo.core.security.ColumnVisibility) Mutation(org.apache.accumulo.core.data.Mutation) HashSet(java.util.HashSet) MutationsRejectedException(org.apache.accumulo.core.client.MutationsRejectedException)

Aggregations

Entry (java.util.Map.Entry)2 BatchWriter (org.apache.accumulo.core.client.BatchWriter)2 MutationsRejectedException (org.apache.accumulo.core.client.MutationsRejectedException)2 SecurityErrorCode (org.apache.accumulo.core.client.security.SecurityErrorCode)2 Mutation (org.apache.accumulo.core.data.Mutation)2 ColumnVisibility (org.apache.accumulo.core.security.ColumnVisibility)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Random (java.util.Random)1 Set (java.util.Set)1 BatchWriterConfig (org.apache.accumulo.core.client.BatchWriterConfig)1 Connector (org.apache.accumulo.core.client.Connector)1 ConstraintViolationSummary (org.apache.accumulo.core.data.ConstraintViolationSummary)1 Value (org.apache.accumulo.core.data.Value)1 BatchWriterOpts (org.apache.accumulo.examples.cli.BatchWriterOpts)1 Text (org.apache.hadoop.io.Text)1