use of org.apache.accumulo.core.client.TableExistsException in project accumulo by apache.
the class CreateTableCommand method execute.
@Override
public int execute(final String fullCommand, final CommandLine cl, final Shell shellState) throws AccumuloException, AccumuloSecurityException, TableExistsException, TableNotFoundException, IOException, ClassNotFoundException {
final String testTableName = cl.getArgs()[0];
final HashMap<String, String> props = new HashMap<>();
NewTableConfiguration ntc = new NewTableConfiguration();
if (!testTableName.matches(Tables.VALID_NAME_REGEX)) {
shellState.getReader().println("Only letters, numbers and underscores are allowed for use in table names.");
throw new IllegalArgumentException();
}
final String tableName = cl.getArgs()[0];
if (shellState.getConnector().tableOperations().exists(tableName)) {
throw new TableExistsException(null, tableName, null);
}
final SortedSet<Text> partitions = new TreeSet<>();
final boolean decode = cl.hasOption(base64Opt.getOpt());
if (cl.hasOption(createTableOptSplit.getOpt())) {
partitions.addAll(ShellUtil.scanFile(cl.getOptionValue(createTableOptSplit.getOpt()), decode));
} else if (cl.hasOption(createTableOptCopySplits.getOpt())) {
final String oldTable = cl.getOptionValue(createTableOptCopySplits.getOpt());
if (!shellState.getConnector().tableOperations().exists(oldTable)) {
throw new TableNotFoundException(null, oldTable, null);
}
partitions.addAll(shellState.getConnector().tableOperations().listSplits(oldTable));
}
if (cl.hasOption(createTableOptCopyConfig.getOpt())) {
final String oldTable = cl.getOptionValue(createTableOptCopyConfig.getOpt());
if (!shellState.getConnector().tableOperations().exists(oldTable)) {
throw new TableNotFoundException(null, oldTable, null);
}
}
TimeType timeType = TimeType.MILLIS;
if (cl.hasOption(createTableOptTimeLogical.getOpt())) {
timeType = TimeType.LOGICAL;
}
if (cl.hasOption(createTableOptInitProp.getOpt())) {
String[] keyVals = StringUtils.split(cl.getOptionValue(createTableOptInitProp.getOpt()), ',');
for (String keyVal : keyVals) {
String[] sa = StringUtils.split(keyVal, '=');
props.put(sa[0], sa[1]);
}
}
// Set iterator if supplied
if (cl.hasOption(createTableOptIteratorProps.getOpt())) {
ntc = attachIteratorToNewTable(cl, shellState, ntc);
}
// Set up locality groups, if supplied
if (cl.hasOption(createTableOptLocalityProps.getOpt())) {
ntc = setLocalityForNewTable(cl, ntc);
}
// create table
shellState.getConnector().tableOperations().create(tableName, ntc.setTimeType(timeType).setProperties(props));
if (partitions.size() > 0) {
shellState.getConnector().tableOperations().addSplits(tableName, partitions);
}
// switch shell to new table context
shellState.setTableName(tableName);
if (cl.hasOption(createTableNoDefaultIters.getOpt())) {
for (String key : IteratorUtil.generateInitialTableProperties(true).keySet()) {
shellState.getConnector().tableOperations().removeProperty(tableName, key);
}
}
// Copy options if flag was set
if (cl.hasOption(createTableOptCopyConfig.getOpt())) {
if (shellState.getConnector().tableOperations().exists(tableName)) {
final Iterable<Entry<String, String>> configuration = shellState.getConnector().tableOperations().getProperties(cl.getOptionValue(createTableOptCopyConfig.getOpt()));
for (Entry<String, String> entry : configuration) {
if (Property.isValidTablePropertyKey(entry.getKey())) {
shellState.getConnector().tableOperations().setProperty(tableName, entry.getKey(), entry.getValue());
}
}
}
}
if (cl.hasOption(createTableOptEVC.getOpt())) {
try {
shellState.getConnector().tableOperations().addConstraint(tableName, VisibilityConstraint.class.getName());
} catch (AccumuloException e) {
Shell.log.warn(e.getMessage() + " while setting visibility constraint, but table was created");
}
}
// Load custom formatter if set
if (cl.hasOption(createTableOptFormatter.getOpt())) {
final String formatterClass = cl.getOptionValue(createTableOptFormatter.getOpt());
shellState.getConnector().tableOperations().setProperty(tableName, Property.TABLE_FORMATTER_CLASS.toString(), formatterClass);
}
return 0;
}
use of org.apache.accumulo.core.client.TableExistsException in project accumulo by apache.
the class ProxyServer method createTable.
@Override
public void createTable(ByteBuffer login, String tableName, boolean versioningIter, org.apache.accumulo.proxy.thrift.TimeType type) throws org.apache.accumulo.proxy.thrift.AccumuloException, org.apache.accumulo.proxy.thrift.AccumuloSecurityException, org.apache.accumulo.proxy.thrift.TableExistsException, TException {
try {
if (type == null)
type = org.apache.accumulo.proxy.thrift.TimeType.MILLIS;
NewTableConfiguration tConfig = new NewTableConfiguration().setTimeType(TimeType.valueOf(type.toString()));
if (!versioningIter)
tConfig = tConfig.withoutDefaultIterators();
getConnector(login).tableOperations().create(tableName, tConfig);
} catch (TableExistsException e) {
throw new org.apache.accumulo.proxy.thrift.TableExistsException(e.toString());
} catch (Exception e) {
handleException(e);
}
}
use of org.apache.accumulo.core.client.TableExistsException in project accumulo-examples by apache.
the class InsertWithBatchWriter method main.
public static void main(String[] args) throws AccumuloException, AccumuloSecurityException, TableNotFoundException {
Connector connector = Connector.builder().usingProperties("conf/accumulo-client.properties").build();
try {
connector.tableOperations().create("hellotable");
} catch (TableExistsException e) {
// ignore
}
try (BatchWriter bw = connector.createBatchWriter("hellotable")) {
log.trace("writing ...");
for (int i = 0; i < 10000; i++) {
Mutation m = new Mutation(String.format("row_%d", i));
for (int j = 0; j < 5; j++) {
m.put("colfam", String.format("colqual_%d", j), new Value((String.format("value_%d_%d", i, j)).getBytes()));
}
bw.addMutation(m);
if (i % 100 == 0) {
log.trace(String.valueOf(i));
}
}
}
}
use of org.apache.accumulo.core.client.TableExistsException in project accumulo by apache.
the class TableOperationsImpl method importDirectory.
@Override
public void importDirectory(String tableName, String dir, String failureDir, boolean setTime) throws IOException, AccumuloSecurityException, TableNotFoundException, AccumuloException {
checkArgument(tableName != null, "tableName is null");
checkArgument(dir != null, "dir is null");
checkArgument(failureDir != null, "failureDir is null");
// check for table existance
Tables.getTableId(context.getInstance(), tableName);
Path dirPath = checkPath(dir, "Bulk", "");
Path failPath = checkPath(failureDir, "Bulk", "failure");
List<ByteBuffer> args = Arrays.asList(ByteBuffer.wrap(tableName.getBytes(UTF_8)), ByteBuffer.wrap(dirPath.toString().getBytes(UTF_8)), ByteBuffer.wrap(failPath.toString().getBytes(UTF_8)), ByteBuffer.wrap((setTime + "").getBytes(UTF_8)));
Map<String, String> opts = new HashMap<>();
try {
doTableFateOperation(tableName, TableNotFoundException.class, FateOperation.TABLE_BULK_IMPORT, args, opts);
} catch (TableExistsException e) {
// should not happen
throw new AssertionError(e);
}
}
use of org.apache.accumulo.core.client.TableExistsException in project accumulo by apache.
the class TableOperationsImpl method deleteRows.
@Override
public void deleteRows(String tableName, Text start, Text end) throws AccumuloException, AccumuloSecurityException, TableNotFoundException {
checkArgument(tableName != null, "tableName is null");
ByteBuffer EMPTY = ByteBuffer.allocate(0);
List<ByteBuffer> args = Arrays.asList(ByteBuffer.wrap(tableName.getBytes(UTF_8)), start == null ? EMPTY : TextUtil.getByteBuffer(start), end == null ? EMPTY : TextUtil.getByteBuffer(end));
Map<String, String> opts = new HashMap<>();
try {
doTableFateOperation(tableName, TableNotFoundException.class, FateOperation.TABLE_DELETE_RANGE, args, opts);
} catch (TableExistsException e) {
// should not happen
throw new AssertionError(e);
}
}
Aggregations