Example usage for java.util Map isEmpty

List of usage examples for java.util Map isEmpty

Introduction

In this page you can find the example usage for java.util Map isEmpty.

Prototype

boolean isEmpty();

Source Link

Document

Returns true if this map contains no key-value mappings.

Usage

From source file:Main.java

public static void main(String[] a) {
    Map<String, String> map = new HashMap<String, String>();
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");
    map.clear();/*from ww w.  ja  va  2 s.  com*/

    System.out.println(map.isEmpty());
}

From source file:com.conversantmedia.mapreduce.tool.RunJob.java

public static void main(String[] args) throws ToolException, IOException {

    // Get the base packages from the classpath resource
    String[] scanPackages = getBasePackagesToScanForDrivers();

    // Initialize the reflections object
    Reflections reflections = initReflections((Object[]) scanPackages);

    // Search the classpath for Tool and Driver annotations
    Map<String, DriverMeta> idMap = findAllDrivers(reflections);

    if (idMap.isEmpty()) {
        System.out.printf("No drivers found in package(s) [%s]\n", StringUtils.join(scanPackages, ","));
        System.exit(0);// w ww  .j a v a 2  s. c  o m
    }

    // Expects the first argument to be the id of the
    // tool to run. Otherwise list them all:
    if (args.length < 1) {
        outputDriversTable(idMap);
        System.exit(0);
    }

    // Shift off the first (driver id) argument
    String id = args[0];
    args = ArrayUtils.subarray(args, 1, args.length);

    DriverMeta driverMeta = idMap.get(id);
    if (driverMeta == null) {
        if (StringUtils.isNotBlank(id) && !StringUtils.startsWith(id, "-")) { // don't output message if no driver was specified
            // or if the first arg is an argument such as --conf (from runjob script)
            System.out.println("No Tool or Driver class found with id [" + id + "]");
        }
        outputDriversTable(idMap);
        System.exit(1);
    }

    // Finally, run the tool
    runDriver(driverMeta, args);
}

From source file:kindleclippings.quizlet.QuizletSync.java

public static void main(String[] args)
        throws IOException, JSONException, URISyntaxException, InterruptedException, BackingStoreException {

    ProgressMonitor progress = new ProgressMonitor(null, "QuizletSync", "loading Kindle clippings file", 0,
            100);/*w w  w.j av a 2  s .c o  m*/
    progress.setMillisToPopup(0);
    progress.setMillisToDecideToPopup(0);
    progress.setProgress(0);
    try {

        Map<String, List<Clipping>> books = readClippingsFile();

        if (books == null)
            return;

        if (books.isEmpty()) {
            JOptionPane.showMessageDialog(null, "no clippings to be uploaded", "QuizletSync",
                    JOptionPane.OK_OPTION);
            return;
        }
        progress.setNote("checking Quizlet account");
        progress.setProgress(5);

        Preferences prefs = getPrefs();

        QuizletAPI api = new QuizletAPI(prefs.get("access_token", null));

        Collection<TermSet> sets = null;
        try {
            progress.setNote("checking Quizlet library");
            progress.setProgress(10);
            sets = api.getSets(prefs.get("user_id", null));
        } catch (IOException e) {
            if (e.toString().contains("401")) {
                // Not Authorized => Token has been revoked
                clearPrefs();
                prefs = getPrefs();
                api = new QuizletAPI(prefs.get("access_token", null));
                sets = api.getSets(prefs.get("user_id", null));
            } else {
                throw e;
            }
        }

        progress.setProgress(15);
        progress.setMaximum(15 + books.size());
        progress.setNote("uploading new notes");

        Map<String, TermSet> indexedSets = new HashMap<String, TermSet>(sets.size());

        for (TermSet t : sets) {
            indexedSets.put(t.getTitle(), t);
        }

        int pro = 15;
        int createdSets = 0;
        int createdTerms = 0;
        int updatedTerms = 0;
        for (List<Clipping> c : books.values()) {

            String book = c.get(0).getBook();
            progress.setNote(book);
            progress.setProgress(pro++);

            TermSet termSet = indexedSets.get(book);
            if (termSet == null) {
                if (c.size() < 2) {
                    System.err.println("ignored [" + book + "] (need at least two notes)");
                    continue;
                }

                addSet(api, book, c);
                createdSets++;
                createdTerms += c.size();
                continue;
            }
            // compare against existing terms
            for (Clipping cl : c) {
                if (!checkExistingTerm(cl, termSet)) {
                    addTerm(api, termSet, cl);
                    updatedTerms++;
                }
            }
        }
        progress.setProgress(pro++);

        if (createdSets == 0 && updatedTerms == 0) {
            JOptionPane.showMessageDialog(null, "Done.\nNo new data was uploaded", "QuizletSync",
                    JOptionPane.OK_OPTION);
        } else if (createdSets > 0) {
            JOptionPane.showMessageDialog(null,
                    String.format(
                            "Done.\nCreated %d new sets with %d cards, and added %d cards to existing sets",
                            createdSets, createdTerms, updatedTerms),
                    "QuizletSync", JOptionPane.OK_OPTION);
        } else {
            JOptionPane.showMessageDialog(null,
                    String.format("Done.\nAdded %d cards to existing sets", updatedTerms), "QuizletSync",
                    JOptionPane.OK_OPTION);
        }
    } finally {
        progress.close();
    }

    System.exit(0);
}

