Search in sources :

Example 1 with ShellCommandException

use of org.apache.accumulo.shell.ShellCommandException in project accumulo by apache.

the class SetIterCommand method setNamespaceProperties.

protected void setNamespaceProperties(final CommandLine cl, final Shell shellState, final int priority, final Map<String, String> options, final String classname, final String name) throws AccumuloException, AccumuloSecurityException, ShellCommandException, NamespaceNotFoundException {
    // remove empty values
    final String namespace = OptUtil.getNamespaceOpt(cl, shellState);
    if (!shellState.getConnector().namespaceOperations().testClassLoad(namespace, classname, SortedKeyValueIterator.class.getName())) {
        throw new ShellCommandException(ErrorCode.INITIALIZATION_FAILURE, "Servers are unable to load " + classname + " as type " + SortedKeyValueIterator.class.getName());
    }
    final String aggregatorClass = options.get("aggregatorClass");
    @SuppressWarnings("deprecation") String deprecatedAggregatorClassName = org.apache.accumulo.core.iterators.aggregation.Aggregator.class.getName();
    if (aggregatorClass != null && !shellState.getConnector().namespaceOperations().testClassLoad(namespace, aggregatorClass, deprecatedAggregatorClassName)) {
        throw new ShellCommandException(ErrorCode.INITIALIZATION_FAILURE, "Servers are unable to load " + aggregatorClass + " as type " + deprecatedAggregatorClassName);
    }
    for (Iterator<Entry<String, String>> i = options.entrySet().iterator(); i.hasNext(); ) {
        final Entry<String, String> entry = i.next();
        if (entry.getValue() == null || entry.getValue().isEmpty()) {
            i.remove();
        }
    }
    final EnumSet<IteratorScope> scopes = EnumSet.noneOf(IteratorScope.class);
    if (cl.hasOption(allScopeOpt.getOpt()) || cl.hasOption(mincScopeOpt.getOpt())) {
        scopes.add(IteratorScope.minc);
    }
    if (cl.hasOption(allScopeOpt.getOpt()) || cl.hasOption(majcScopeOpt.getOpt())) {
        scopes.add(IteratorScope.majc);
    }
    if (cl.hasOption(allScopeOpt.getOpt()) || cl.hasOption(scanScopeOpt.getOpt())) {
        scopes.add(IteratorScope.scan);
    }
    if (scopes.isEmpty()) {
        throw new IllegalArgumentException("You must select at least one scope to configure");
    }
    final IteratorSetting setting = new IteratorSetting(priority, name, classname, options);
    shellState.getConnector().namespaceOperations().attachIterator(namespace, setting, scopes);
}
Also used : ShellCommandException(org.apache.accumulo.shell.ShellCommandException) Entry(java.util.Map.Entry) IteratorSetting(org.apache.accumulo.core.client.IteratorSetting) IteratorScope(org.apache.accumulo.core.iterators.IteratorUtil.IteratorScope)

Example 2 with ShellCommandException

use of org.apache.accumulo.shell.ShellCommandException in project accumulo by apache.

the class ConstraintCommand method execute.

