Search in sources :

Example 1 with PrintFile

use of org.apache.accumulo.shell.Shell.PrintFile in project accumulo by apache.

the class ConfigCommand method execute.

@Override
public int execute(final String fullCommand, final CommandLine cl, final Shell shellState) throws AccumuloException, AccumuloSecurityException, TableNotFoundException, IOException, ClassNotFoundException, NamespaceNotFoundException {
    reader = shellState.getReader();
    final String tableName = cl.getOptionValue(tableOpt.getOpt());
    if (tableName != null && !shellState.getConnector().tableOperations().exists(tableName)) {
        throw new TableNotFoundException(null, tableName, null);
    }
    final String namespace = cl.getOptionValue(namespaceOpt.getOpt());
    if (namespace != null && !shellState.getConnector().namespaceOperations().exists(namespace)) {
        throw new NamespaceNotFoundException(null, namespace, null);
    }
    if (cl.hasOption(deleteOpt.getOpt())) {
        // delete property from table
        String property = cl.getOptionValue(deleteOpt.getOpt());
        if (property.contains("=")) {
            throw new BadArgumentException("Invalid '=' operator in delete operation.", fullCommand, fullCommand.indexOf('='));
        }
        if (tableName != null) {
            if (!Property.isValidTablePropertyKey(property)) {
                Shell.log.warn("Invalid per-table property : " + property + ", still removing from zookeeper if it's there.");
            }
            shellState.getConnector().tableOperations().removeProperty(tableName, property);
            Shell.log.debug("Successfully deleted table configuration option.");
        } else if (namespace != null) {
            if (!Property.isValidTablePropertyKey(property)) {
                Shell.log.warn("Invalid per-table property : " + property + ", still removing from zookeeper if it's there.");
            }
            shellState.getConnector().namespaceOperations().removeProperty(namespace, property);
            Shell.log.debug("Successfully deleted namespace configuration option.");
        } else {
            if (!Property.isValidZooPropertyKey(property)) {
                Shell.log.warn("Invalid per-table property : " + property + ", still removing from zookeeper if it's there.");
            }
            shellState.getConnector().instanceOperations().removeProperty(property);
            Shell.log.debug("Successfully deleted system configuration option");
        }
    } else if (cl.hasOption(setOpt.getOpt())) {
        // set property on table
        String property = cl.getOptionValue(setOpt.getOpt()), value = null;
        if (!property.contains("=")) {
            throw new BadArgumentException("Missing '=' operator in set operation.", fullCommand, fullCommand.indexOf(property));
        }
        final String[] pair = property.split("=", 2);
        property = pair[0];
        value = pair[1];
        if (tableName != null) {
            if (!Property.isValidTablePropertyKey(property)) {
                throw new BadArgumentException("Invalid per-table property.", fullCommand, fullCommand.indexOf(property));
            }
            if (property.equals(Property.TABLE_DEFAULT_SCANTIME_VISIBILITY.getKey())) {
                // validate that it is a valid expression
                new ColumnVisibility(value);
            }
            shellState.getConnector().tableOperations().setProperty(tableName, property, value);
            Shell.log.debug("Successfully set table configuration option.");
        } else if (namespace != null) {
            if (!Property.isValidTablePropertyKey(property)) {
                throw new BadArgumentException("Invalid per-table property.", fullCommand, fullCommand.indexOf(property));
            }
            if (property.equals(Property.TABLE_DEFAULT_SCANTIME_VISIBILITY.getKey())) {
                // validate that it is a valid expression
                new ColumnVisibility(value);
            }
            shellState.getConnector().namespaceOperations().setProperty(namespace, property, value);
            Shell.log.debug("Successfully set table configuration option.");
        } else {
            if (!Property.isValidZooPropertyKey(property)) {
                throw new BadArgumentException("Property cannot be modified in zookeeper", fullCommand, fullCommand.indexOf(property));
            }
            shellState.getConnector().instanceOperations().setProperty(property, value);
            Shell.log.debug("Successfully set system configuration option");
        }
    } else {
        // display properties
        final TreeMap<String, String> systemConfig = new TreeMap<>();
        systemConfig.putAll(shellState.getConnector().instanceOperations().getSystemConfiguration());
        final String outputFile = cl.getOptionValue(outputFileOpt.getOpt());
        final PrintFile printFile = outputFile == null ? null : new PrintFile(outputFile);
        final TreeMap<String, String> siteConfig = new TreeMap<>();
        siteConfig.putAll(shellState.getConnector().instanceOperations().getSiteConfiguration());
        final TreeMap<String, String> defaults = new TreeMap<>();
        for (Entry<String, String> defaultEntry : DefaultConfiguration.getInstance()) {
            defaults.put(defaultEntry.getKey(), defaultEntry.getValue());
        }
        final TreeMap<String, String> namespaceConfig = new TreeMap<>();
        if (tableName != null) {
            String n = Namespaces.getNamespaceName(shellState.getInstance(), Tables.getNamespaceId(shellState.getInstance(), Tables.getTableId(shellState.getInstance(), tableName)));
            for (Entry<String, String> e : shellState.getConnector().namespaceOperations().getProperties(n)) {
                namespaceConfig.put(e.getKey(), e.getValue());
            }
        }
        Iterable<Entry<String, String>> acuconf = shellState.getConnector().instanceOperations().getSystemConfiguration().entrySet();
        if (tableName != null) {
            acuconf = shellState.getConnector().tableOperations().getProperties(tableName);
        } else if (namespace != null) {
            acuconf = shellState.getConnector().namespaceOperations().getProperties(namespace);
        }
        final TreeMap<String, String> sortedConf = new TreeMap<>();
        for (Entry<String, String> propEntry : acuconf) {
            sortedConf.put(propEntry.getKey(), propEntry.getValue());
        }
        for (Entry<String, String> propEntry : acuconf) {
            final String key = propEntry.getKey();
            // specified, or all of them if none specified
            if (cl.hasOption(filterOpt.getOpt()) && !key.contains(cl.getOptionValue(filterOpt.getOpt()))) {
                continue;
            }
            if ((tableName != null || namespace != null) && !Property.isValidTablePropertyKey(key)) {
                continue;
            }
            COL2 = Math.max(COL2, propEntry.getKey().length() + 3);
        }
        final ArrayList<String> output = new ArrayList<>();
        printConfHeader(output);
        for (Entry<String, String> propEntry : sortedConf.entrySet()) {
            final String key = propEntry.getKey();
            // specified, or all of them if none specified
            if (cl.hasOption(filterOpt.getOpt()) && !key.contains(cl.getOptionValue(filterOpt.getOpt()))) {
                continue;
            }
            if ((tableName != null || namespace != null) && !Property.isValidTablePropertyKey(key)) {
                continue;
            }
            String siteVal = siteConfig.get(key);
            String sysVal = systemConfig.get(key);
            String curVal = propEntry.getValue();
            String dfault = defaults.get(key);
            String nspVal = namespaceConfig.get(key);
            boolean printed = false;
            if (dfault != null && key.toLowerCase().contains("password")) {
                siteVal = sysVal = dfault = curVal = curVal.replaceAll(".", "*");
            }
            if (sysVal != null) {
                if (defaults.containsKey(key) && !Property.getPropertyByKey(key).isExperimental()) {
                    printConfLine(output, "default", key, dfault);
                    printed = true;
                }
                if (!defaults.containsKey(key) || !defaults.get(key).equals(siteVal)) {
                    printConfLine(output, "site", printed ? "   @override" : key, siteVal == null ? "" : siteVal);
                    printed = true;
                }
                if (!siteConfig.containsKey(key) || !siteVal.equals(sysVal)) {
                    printConfLine(output, "system", printed ? "   @override" : key, sysVal);
                    printed = true;
                }
            }
            if (nspVal != null) {
                if (!systemConfig.containsKey(key) || !sysVal.equals(nspVal)) {
                    printConfLine(output, "namespace", printed ? "   @override" : key, nspVal);
                    printed = true;
                }
            }
            // show per-table value only if it is different (overridden)
            if (tableName != null && !curVal.equals(nspVal)) {
                printConfLine(output, "table", printed ? "   @override" : key, curVal);
            } else if (namespace != null && !curVal.equals(sysVal)) {
                printConfLine(output, "namespace", printed ? "   @override" : key, curVal);
            }
        }
        printConfFooter(output);
        shellState.printLines(output.iterator(), !cl.hasOption(disablePaginationOpt.getOpt()), printFile);
        if (printFile != null) {
            printFile.close();
        }
    }
    return 0;
}
Also used : BadArgumentException(org.apache.accumulo.core.util.BadArgumentException) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) Entry(java.util.Map.Entry) ArrayList(java.util.ArrayList) PrintFile(org.apache.accumulo.shell.Shell.PrintFile) ColumnVisibility(org.apache.accumulo.core.security.ColumnVisibility) TreeMap(java.util.TreeMap) NamespaceNotFoundException(org.apache.accumulo.core.client.NamespaceNotFoundException)