From source file:mvm.rya.indexing.external.ExternalIndexMain.java

public static void main(String[] args) throws Exception {
    Preconditions.checkArgument(args.length == 6, "java " + ExternalIndexMain.class.getCanonicalName()
            + " sparqlFile cbinstance cbzk cbuser cbpassword rdfTablePrefix.");

    final String sparqlFile = args[0];

    instStr = args[1];/*from   w ww.j  a va 2s. c o  m*/
    zooStr = args[2];
    userStr = args[3];
    passStr = args[4];
    tablePrefix = args[5];

    String queryString = FileUtils.readFileToString(new File(sparqlFile));

    // Look for Extra Indexes
    Instance inst = new ZooKeeperInstance(instStr, zooStr);
    Connector c = inst.getConnector(userStr, passStr.getBytes());

    System.out.println("Searching for Indexes");
    Map<String, String> indexTables = Maps.newLinkedHashMap();
    for (String table : c.tableOperations().list()) {
        if (table.startsWith(tablePrefix + "INDEX_")) {
            Scanner s = c.createScanner(table, new Authorizations());
            s.setRange(Range.exact(new Text("~SPARQL")));
            for (Entry<Key, Value> e : s) {
                indexTables.put(table, e.getValue().toString());
            }
        }
    }

    List<ExternalTupleSet> index = Lists.newArrayList();

    if (indexTables.isEmpty()) {
        System.out.println("No Index found");
    } else {
        for (String table : indexTables.keySet()) {
            String indexSparqlString = indexTables.get(table);
            System.out.println("====================== INDEX FOUND ======================");
            System.out.println(" table : " + table);
            System.out.println(" sparql : ");
            System.out.println(indexSparqlString);

            index.add(new AccumuloIndexSet(indexSparqlString, c, table));
        }
    }

    // Connect to Rya
    Sail s = getRyaSail();
    SailRepository repo = new SailRepository(s);
    repo.initialize();

    // Perform Query

    CountingTupleQueryResultHandler count = new CountingTupleQueryResultHandler();

    SailRepositoryConnection conn;
    if (index.isEmpty()) {
        conn = repo.getConnection();

    } else {
        ExternalProcessor processor = new ExternalProcessor(index);

        Sail processingSail = new ExternalSail(s, processor);
        SailRepository smartSailRepo = new SailRepository(processingSail);
        smartSailRepo.initialize();

        conn = smartSailRepo.getConnection();
    }

    startTime = System.currentTimeMillis();
    lastTime = startTime;
    System.out.println("Query Started");
    conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString).evaluate(count);

    System.out.println("Count of Results found : " + count.i);
    System.out.println("Total query time (s) : " + (System.currentTimeMillis() - startTime) / 1000.);
}

