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;
}
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);
}
}
Aggregations