Example 2 with PrintFile

use of org.apache.accumulo.shell.Shell.PrintFile in project accumulo by apache.

the class ScanCommand method execute.

@Override
public int execute(final String fullCommand, final CommandLine cl, final Shell shellState) throws Exception {
    try (final PrintFile printFile = getOutputFile(cl)) {
        final String tableName = OptUtil.getTableOpt(cl, shellState);
        final Class<? extends Formatter> formatter = getFormatter(cl, tableName, shellState);
        final ScanInterpreter interpeter = getInterpreter(cl, tableName, shellState);
        String classLoaderContext = null;
        if (cl.hasOption(contextOpt.getOpt())) {
            classLoaderContext = cl.getOptionValue(contextOpt.getOpt());
        }
        // handle first argument, if present, the authorizations list to
        // scan with
        final Authorizations auths = getAuths(cl, shellState);
        final Scanner scanner = shellState.getConnector().createScanner(tableName, auths);
        if (null != classLoaderContext) {
            scanner.setClassLoaderContext(classLoaderContext);
        }
        // handle session-specific scan iterators
        addScanIterators(shellState, cl, scanner, tableName);
        // handle remaining optional arguments
        scanner.setRange(getRange(cl, interpeter));
        // handle columns
        fetchColumns(cl, scanner, interpeter);
        // set timeout
        scanner.setTimeout(getTimeout(cl), TimeUnit.MILLISECONDS);
        setupSampling(tableName, cl, shellState, scanner);
        // output the records
        final FormatterConfig config = new FormatterConfig();
        config.setPrintTimestamps(cl.hasOption(timestampOpt.getOpt()));
        if (cl.hasOption(showFewOpt.getOpt())) {
            final String showLength = cl.getOptionValue(showFewOpt.getOpt());
            try {
                final int length = Integer.parseInt(showLength);
                config.setShownLength(length);
            } catch (NumberFormatException nfe) {
                shellState.getReader().println("Arg must be an integer.");
            } catch (IllegalArgumentException iae) {
                shellState.getReader().println("Arg must be greater than one.");
            }
        }
        printRecords(cl, shellState, config, scanner, formatter, printFile);
    }
    return 0;
}
Also used : DefaultScanInterpreter(org.apache.accumulo.core.util.interpret.DefaultScanInterpreter) ScanInterpreter(org.apache.accumulo.core.util.interpret.ScanInterpreter) Scanner(org.apache.accumulo.core.client.Scanner) Authorizations(org.apache.accumulo.core.security.Authorizations) FormatterConfig(org.apache.accumulo.core.util.format.FormatterConfig) PrintFile(org.apache.accumulo.shell.Shell.PrintFile)