From source file:EVT.java

/**
 * @param args//  w w  w  .  j av  a2  s. co m
 */
public static void main(final String[] args) throws MalformedURLException {
    try {
        URL url = new EVT().getClass().getClassLoader().getResource("general.properties");
        config = new PropertiesConfiguration(url);

    } catch (ConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.exit(1);
    }
    final Map parameters = parseParameters(args);

    if (parameters.containsKey("-v"))
        verboseMode = 1;
    if (parameters.containsKey("-V") || parameters.containsKey("-vv"))
        verboseMode = 2;
    //the from version and the to version must be put in property file
    String supportedVersions = PropertiesUtil.concatenatePropsValues(config, ALFRESCO_VERSION, ",");
    List<String> supportedVersionsList = Arrays.asList(supportedVersions.split(","));
    String alfrescoVersion = (String) parameters.get(ALFRESCO_VERSION);
    boolean supportedVersion = (alfrescoVersion != null) && supportedVersionsList.contains(alfrescoVersion);

    System.out.println(
            "\nAlfresco Environment Validation Tool (for Alfresco Enterprise " + supportedVersions + ")");
    System.out.println("------------------------------------------------------------------");

    if (parameters.isEmpty() || parameters.containsKey("-?") || parameters.containsKey("--help")
            || parameters.containsKey("/?") || !parameters.containsKey(DBValidator.PARAMETER_DATABASE_TYPE)
            || !parameters.containsKey(DBValidator.PARAMETER_DATABASE_HOSTNAME)
            || !parameters.containsKey(DBValidator.PARAMETER_DATABASE_LOGIN)
            || !parameters.containsKey(ALFRESCO_VERSION)
            || !parameters.containsKey(IndexDiskSpeedValidator.PARAMETER_DISK_LOCATION)) {
        System.out.println("");
        System.out.println("usage: evt[.sh|.cmd] [-?|--help] [-v] [-V|-vv]");
        System.out.println("            -a alfrescoversion -t databaseType -h databaseHost [-r databasePort]");
        System.out.println(
                "            [-d databaseName] -l databaseLogin [-p databasePassword] -i indexlocation");
        System.out.println("");
        System.out.println("where:      -?|--help        - display this help");
        System.out.println("            -v               - produce verbose output");
        System.out.println("            -V|-vv           - produce super-verbose output (stack traces)");
        System.out.println(
                "            alfrescoversion  - Version for which the verification is made .  May be one of:");
        System.out.println(
                "                               4.0.0,4.0.1,4.0.2,4.1.1,4.1.2,4.1.3,4.1.4,4.1.5,4.1.6,4.2");
        System.out.println("            databaseType     - the type of database.  May be one of:");
        System.out.println("                               mysql, postgresql, oracle, mssqlserver, db2");
        System.out.println("            databaseHost     - the hostname of the database server");
        System.out.println("            databasePort     - the port the database is listening on (optional -");
        System.out.println("                               defaults to default for the database type)");
        System.out.println("            databaseName     - the name of the Alfresco database (optional -");
        System.out.println("                               defaults to 'alfresco')");
        System.out.println("            databaseLogin    - the login Alfresco will use to connect to the");
        System.out.println("                               database");
        System.out.println("            databasePassword - the password for that user (optional)");
        System.out.println(
                "            indexlocation    - a path to a folder that will contain Alfresco indexes");
        System.out.println("");
        System.out.println("The tool must be run as the OS user that Alfreso will run as.  In particular");
        System.out.println("it will report erroneous results if run as \"root\" (or equivalent on other");
        System.out.println("OSes) if Alfresco is not intended to be run as that user.");
        System.out.println("");
    } else if (!supportedVersion) {
        System.out.println("");
        System.out.println("Version " + alfrescoVersion
                + " is not in the list of the Alfresco versions supported by this tool.");
        System.out.println("Please specify one of the following versions: " + supportedVersions);
    } else {
        final StdoutValidatorCallback callback = new StdoutValidatorCallback();

        (new AllValidators()).validate(parameters, callback);

        System.out.println("\n\n                         **** FINAL GRADE: "
                + TestResult.typeToString(callback.worstResult) + " ****\n");
    }
}