@Override
public int execute(final String fullCommand, final CommandLine cl, final Shell shellState) throws Exception {
    final String tableName;
    final String namespace;
    if (cl.hasOption(namespaceOpt.getOpt())) {
        namespace = cl.getOptionValue(namespaceOpt.getOpt());
    } else {
        namespace = null;
    }
    if (cl.hasOption(OptUtil.tableOpt().getOpt()) || !shellState.getTableName().isEmpty()) {
        tableName = OptUtil.getTableOpt(cl, shellState);
    } else {
        tableName = null;
    }
    int i;
    switch(OptUtil.getAldOpt(cl)) {
        case ADD:
            for (String constraint : cl.getArgs()) {
                if (namespace != null) {
                    if (!shellState.getConnector().namespaceOperations().testClassLoad(namespace, constraint, Constraint.class.getName())) {
                        throw new ShellCommandException(ErrorCode.INITIALIZATION_FAILURE, "Servers are unable to load " + constraint + " as type " + Constraint.class.getName());
                    }
                    i = shellState.getConnector().namespaceOperations().addConstraint(namespace, constraint);
                    shellState.getReader().println("Added constraint " + constraint + " to namespace " + namespace + " with number " + i);
                } else if (tableName != null && !tableName.isEmpty()) {
                    if (!shellState.getConnector().tableOperations().testClassLoad(tableName, constraint, Constraint.class.getName())) {
                        throw new ShellCommandException(ErrorCode.INITIALIZATION_FAILURE, "Servers are unable to load " + constraint + " as type " + Constraint.class.getName());
                    }
                    i = shellState.getConnector().tableOperations().addConstraint(tableName, constraint);
                    shellState.getReader().println("Added constraint " + constraint + " to table " + tableName + " with number " + i);
                } else {
                    throw new IllegalArgumentException("Please specify either a table or a namespace");
                }
            }
            break;
        case DELETE:
            for (String constraint : cl.getArgs()) {
                i = Integer.parseInt(constraint);
                if (namespace != null) {
                    shellState.getConnector().namespaceOperations().removeConstraint(namespace, i);
                    shellState.getReader().println("Removed constraint " + i + " from namespace " + namespace);
                } else if (tableName != null) {
                    shellState.getConnector().tableOperations().removeConstraint(tableName, i);
                    shellState.getReader().println("Removed constraint " + i + " from table " + tableName);
                } else {
                    throw new IllegalArgumentException("Please specify either a table or a namespace");
                }
            }
            break;
        case LIST:
            if (namespace != null) {
                for (Entry<String, Integer> property : shellState.getConnector().namespaceOperations().listConstraints(namespace).entrySet()) {
                    shellState.getReader().println(property.toString());
                }
            } else if (tableName != null) {
                for (Entry<String, Integer> property : shellState.getConnector().tableOperations().listConstraints(tableName).entrySet()) {
                    shellState.getReader().println(property.toString());
                }
            } else {
                throw new IllegalArgumentException("Please specify either a table or a namespace");
            }
    }
    return 0;
}
Also used : Entry(java.util.Map.Entry) Constraint(org.apache.accumulo.core.constraints.Constraint) ShellCommandException(org.apache.accumulo.shell.ShellCommandException) Constraint(org.apache.accumulo.core.constraints.Constraint)

Example 3 with ShellCommandException

use of org.apache.accumulo.shell.ShellCommandException in project accumulo by apache.

the class SetIterCommand method setUpOptions.