Example 3 with PrintFile

use of org.apache.accumulo.shell.Shell.PrintFile in project accumulo by apache.

the class GetSplitsCommand method execute.

@Override
public int execute(final String fullCommand, final CommandLine cl, final Shell shellState) throws IOException, AccumuloException, AccumuloSecurityException, TableNotFoundException {
    final String tableName = OptUtil.getTableOpt(cl, shellState);
    final String outputFile = cl.getOptionValue(outputFileOpt.getOpt());
    final String m = cl.getOptionValue(maxSplitsOpt.getOpt());
    final int maxSplits = m == null ? 0 : Integer.parseInt(m);
    final boolean encode = cl.hasOption(base64Opt.getOpt());
    final boolean verbose = cl.hasOption(verboseOpt.getOpt());
    try (PrintLine p = outputFile == null ? new PrintShell(shellState.getReader()) : new PrintFile(outputFile)) {
        if (!verbose) {
            for (Text row : maxSplits > 0 ? shellState.getConnector().tableOperations().listSplits(tableName, maxSplits) : shellState.getConnector().tableOperations().listSplits(tableName)) {
                p.print(encode(encode, row));
            }
        } else {
            String systemTableToCheck = MetadataTable.NAME.equals(tableName) ? RootTable.NAME : MetadataTable.NAME;
            final Scanner scanner = shellState.getConnector().createScanner(systemTableToCheck, Authorizations.EMPTY);
            TabletsSection.TabletColumnFamily.PREV_ROW_COLUMN.fetch(scanner);
            final Text start = new Text(shellState.getConnector().tableOperations().tableIdMap().get(tableName));
            final Text end = new Text(start);
            end.append(new byte[] { '<' }, 0, 1);
            scanner.setRange(new Range(start, end));
            for (Iterator<Entry<Key, Value>> iterator = scanner.iterator(); iterator.hasNext(); ) {
                final Entry<Key, Value> next = iterator.next();
                if (TabletsSection.TabletColumnFamily.PREV_ROW_COLUMN.hasColumns(next.getKey())) {
                    KeyExtent extent = new KeyExtent(next.getKey().getRow(), next.getValue());
                    final String pr = encode(encode, extent.getPrevEndRow());
                    final String er = encode(encode, extent.getEndRow());
                    final String line = String.format("%-26s (%s, %s%s", obscuredTabletName(extent), pr == null ? "-inf" : pr, er == null ? "+inf" : er, er == null ? ") Default Tablet " : "]");
                    p.print(line);
                }
            }
        }
    }
    return 0;
}
Also used : Scanner(org.apache.accumulo.core.client.Scanner) PrintShell(org.apache.accumulo.shell.Shell.PrintShell) Text(org.apache.hadoop.io.Text) Range(org.apache.accumulo.core.data.Range) KeyExtent(org.apache.accumulo.core.data.impl.KeyExtent) Entry(java.util.Map.Entry) PrintLine(org.apache.accumulo.shell.Shell.PrintLine) Value(org.apache.accumulo.core.data.Value) PrintFile(org.apache.accumulo.shell.Shell.PrintFile) Key(org.apache.accumulo.core.data.Key)