From source file:Main.java

public static void main(String args[]) {
    Map<String, Integer> atomNums = new TreeMap<String, Integer>();

    atomNums.put("A", 1);
    atomNums.put("B", 2);
    atomNums.put("C", 3);
    atomNums.put("D", 4);
    atomNums.put("E", 5);
    atomNums.put("F", 6);

    System.out.println("The map contains these " + atomNums.size() + " entries:");

    Set<Map.Entry<String, Integer>> set = atomNums.entrySet();

    for (Map.Entry<String, Integer> me : set) {
        System.out.print(me.getKey() + ", Atomic Number: ");
        System.out.println(me.getValue());
    }// w  ww. j av  a 2  s . com
    TreeMap<String, Integer> atomNums2 = new TreeMap<String, Integer>();

    atomNums2.put("Q", 30);
    atomNums2.put("W", 82);

    atomNums.putAll(atomNums2);

    set = atomNums.entrySet();

    System.out.println("The map now contains these " + atomNums.size() + " entries:");
    for (Map.Entry<String, Integer> me : set) {
        System.out.print(me.getKey() + ", Atomic Number: ");
        System.out.println(me.getValue());
    }

    if (atomNums.containsKey("A"))
        System.out.println("A has an atomic number of " + atomNums.get("A"));

    if (atomNums.containsValue(82))
        System.out.println("The atomic number 82 is in the map.");
    System.out.println();

    if (atomNums.remove("A") != null)
        System.out.println("A has been removed.\n");
    else
        System.out.println("Entry not found.\n");

    Set<String> keys = atomNums.keySet();
    for (String str : keys)
        System.out.println(str + " ");

    Collection<Integer> vals = atomNums.values();
    for (Integer n : vals)
        System.out.println(n + " ");

    atomNums.clear();
    if (atomNums.isEmpty())
        System.out.println("The map is now empty.");
}

From source file:net.ontopia.topicmaps.cmdlineutils.rdbms.RDBMSIndexTool.java