private static String setUpOptions(ClassLoader classloader, final ConsoleReader reader, final String className, final Map<String, String> options) throws IOException, ShellCommandException {
    String input;
    @SuppressWarnings("rawtypes") SortedKeyValueIterator untypedInstance;
    @SuppressWarnings("rawtypes") Class<? extends SortedKeyValueIterator> clazz;
    try {
        clazz = classloader.loadClass(className).asSubclass(SortedKeyValueIterator.class);
        untypedInstance = clazz.newInstance();
    } catch (ClassNotFoundException e) {
        StringBuilder msg = new StringBuilder("Unable to load ").append(className);
        if (className.indexOf('.') < 0) {
            msg.append("; did you use a fully qualified package name?");
        } else {
            msg.append("; class not found.");
        }
        throw new ShellCommandException(ErrorCode.INITIALIZATION_FAILURE, msg.toString());
    } catch (InstantiationException | IllegalAccessException e) {
        throw new IllegalArgumentException(e.getMessage());
    } catch (ClassCastException e) {
        StringBuilder msg = new StringBuilder(50);
        msg.append(className).append(" loaded successfully but does not implement SortedKeyValueIterator.");
        msg.append(" This class cannot be used with this command.");
        throw new ShellCommandException(ErrorCode.INITIALIZATION_FAILURE, msg.toString());
    }
    @SuppressWarnings("unchecked") SortedKeyValueIterator<Key, Value> skvi = untypedInstance;
    OptionDescriber iterOptions = null;
    if (OptionDescriber.class.isAssignableFrom(skvi.getClass())) {
        iterOptions = (OptionDescriber) skvi;
    }
    String iteratorName;
    if (null != iterOptions) {
        final IteratorOptions itopts = iterOptions.describeOptions();
        iteratorName = itopts.getName();
        if (iteratorName == null) {
            throw new IllegalArgumentException(className + " described its default distinguishing name as null");
        }
        String shortClassName = className;
        if (className.contains(".")) {
            shortClassName = className.substring(className.lastIndexOf('.') + 1);
        }
        final Map<String, String> localOptions = new HashMap<>();
        do {
            // clean up the overall options that caused things to fail
            for (String key : localOptions.keySet()) {
                options.remove(key);
            }
            localOptions.clear();
            reader.println(itopts.getDescription());
            String prompt;
            if (itopts.getNamedOptions() != null) {
                for (Entry<String, String> e : itopts.getNamedOptions().entrySet()) {
                    prompt = Shell.repeat("-", 10) + "> set " + shortClassName + " parameter " + e.getKey() + ", " + e.getValue() + ": ";
                    reader.flush();
                    input = reader.readLine(prompt);
                    if (input == null) {
                        reader.println();
                        throw new IOException("Input stream closed");
                    }
                    // Places all Parameters and Values into the LocalOptions, even if the value is "".
                    // This allows us to check for "" values when setting the iterators and allows us to remove
                    // the parameter and value from the table property.
                    localOptions.put(e.getKey(), input);
                }
            }
            if (itopts.getUnnamedOptionDescriptions() != null) {
                for (String desc : itopts.getUnnamedOptionDescriptions()) {
                    reader.println(Shell.repeat("-", 10) + "> entering options: " + desc);
                    input = "start";
                    prompt = Shell.repeat("-", 10) + "> set " + shortClassName + " option (<name> <value>, hit enter to skip): ";
                    while (true) {
                        reader.flush();
                        input = reader.readLine(prompt);
                        if (input == null) {
                            reader.println();
                            throw new IOException("Input stream closed");
                        } else {
                            input = new String(input);
                        }
                        if (input.length() == 0)
                            break;
                        String[] sa = input.split(" ", 2);
                        localOptions.put(sa[0], sa[1]);
                    }
                }
            }
            options.putAll(localOptions);
            if (!iterOptions.validateOptions(options))
                reader.println("invalid options for " + clazz.getName());
        } while (!iterOptions.validateOptions(options));
    } else {
        reader.flush();
        reader.println("The iterator class does not implement OptionDescriber. Consider this for better iterator configuration using this setiter command.");
        iteratorName = reader.readLine("Name for iterator (enter to skip): ");
        if (null == iteratorName) {
            reader.println();
            throw new IOException("Input stream closed");
        } else if (StringUtils.isWhitespace(iteratorName)) {
            // Treat whitespace or empty string as no name provided
            iteratorName = null;
        }
        reader.flush();
        reader.println("Optional, configure name-value options for iterator:");
        String prompt = Shell.repeat("-", 10) + "> set option (<name> <value>, hit enter to skip): ";
        final HashMap<String, String> localOptions = new HashMap<>();
        while (true) {
            reader.flush();
            input = reader.readLine(prompt);
            if (input == null) {
                reader.println();
                throw new IOException("Input stream closed");
            } else if (StringUtils.isWhitespace(input)) {
                break;
            }
            String[] sa = input.split(" ", 2);
            localOptions.put(sa[0], sa[1]);
        }
        options.putAll(localOptions);
    }
    return iteratorName;
}
Also used : HashMap(java.util.HashMap) ShellCommandException(org.apache.accumulo.shell.ShellCommandException) SortedKeyValueIterator(org.apache.accumulo.core.iterators.SortedKeyValueIterator) IOException(java.io.IOException) IteratorOptions(org.apache.accumulo.core.iterators.OptionDescriber.IteratorOptions) Value(org.apache.accumulo.core.data.Value) OptionDescriber(org.apache.accumulo.core.iterators.OptionDescriber) Key(org.apache.accumulo.core.data.Key)