Example 4 with PrintFile

use of org.apache.accumulo.shell.Shell.PrintFile in project accumulo by apache.

the class GrepCommand method execute.

@Override
public int execute(final String fullCommand, final CommandLine cl, final Shell shellState) throws Exception {
    try (final PrintFile printFile = getOutputFile(cl)) {
        final String tableName = OptUtil.getTableOpt(cl, shellState);
        if (cl.getArgList().isEmpty()) {
            throw new MissingArgumentException("No terms specified");
        }
        final Class<? extends Formatter> formatter = getFormatter(cl, tableName, shellState);
        final ScanInterpreter interpeter = getInterpreter(cl, tableName, shellState);
        // handle first argument, if present, the authorizations list to
        // scan with
        int numThreads = 20;
        if (cl.hasOption(numThreadsOpt.getOpt())) {
            numThreads = Integer.parseInt(cl.getOptionValue(numThreadsOpt.getOpt()));
        }
        final Authorizations auths = getAuths(cl, shellState);
        final BatchScanner scanner = shellState.getConnector().createBatchScanner(tableName, auths, numThreads);
        scanner.setRanges(Collections.singletonList(getRange(cl, interpeter)));
        scanner.setTimeout(getTimeout(cl), TimeUnit.MILLISECONDS);
        setupSampling(tableName, cl, shellState, scanner);
        for (int i = 0; i < cl.getArgs().length; i++) {
            setUpIterator(Integer.MAX_VALUE - cl.getArgs().length + i, "grep" + i, cl.getArgs()[i], scanner, cl);
        }
        try {
            // handle columns
            fetchColumns(cl, scanner, interpeter);
            // output the records
            final FormatterConfig config = new FormatterConfig();
            config.setPrintTimestamps(cl.hasOption(timestampOpt.getOpt()));
            printRecords(cl, shellState, config, scanner, formatter, printFile);
        } finally {
            scanner.close();
        }
    }
    return 0;
}
Also used : ScanInterpreter(org.apache.accumulo.core.util.interpret.ScanInterpreter) Authorizations(org.apache.accumulo.core.security.Authorizations) FormatterConfig(org.apache.accumulo.core.util.format.FormatterConfig) MissingArgumentException(org.apache.commons.cli.MissingArgumentException) BatchScanner(org.apache.accumulo.core.client.BatchScanner) PrintFile(org.apache.accumulo.shell.Shell.PrintFile)

Aggregations

PrintFile (org.apache.accumulo.shell.Shell.PrintFile)4 Entry (java.util.Map.Entry)2 Scanner (org.apache.accumulo.core.client.Scanner)2 Authorizations (org.apache.accumulo.core.security.Authorizations)2 FormatterConfig (org.apache.accumulo.core.util.format.FormatterConfig)2 ScanInterpreter (org.apache.accumulo.core.util.interpret.ScanInterpreter)2 ArrayList (java.util.ArrayList)1 TreeMap (java.util.TreeMap)1 BatchScanner (org.apache.accumulo.core.client.BatchScanner)1 NamespaceNotFoundException (org.apache.accumulo.core.client.NamespaceNotFoundException)1 TableNotFoundException (org.apache.accumulo.core.client.TableNotFoundException)1 Key (org.apache.accumulo.core.data.Key)1 Range (org.apache.accumulo.core.data.Range)1 Value (org.apache.accumulo.core.data.Value)1 KeyExtent (org.apache.accumulo.core.data.impl.KeyExtent)1 ColumnVisibility (org.apache.accumulo.core.security.ColumnVisibility)1 BadArgumentException (org.apache.accumulo.core.util.BadArgumentException)1 DefaultScanInterpreter (org.apache.accumulo.core.util.interpret.DefaultScanInterpreter)1 PrintLine (org.apache.accumulo.shell.Shell.PrintLine)1 PrintShell (org.apache.accumulo.shell.Shell.PrintShell)1