public static void main(String[] argv) throws Exception {

    // Initialize logging
    CmdlineUtils.initializeLogging();/*from  w w  w .ja va 2  s  .co  m*/

    // Register logging options
    CmdlineOptions options = new CmdlineOptions("RDBMSIndexTool", argv);
    CmdlineUtils.registerLoggingOptions(options);

    // Parse command line options
    try {
        options.parse();
    } catch (CmdlineOptions.OptionsException e) {
        System.err.println("Error: " + e.getMessage());
        System.exit(1);
    }

    // Get command line arguments
    String[] args = options.getArguments();

    if (args.length != 1) {
        usage();
        System.exit(3);
    }

    // load database schema project
    ClassLoader cloader = RDBMSIndexTool.class.getClassLoader();
    InputStream istream = cloader.getResourceAsStream("net/ontopia/topicmaps/impl/rdbms/config/schema.xml");
    Project dbp = DatabaseProjectReader.loadProject(istream);

    // open database connection
    String propfile = args[0];
    ConnectionFactoryIF cf = new DefaultConnectionFactory(PropertyUtils.loadProperties(new File(propfile)),
            true);

    Connection conn = cf.requestConnection();
    try {
        DatabaseMetaData dbm = conn.getMetaData();
        boolean downcase = dbm.storesLowerCaseIdentifiers();

        Map extra_indexes = new TreeMap();
        Map missing_indexes = new TreeMap();

        Iterator tables = dbp.getTables().iterator();
        while (tables.hasNext()) {
            Table table = (Table) tables.next();
            String table_name = (downcase ? table.getName().toLowerCase() : table.getName());
            //! System.out.println("T :"  + table_name);

            // get primary keys from database
            Map pkeys = getPrimaryKeys(table_name, dbm);

            // get indexes from database
            Map indexes = getIndexes(table_name, dbm);

            Map dindexes = new HashMap();
            if (table.getPrimaryKeys() != null) {
                String pkey = table_name + '(' + StringUtils.join(table.getPrimaryKeys(), ',') + ')';
                if (!pkeys.containsKey(pkey))
                    System.out.println("PKM: " + pkey);
            }

            Iterator iter = table.getIndexes().iterator();
            while (iter.hasNext()) {
                Index index = (Index) iter.next();
                String i = table_name + '(' + StringUtils.join(index.getColumns(), ',') + ')';
                String index_name = (downcase ? index.getName().toLowerCase() : index.getName());
                dindexes.put(i, index_name);
            }

            Set extra = new HashSet(indexes.keySet());
            extra.removeAll(dindexes.keySet());
            extra.removeAll(pkeys.keySet());
            if (!extra.isEmpty()) {
                Iterator i = extra.iterator();
                while (i.hasNext()) {
                    Object k = i.next();
                    extra_indexes.put(k, indexes.get(k));
                }
            }

            Set missing = new HashSet(dindexes.keySet());
            missing.addAll(pkeys.keySet());
            missing.removeAll(indexes.keySet());
            if (!missing.isEmpty()) {
                Iterator i = missing.iterator();
                while (i.hasNext()) {
                    Object k = i.next();
                    missing_indexes.put(k, dindexes.get(k));
                }
            }

        }
        if (!extra_indexes.isEmpty())
            System.out.println("/* --- Extra indexes ----------------------------------------- */");
        Iterator eiter = extra_indexes.keySet().iterator();
        while (eiter.hasNext()) {
            Object k = eiter.next();
            System.out.println("drop index " + extra_indexes.get(k) + "; /* " + k + " */");
        }

        if (!missing_indexes.isEmpty())
            System.out.println("/* --- Missing indexes---------------------------------------- */");
        Iterator miter = missing_indexes.keySet().iterator();
        while (miter.hasNext()) {
            Object k = miter.next();
            System.out.println("create index " + missing_indexes.get(k) + " on " + k + ";");
        }

    } finally {
        conn.rollback();
        conn.close();
    }

}

From source file:edu.msu.cme.rdp.graph.utils.ContigMerger.java