Example 4 with ShellCommandException

use of org.apache.accumulo.shell.ShellCommandException in project accumulo by apache.

the class SetIterCommand method setTableProperties.

protected void setTableProperties(final CommandLine cl, final Shell shellState, final int priority, final Map<String, String> options, final String classname, final String name) throws AccumuloException, AccumuloSecurityException, ShellCommandException, TableNotFoundException {
    // remove empty values
    final String tableName = OptUtil.getTableOpt(cl, shellState);
    ScanCommand.ensureTserversCanLoadIterator(shellState, tableName, classname);
    final String aggregatorClass = options.get("aggregatorClass");
    @SuppressWarnings("deprecation") String deprecatedAggregatorClassName = org.apache.accumulo.core.iterators.aggregation.Aggregator.class.getName();
    if (aggregatorClass != null && !shellState.getConnector().tableOperations().testClassLoad(tableName, aggregatorClass, deprecatedAggregatorClassName)) {
        throw new ShellCommandException(ErrorCode.INITIALIZATION_FAILURE, "Servers are unable to load " + aggregatorClass + " as type " + deprecatedAggregatorClassName);
    }
    for (Iterator<Entry<String, String>> i = options.entrySet().iterator(); i.hasNext(); ) {
        final Entry<String, String> entry = i.next();
        if (entry.getValue() == null || entry.getValue().isEmpty()) {
            i.remove();
        }
    }
    final EnumSet<IteratorScope> scopes = EnumSet.noneOf(IteratorScope.class);
    if (cl.hasOption(allScopeOpt.getOpt()) || cl.hasOption(mincScopeOpt.getOpt())) {
        scopes.add(IteratorScope.minc);
    }
    if (cl.hasOption(allScopeOpt.getOpt()) || cl.hasOption(majcScopeOpt.getOpt())) {
        scopes.add(IteratorScope.majc);
    }
    if (cl.hasOption(allScopeOpt.getOpt()) || cl.hasOption(scanScopeOpt.getOpt())) {
        scopes.add(IteratorScope.scan);
    }
    if (scopes.isEmpty()) {
        throw new IllegalArgumentException("You must select at least one scope to configure");
    }
    final IteratorSetting setting = new IteratorSetting(priority, name, classname, options);
    shellState.getConnector().tableOperations().attachIterator(tableName, setting, scopes);
}
Also used : ShellCommandException(org.apache.accumulo.shell.ShellCommandException) Entry(java.util.Map.Entry) IteratorSetting(org.apache.accumulo.core.client.IteratorSetting) IteratorScope(org.apache.accumulo.core.iterators.IteratorUtil.IteratorScope)

Aggregations

ShellCommandException (org.apache.accumulo.shell.ShellCommandException)4 Entry (java.util.Map.Entry)3 IteratorSetting (org.apache.accumulo.core.client.IteratorSetting)2 IteratorScope (org.apache.accumulo.core.iterators.IteratorUtil.IteratorScope)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Constraint (org.apache.accumulo.core.constraints.Constraint)1 Key (org.apache.accumulo.core.data.Key)1 Value (org.apache.accumulo.core.data.Value)1 OptionDescriber (org.apache.accumulo.core.iterators.OptionDescriber)1 IteratorOptions (org.apache.accumulo.core.iterators.OptionDescriber.IteratorOptions)1 SortedKeyValueIterator (org.apache.accumulo.core.iterators.SortedKeyValueIterator)1