public static void main(String[] args) throws IOException {
    final BufferedReader hmmgsResultReader;
    final IndexedSeqReader nuclContigReader;
    final double minBits;
    final int minProtLength;
    final Options options = new Options();
    final PrintStream out;
    final ProfileHMM hmm;
    final FastaWriter protSeqOut;
    final FastaWriter nuclSeqOut;
    final boolean prot;
    final boolean all;
    final String shortSampleName;

    options.addOption("a", "all", false,
            "Generate all combinations for multiple paths, instead of just the best");
    options.addOption("b", "min-bits", true, "Minimum bits score");
    options.addOption("l", "min-length", true, "Minimum length");
    options.addOption("s", "short_samplename", true,
            "short sample name, to be used as part of contig identifiers. This allow analyzing contigs together from different samples in downstream analysis ");
    options.addOption("o", "out", true, "Write output to file instead of stdout");

    try {//w w w. j a va2s  .c  o  m
        CommandLine line = new PosixParser().parse(options, args);

        if (line.hasOption("min-bits")) {
            minBits = Double.valueOf(line.getOptionValue("min-bits"));
        } else {
            minBits = Double.NEGATIVE_INFINITY;
        }

        if (line.hasOption("min-length")) {
            minProtLength = Integer.valueOf(line.getOptionValue("min-length"));
        } else {
            minProtLength = 0;
        }

        if (line.hasOption("short_samplename")) {
            shortSampleName = line.getOptionValue("short_samplename") + "_";
        } else {
            shortSampleName = "";
        }

        if (line.hasOption("out")) {
            out = new PrintStream(line.getOptionValue("out"));
        } else {
            out = System.err;
        }

        all = line.hasOption("all");

        args = line.getArgs();

        if (args.length != 3) {
            throw new Exception("Unexpected number of arguments");
        }

        hmmgsResultReader = new BufferedReader(new FileReader(new File(args[1])));
        nuclContigReader = new IndexedSeqReader(new File(args[2]));
        hmm = HMMER3bParser.readModel(new File(args[0]));

        prot = (hmm.getAlphabet() == SequenceType.Protein);

        if (prot) {
            protSeqOut = new FastaWriter(new File("prot_merged.fasta"));
        } else {
            protSeqOut = null;
        }
        nuclSeqOut = new FastaWriter(new File("nucl_merged.fasta"));

    } catch (Exception e) {
        new HelpFormatter().printHelp("USAGE: ContigMerger [options] <hmm> <hmmgs_file> <nucl_contig>",
                options);
        System.err.println("Error: " + e.getMessage());
        System.exit(1);
        throw new RuntimeException("I hate you javac");
    }

    String line;
    SearchDirection lastDir = SearchDirection.left; //So this has an assumption built in
    //It depends on hmmgs always outputting left fragments, then right
    //We can't just use the kmer to figure out if we've switched to another starting point
    //because we allow multiple starting model pos, so two different starting
    //positions can have the same starting kmer

    Map<String, Sequence> leftContigs = new HashMap();
    Map<String, Sequence> rightContigs = new HashMap();

    int contigsMerged = 0;
    int writtenMerges = 0;
    long startTime = System.currentTimeMillis();
    String kmer = null;
    String geneName = null;

    while ((line = hmmgsResultReader.readLine()) != null) {
        if (line.startsWith("#")) {
            continue;
        }

        String[] lexemes = line.trim().split("\t");
        if (lexemes.length != 12 || lexemes[0].equals("-")) {
            System.err.println("Skipping line: " + line);
            continue;
        }
        //contig_53493   nirk   1500:6:35:16409:3561/1   ADV15048   tcggcgctctacacgttcctgcagcccggg   40   210   70   left   -44.692   184   0
        int index = 0;

        String seqid = lexemes[0];
        geneName = lexemes[1];
        String readid = lexemes[2];
        String refid = lexemes[3];
        kmer = lexemes[4];
        int modelStart = Integer.valueOf(lexemes[5]);

        int nuclLength = Integer.valueOf(lexemes[6]);
        int protLength = Integer.valueOf(lexemes[7]);

        SearchDirection dir = SearchDirection.valueOf(lexemes[8]);
        if (dir != lastDir) {
            if (dir == SearchDirection.left) {
                List<MergedContig> mergedContigs = all
                        ? mergeAllContigs(leftContigs, rightContigs, kmer, geneName, hmm)
                        : mergeContigs(leftContigs, rightContigs, kmer, geneName, hmm);

                contigsMerged++;

                for (MergedContig mc : mergedContigs) {
                    String mergedId = shortSampleName + geneName + "_" + mc.leftContig + "_" + mc.rightContig;
                    out.println(mergedId + "\t" + mc.length + "\t" + mc.score);

                    if (mc.score > minBits && mc.length > minProtLength) {
                        if (prot) {
                            protSeqOut.writeSeq(mergedId, mc.protSeq);
                        }
                        nuclSeqOut.writeSeq(mergedId, mc.nuclSeq);

                        writtenMerges++;
                    }
                }
                leftContigs.clear();
                rightContigs.clear();
            }

            lastDir = dir;
        }

        Sequence seq = nuclContigReader.readSeq(seqid);

        if (dir == SearchDirection.left) {
            leftContigs.put(seqid, seq);
        } else if (dir == SearchDirection.right) {
            rightContigs.put(seqid, seq);
        } else {
            throw new IOException("Cannot handle search direction " + dir);
        }
    }

    if (!leftContigs.isEmpty() || !rightContigs.isEmpty()) {
        List<MergedContig> mergedContigs = all ? mergeAllContigs(leftContigs, rightContigs, kmer, geneName, hmm)
                : mergeContigs(leftContigs, rightContigs, kmer, geneName, hmm);

        for (MergedContig mc : mergedContigs) {
            String mergedId = shortSampleName + mc.gene + "_" + mc.leftContig + "_" + mc.rightContig;
            out.println(mergedId + "\t" + mc.length + "\t" + mc.score);
            contigsMerged++;

            if (mc.score > minBits && mc.length > minProtLength) {
                if (prot) {
                    protSeqOut.writeSeq(mergedId, mc.protSeq);
                }
                nuclSeqOut.writeSeq(mergedId, mc.nuclSeq);
                writtenMerges++;
            }
        }
    }

    out.close();
    if (prot) {
        protSeqOut.close();
    }
    nuclSeqOut.close();

    System.err.println("Read in " + contigsMerged + " contigs, wrote out " + writtenMerges
            + " merged contigs in " + ((double) (System.currentTimeMillis() - startTime) / 1000) + "s");
}

From source file:de.unileipzig.ub.scroller.Main.java

public static void main(String[] args) throws IOException {

    Options options = new Options();
    // add t option
    options.addOption("h", "help", false, "display this help");

    // elasticsearch options
    options.addOption("t", "host", true, "elasticsearch hostname (default: 0.0.0.0)");
    options.addOption("p", "port", true, "transport port (that's NOT the http port, default: 9300)");
    options.addOption("c", "cluster", true, "cluster name (default: elasticsearch_mdma)");

    options.addOption("i", "index", true, "index to use");

    options.addOption("f", "filter", true, "filter(s) - e.g. meta.kind=title");
    options.addOption("j", "junctor", true, "values: and, or (default: and)");
    options.addOption("n", "notice-every", true, "show speed after every N items");

    options.addOption("v", "verbose", false, "be verbose");
    // options.addOption("z", "end-of-message", true, "sentinel to print to stdout, once the regular input finished (default: EOM)");

    CommandLineParser parser = new PosixParser();
    CommandLine cmd = null;/* ww w .j  a  v a2  s. c  om*/

    try {
        cmd = parser.parse(options, args);
    } catch (ParseException ex) {
        logger.error(ex);
        System.exit(1);
    }

    // process options
    if (cmd.hasOption("h")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("scroller", options, true);
        System.exit(0);
    }

    String endOfMessage = "EOM";

    boolean verbose = false;
    if (cmd.hasOption("verbose")) {
        verbose = true;
    }

    if (!cmd.hasOption("i")) {
        System.err.println("error: no index specified");
        System.exit(1);
    }

    long noticeEvery = 10000;
    if (cmd.hasOption("n")) {
        noticeEvery = Long.parseLong(cmd.getOptionValue("n"));
    }

    // ES options
    String[] hosts = new String[] { "0.0.0.0" };
    int port = 9300;
    String clusterName = "elasticsearch_mdma";
    int bulkSize = 3000;

    if (cmd.hasOption("host")) {
        hosts = cmd.getOptionValues("host");
    }
    if (cmd.hasOption("port")) {
        port = Integer.parseInt(cmd.getOptionValue("port"));
    }
    if (cmd.hasOption("cluster")) {
        clusterName = cmd.getOptionValue("cluster");
    }

    // Index
    String indexName = cmd.getOptionValue("index");

    Map<String, String> filterMap = new HashMap<String, String>();
    if (cmd.hasOption("filter")) {
        try {
            filterMap = getMapForKeys(cmd.getOptionValues("filter"));
        } catch (ParseException pe) {
            System.err.println(pe);
            System.exit(1);
        }
    }

    Collection<HashMap> filterList = new ArrayList<HashMap>();
    if (cmd.hasOption("filter")) {
        try {
            filterList = getFilterList(cmd.getOptionValues("filter"));
        } catch (ParseException pe) {
            System.err.println(pe);
            System.exit(1);
        }
    }

    // ES Client
    final Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "elasticsearch_mdma")
            .put("client.transport.ping_timeout", "60s").build();
    final TransportClient client = new TransportClient(settings);
    for (String host : hosts) {
        client.addTransportAddress(new InetSocketTransportAddress(host, port));
    }

    // build the query
    String junctor = "and";
    if (cmd.hasOption("j")) {
        junctor = cmd.getOptionValue("j");
    }

    //        ArrayList<TermFilterBuilder> filters = new ArrayList<TermFilterBuilder>();
    //        if (filterMap.size() > 0) {
    //            for (Map.Entry<String, String> entry : filterMap.entrySet()) {
    //                filters.add(new TermFilterBuilder(entry.getKey(), entry.getValue()));
    //            }
    //        }

    ArrayList<TermFilterBuilder> filters = new ArrayList<TermFilterBuilder>();
    if (filterList.size() > 0) {
        for (HashMap map : filterList) {
            for (Object obj : map.entrySet()) {
                Map.Entry entry = (Map.Entry) obj;
                filters.add(new TermFilterBuilder(entry.getKey().toString(), entry.getValue().toString()));
            }
        }
    }

    FilterBuilder fb = null;
    if (junctor.equals("and")) {
        AndFilterBuilder afb = new AndFilterBuilder();
        for (TermFilterBuilder tfb : filters) {
            afb.add(tfb);
        }
        fb = afb;
    }

    if (junctor.equals("or")) {
        OrFilterBuilder ofb = new OrFilterBuilder();
        for (TermFilterBuilder tfb : filters) {
            ofb.add(tfb);
        }
        fb = ofb;
    }

    //        TermFilterBuilder tfb0 = new TermFilterBuilder("meta.kind", "title");
    //        TermFilterBuilder tfb1 = new TermFilterBuilder("meta.timestamp", "201112081240");
    //
    //        AndFilterBuilder afb0 = new AndFilterBuilder(tfb0, tfb1);

    QueryBuilder qb0 = null;
    if (filterMap.isEmpty()) {
        qb0 = matchAllQuery();
    } else {
        qb0 = filteredQuery(matchAllQuery(), fb);
    }

    // sorting
    // FieldSortBuilder sortBuilder = new FieldSortBuilder("meta.timestamp");
    // sortBuilder.order(SortOrder.DESC);

    // FilteredQueryBuilder fqb0 = filteredQuery(matchAllQuery(), tfb0);

    final CountResponse countResponse = client.prepareCount(indexName).setQuery(qb0).execute().actionGet();
    final long total = countResponse.getCount();

    SearchResponse scrollResp = client.prepareSearch(indexName).setSearchType(SearchType.SCAN)
            .setScroll(new TimeValue(60000)).setQuery(qb0)
            // .addSort(sortBuilder) // sort has no effect on scroll type (see: https://github.com/CPAN-API/cpan-api/issues/172)
            .setSize(1000) //1000 hits per shard will be returned for each scroll
            .execute().actionGet();

    //Scroll until no hits are returned

    System.err.println("[Scroller] query: " + qb0.toString());
    System.err.println("[Scroller] took: " + scrollResp.getTookInMillis() + "ms");
    System.err.println("[Scroller] docs found: " + total);

    long counter = 0;
    long start = System.currentTimeMillis();

    while (true) {
        scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(600000))
                .execute().actionGet();

        if (scrollResp.getHits().hits().length == 0) {
            break;
        }

        for (SearchHit hit : scrollResp.getHits()) {

            System.out.println(hit.sourceAsString());
            counter += 1;
            if (counter % noticeEvery == 0) {
                final double elapsed = (System.currentTimeMillis() - start) / 1000;
                final double speed = counter / elapsed;
                final long eta = (long) ((elapsed / counter) * (total - counter) * 1000);
                System.err.println(
                        counter + "/" + total + " records recvd @ speed " + String.format("%1$,.1f", speed)
                                + " r/s eta: " + DurationFormatUtils.formatDurationWords(eta, false, false));
            }
        }
    }
    System.out.close();
    // System.out.println(endOfMessage);
}

From source file:Main.java

public static boolean isEmptyMap(Map map) {
    return null == map || map.isEmpty();
}