List of usage examples for org.apache.commons.cli HelpFormatter printHelp
public void printHelp(String cmdLineSyntax, Options options)
options
with the specified command line syntax. From source file:SDRecord.java
public static void main(String[] args) { boolean recordToInf = false; long recordTo = 0, txsize = 0, wr = 0, max = 0; int sourcePort = 0, destPort = 0; String val; OutputStream writer = null;//from w w w . ja va 2 s.com InetAddress rhost = null, lhost = null; DatagramSocket socket = null; //Default values int buffSize = 1500; try { lhost = InetAddress.getByName("0.0.0.0"); } catch (UnknownHostException e1) { System.err.println("ERROR!: Host not reconized"); System.exit(3); } recordToInf = true; sourcePort = 7355; Options options = new Options(); options.addOption("m", true, "Minutes to record, default is no limit"); options.addOption("l", true, "Bind to a specific local address, default is 0.0.0.0"); options.addOption("p", true, "Local port to use, default is 7355"); options.addOption("r", true, "Remote address where to send data"); options.addOption("d", true, "Remote port, to use with -r option"); options.addOption("f", true, "Output file where to save the recording"); options.addOption("s", true, "Stop recording when reaching specified MBs"); options.addOption("h", false, "Help"); CommandLineParser parser = new DefaultParser(); CommandLine cmd = null; try { cmd = parser.parse(options, args); } catch (ParseException e1) { System.err.println("ERROR!: Error while parsing the command line"); System.exit(1); } if (cmd.hasOption("m")) { val = cmd.getOptionValue("m"); try { if (Long.parseLong(val) < 0) { System.err.println("ERROR!: -m argument value cannot be negative"); System.exit(3); } recordTo = System.currentTimeMillis() + (Long.parseLong(val) * 60000); recordToInf = false; } catch (NumberFormatException e) { System.err.println("ERROR!: -m argument not an integer"); System.exit(3); } } if (cmd.hasOption("l")) { val = cmd.getOptionValue("l"); try { lhost = InetAddress.getByName(val); } catch (UnknownHostException e) { System.err.println("ERROR!: Host not reconized"); System.exit(3); } } if (cmd.hasOption("p")) { val = cmd.getOptionValue("p"); try { sourcePort = Integer.parseInt(val); } catch (NumberFormatException e) { System.err.println("ERROR!: -p argument not an integer"); System.exit(3); } } if (cmd.hasOption("r")) { val = cmd.getOptionValue("r"); try { rhost = InetAddress.getByName(val); } catch (UnknownHostException e) { System.err.println("ERROR!: Host not reconized"); System.exit(3); } } if (cmd.hasOption("d")) { val = cmd.getOptionValue("d"); try { destPort = Integer.parseInt(val); } catch (NumberFormatException e) { System.err.println("-ERROR!: -d argument not an integer"); System.exit(3); } } if (cmd.hasOption("f")) { val = cmd.getOptionValue("f"); try { writer = new FileOutputStream(val); } catch (FileNotFoundException e) { System.err.println("ERROR!: File not found"); System.exit(3); } } if (cmd.hasOption("s")) { val = cmd.getOptionValue("s"); try { max = (long) (Double.parseDouble(val) * 1000000); } catch (NumberFormatException e) { System.err.println("ERROR!: -s argument not valid"); System.exit(3); } if (Double.parseDouble(val) < 0) { System.err.println("ERROR!: -s argument value cannot be negative"); System.exit(3); } } if (cmd.hasOption("h")) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("SDRecord", options); System.exit(0); } try { socket = new DatagramSocket(sourcePort, lhost); //socket options socket.setReuseAddress(true); } catch (SocketException e) { e.printStackTrace(); System.exit(3); } byte[] buffer = new byte[buffSize]; DatagramPacket packet = new DatagramPacket(buffer, buffer.length); System.err.println("Listening " + lhost.toString() + " on port " + sourcePort); while (recordToInf == true || System.currentTimeMillis() <= recordTo) { //Stop recording when reaching max bytes if (max != 0 && txsize >= max) break; packet.setData(buffer); try { socket.receive(packet); } catch (IOException e) { e.printStackTrace(); System.exit(4); } //Ignoring packets with no data if (basicFilter(packet) == null) continue; if (writer == null && rhost == null) wr = recordToStdout(packet); if (writer != null) wr = recordToFile(packet, writer); if (rhost != null) wr = recordToSocket(packet, socket, rhost, destPort); txsize += wr; System.err .print("\r" + formatSize(txsize) + " transferred" + "\033[K" + "\t Press Ctrl+c to terminate"); } //closing socket and exit System.err.print("\r" + formatSize(txsize) + " transferred" + "\033[K"); socket.close(); System.out.println(); System.exit(0); }
From source file:ivory.core.tokenize.Tokenizer.java
@SuppressWarnings("static-access") public static void main(String[] args) { Options options = new Options(); options.addOption(OptionBuilder.withArgName("full path to model file or directory").hasArg() .withDescription("model file").create("model")); options.addOption(OptionBuilder.withArgName("full path to input file").hasArg() .withDescription("input file").isRequired().create("input")); options.addOption(OptionBuilder.withArgName("full path to output file").hasArg() .withDescription("output file").isRequired().create("output")); options.addOption(OptionBuilder.withArgName("en | zh | de | fr | ar | tr | es").hasArg() .withDescription("2-character language code").isRequired().create("lang")); options.addOption(OptionBuilder.withArgName("path to stopwords list").hasArg() .withDescription("one stopword per line").create("stopword")); options.addOption(OptionBuilder.withArgName("path to stemmed stopwords list").hasArg() .withDescription("one stemmed stopword per line").create("stemmed_stopword")); options.addOption(OptionBuilder.withArgName("true|false").hasArg().withDescription("turn on/off stemming") .create("stem")); options.addOption(OptionBuilder.withDescription("Hadoop option to load external jars") .withArgName("jar packages").hasArg().create("libjars")); CommandLine cmdline;/*from w w w .j a v a 2 s .c om*/ CommandLineParser parser = new GnuParser(); try { String stopwordList = null, stemmedStopwordList = null, modelFile = null; boolean isStem = true; cmdline = parser.parse(options, args); if (cmdline.hasOption("stopword")) { stopwordList = cmdline.getOptionValue("stopword"); } if (cmdline.hasOption("stemmed_stopword")) { stemmedStopwordList = cmdline.getOptionValue("stemmed_stopword"); } if (cmdline.hasOption("stem")) { isStem = Boolean.parseBoolean(cmdline.getOptionValue("stem")); } if (cmdline.hasOption("model")) { modelFile = cmdline.getOptionValue("model"); } ivory.core.tokenize.Tokenizer tokenizer = TokenizerFactory.createTokenizer( cmdline.getOptionValue("lang"), modelFile, isStem, stopwordList, stemmedStopwordList, null); BufferedWriter out = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(cmdline.getOptionValue("output")), "UTF8")); BufferedReader in = new BufferedReader( new InputStreamReader(new FileInputStream(cmdline.getOptionValue("input")), "UTF8")); String line = null; while ((line = in.readLine()) != null) { String[] tokens = tokenizer.processContent(line); String s = ""; for (String token : tokens) { s += token + " "; } out.write(s.trim() + "\n"); } in.close(); out.close(); } catch (Exception exp) { System.out.println(exp); HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("Tokenizer", options); System.exit(-1); } }
From source file:com.intuit.tank.proxy.Main.java
public static void main(String[] args) throws Exception { Options options = new Options(); options.addOption(new Option("config", true, "The name of the recording configuration file.")); options.addOption(new Option("apiPort", true, "The port on which the api server should listen to.")); options.addOption(new Option("help", false, "Print usage.")); String configFile = null;/*from w w w. ja v a 2s . com*/ int apiPort = 8008; CommandLineParser clp = new PosixParser(); try { CommandLine cl = clp.parse(options, args); if (cl.hasOption("help")) { HelpFormatter hf = new HelpFormatter(); hf.printHelp("CLITest <arguments>", options); System.exit(0); } if (cl.hasOption("config")) { configFile = cl.getOptionValue("config"); } else { logger.info("Using default Configuration"); } if (cl.hasOption("apiPort")) { apiPort = Integer.parseInt(cl.getOptionValue("apiPort")); } else { logger.info("Using " + apiPort + " as the default apiPort."); } } catch (ParseException e) { HelpFormatter hf = new HelpFormatter(); hf.printHelp("CLITest <arguments>", options); System.exit(0); } ProxyConfiguration config = new CommonsProxyConfiguration(configFile); new Main(config, apiPort); }
From source file:cc.twittertools.download.AsyncHTMLStatusBlockCrawler.java
@SuppressWarnings("static-access") public static void main(String[] args) throws Exception { Options options = new Options(); options.addOption(OptionBuilder.withArgName("path").hasArg().withDescription("data file with tweet ids") .create(DATA_OPTION));// ww w . j a va 2 s . co m options.addOption(OptionBuilder.withArgName("path").hasArg().withDescription("output file (*.gz)") .create(OUTPUT_OPTION)); options.addOption(OptionBuilder.withArgName("path").hasArg() .withDescription("output repair file (can be used later as a data file)").create(REPAIR_OPTION)); options.addOption(NOFOLLOW_OPTION, NOFOLLOW_OPTION, false, "don't follow 301 redirects"); CommandLine cmdline = null; CommandLineParser parser = new GnuParser(); try { cmdline = parser.parse(options, args); } catch (ParseException exp) { System.err.println("Error parsing command line: " + exp.getMessage()); System.exit(-1); } if (!cmdline.hasOption(DATA_OPTION) || !cmdline.hasOption(OUTPUT_OPTION)) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp(AsyncHTMLStatusBlockCrawler.class.getName(), options); System.exit(-1); } String data = cmdline.getOptionValue(DATA_OPTION); String output = cmdline.getOptionValue(OUTPUT_OPTION); String repair = cmdline.getOptionValue(REPAIR_OPTION); boolean noFollow = cmdline.hasOption(NOFOLLOW_OPTION); new AsyncHTMLStatusBlockCrawler(new File(data), output, repair, noFollow).fetch(); }
From source file:com.xtructure.xnet.demos.art.TwoNodeSimulation.java
/** * The main method.//w w w. j a v a2 s .c o m * * @param args * the arguments */ public static void main(String[] args) { Options options = new Options(); options.addOption("n", "networkFile", true, // "the xml file from which the network is loaded"); options.addOption("i", "inputFile", true, // "the xml file from which the input is loaded"); options.addOption("test", false, "run integration test (non-gui, ignores networkFile and inputFile options)"); BasicParser bp = new BasicParser(); try { CommandLine cl = bp.parse(options, args); if (cl.hasOption("test")) { new TwoNodeSimulation(); } else { File networkFile = new File(RESOURCE_DIR, "default.network.xml"); File inputFile = new File(RESOURCE_DIR, "default.input.xml"); loadConfigFiles(RESOURCE_DIR, inputFile, networkFile); if (cl.hasOption("n")) { networkFile = new File(cl.getOptionValue("n")).getAbsoluteFile(); } if (cl.hasOption("i")) { inputFile = new File(cl.getOptionValue("i")).getAbsoluteFile(); } new TwoNodeSimulation(networkFile, inputFile); } } catch (ParseException e) { System.err.println(e.getMessage()); HelpFormatter hf = new HelpFormatter(); hf.printHelp("Usage:", options); } catch (XMLStreamException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:fr.tpt.s3.mcdag.generator.MainGenerator.java
/** * Main method for the generator: it launches a given number of threads with the parameters * given/*from w w w. ja v a 2s. com*/ * @param args */ public static void main(String[] args) { /* ============================ Command line ================= */ Options options = new Options(); Option o_hi = new Option("mu", "max_utilization", true, "Upper bound utilization"); o_hi.setRequired(true); options.addOption(o_hi); Option o_tasks = new Option("nt", "nb_tasks", true, "Number of tasks for the system"); o_tasks.setRequired(true); options.addOption(o_tasks); Option o_eprob = new Option("e", "eprobability", true, "Probability of edges"); o_eprob.setRequired(true); options.addOption(o_eprob); Option o_levels = new Option("l", "levels", true, "Number of criticality levels"); o_levels.setRequired(true); options.addOption(o_levels); Option o_para = new Option("p", "parallelism", true, "Max parallelism for the DAGs"); o_para.setRequired(true); options.addOption(o_para); Option o_nbdags = new Option("nd", "num_dags", true, "Number of DAGs"); o_nbdags.setRequired(true); options.addOption(o_nbdags); Option o_nbfiles = new Option("nf", "num_files", true, "Number of files"); o_nbfiles.setRequired(true); options.addOption(o_nbfiles); Option o_rfactor = new Option("rf", "reduc_factor", true, "Reduction factor for criticality modes"); o_rfactor.setRequired(false); options.addOption(o_rfactor); Option o_out = new Option("o", "output", true, "Output file for the DAG"); o_out.setRequired(true); options.addOption(o_out); Option graphOpt = new Option("g", "graphviz", false, "Generate a graphviz DOT file"); graphOpt.setRequired(false); options.addOption(graphOpt); Option debugOpt = new Option("d", "debug", false, "Enabling debug"); debugOpt.setRequired(false); options.addOption(debugOpt); Option jobsOpt = new Option("j", "jobs", true, "Number of jobs"); jobsOpt.setRequired(false); options.addOption(jobsOpt); CommandLineParser parser = new DefaultParser(); HelpFormatter formatter = new HelpFormatter(); CommandLine cmd; try { cmd = parser.parse(options, args); } catch (ParseException e) { System.out.println(e.getMessage()); formatter.printHelp("DAG Generator", options); System.exit(1); return; } double maxU = Double.parseDouble(cmd.getOptionValue("max_utilization")); int edgeProb = Integer.parseInt(cmd.getOptionValue("eprobability")); int levels = Integer.parseInt(cmd.getOptionValue("levels")); int nbDags = Integer.parseInt(cmd.getOptionValue("num_dags")); int nbFiles = Integer.parseInt(cmd.getOptionValue("num_files")); int para = Integer.parseInt(cmd.getOptionValue("parallelism")); int nbTasks = Integer.parseInt(cmd.getOptionValue("nb_tasks")); boolean graph = cmd.hasOption("graphviz"); boolean debug = cmd.hasOption("debug"); String output = cmd.getOptionValue("output"); int nbJobs = 1; if (cmd.hasOption("jobs")) nbJobs = Integer.parseInt(cmd.getOptionValue("jobs")); double rfactor = 2.0; if (cmd.hasOption("reduc_factor")) rfactor = Double.parseDouble(cmd.getOptionValue("reduc_factor")); /* ============================= Generator parameters ============================= */ if (nbFiles < 0 || nbDags < 0 || nbJobs < 0) { System.err.println("[ERROR] Generator: Number of files & DAGs need to be positive."); formatter.printHelp("DAG Generator", options); System.exit(1); return; } Thread threads[] = new Thread[nbJobs]; int nbFilesCreated = 0; int count = 0; while (nbFilesCreated != nbFiles) { int launched = 0; for (int i = 0; i < nbJobs && count < nbFiles; i++) { String outFile = output.substring(0, output.lastIndexOf('.')).concat("-" + count + ".xml"); GeneratorThread gt = new GeneratorThread(maxU, nbTasks, edgeProb, levels, para, nbDags, rfactor, outFile, graph, debug); threads[i] = new Thread(gt); threads[i].setName("GeneratorThread-" + i); launched++; count++; threads[i].start(); } for (int i = 0; i < launched; i++) { try { threads[i].join(); nbFilesCreated++; } catch (InterruptedException e) { e.printStackTrace(); } } } }
From source file:com.example.dlp.Redact.java
/** Command line application to redact strings, images using the Data Loss Prevention API. */ public static void main(String[] args) throws Exception { OptionGroup optionsGroup = new OptionGroup(); optionsGroup.setRequired(true);//from w w w.java 2 s . c o m Option stringOption = new Option("s", "string", true, "redact string"); optionsGroup.addOption(stringOption); Option fileOption = new Option("f", "file path", true, "redact input file path"); optionsGroup.addOption(fileOption); Options commandLineOptions = new Options(); commandLineOptions.addOptionGroup(optionsGroup); Option minLikelihoodOption = Option.builder("minLikelihood").hasArg(true).required(false).build(); commandLineOptions.addOption(minLikelihoodOption); Option replaceOption = Option.builder("r").longOpt("replace string").hasArg(true).required(false).build(); commandLineOptions.addOption(replaceOption); Option infoTypesOption = Option.builder("infoTypes").hasArg(true).required(false).build(); infoTypesOption.setArgs(Option.UNLIMITED_VALUES); commandLineOptions.addOption(infoTypesOption); Option outputFilePathOption = Option.builder("o").hasArg(true).longOpt("outputFilePath").required(false) .build(); commandLineOptions.addOption(outputFilePathOption); CommandLineParser parser = new DefaultParser(); HelpFormatter formatter = new HelpFormatter(); CommandLine cmd; try { cmd = parser.parse(commandLineOptions, args); } catch (ParseException e) { System.out.println(e.getMessage()); formatter.printHelp(Redact.class.getName(), commandLineOptions); System.exit(1); return; } String replacement = cmd.getOptionValue(replaceOption.getOpt(), "_REDACTED_"); List<InfoType> infoTypesList = new ArrayList<>(); String[] infoTypes = cmd.getOptionValues(infoTypesOption.getOpt()); if (infoTypes != null) { for (String infoType : infoTypes) { infoTypesList.add(InfoType.newBuilder().setName(infoType).build()); } } Likelihood minLikelihood = Likelihood.valueOf( cmd.getOptionValue(minLikelihoodOption.getOpt(), Likelihood.LIKELIHOOD_UNSPECIFIED.name())); // string inspection if (cmd.hasOption("s")) { String source = cmd.getOptionValue(stringOption.getOpt()); redactString(source, replacement, minLikelihood, infoTypesList); } else if (cmd.hasOption("f")) { String filePath = cmd.getOptionValue(fileOption.getOpt()); String outputFilePath = cmd.getOptionValue(outputFilePathOption.getOpt()); redactImage(filePath, minLikelihood, infoTypesList, outputFilePath); } }
From source file:cc.twittertools.download.AsyncEmbeddedJsonStatusBlockCrawler.java
@SuppressWarnings("static-access") public static void main(String[] args) throws Exception { Options options = new Options(); options.addOption(OptionBuilder.withArgName("path").hasArg().withDescription("data file with tweet ids") .create(DATA_OPTION));/*from w ww . j a va 2 s. co m*/ options.addOption(OptionBuilder.withArgName("path").hasArg().withDescription("output file (*.gz)") .create(OUTPUT_OPTION)); options.addOption(OptionBuilder.withArgName("path").hasArg() .withDescription("output repair file (can be used later as a data file)").create(REPAIR_OPTION)); options.addOption(NOFOLLOW_OPTION, NOFOLLOW_OPTION, false, "don't follow 301 redirects"); CommandLine cmdline = null; CommandLineParser parser = new GnuParser(); try { cmdline = parser.parse(options, args); } catch (ParseException exp) { System.err.println("Error parsing command line: " + exp.getMessage()); System.exit(-1); } if (!cmdline.hasOption(DATA_OPTION) || !cmdline.hasOption(OUTPUT_OPTION)) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp(AsyncEmbeddedJsonStatusBlockCrawler.class.getName(), options); System.exit(-1); } String data = cmdline.getOptionValue(DATA_OPTION); String output = cmdline.getOptionValue(OUTPUT_OPTION); String repair = cmdline.getOptionValue(REPAIR_OPTION); boolean noFollow = cmdline.hasOption(NOFOLLOW_OPTION); new AsyncEmbeddedJsonStatusBlockCrawler(new File(data), output, repair, noFollow).fetch(); }
From source file:de.prozesskraft.pkraft.Commitit.java
public static void main(String[] args) throws org.apache.commons.cli.ParseException, IOException { // try//from www . ja v a 2s .com // { // if (args.length != 3) // { // System.out.println("Please specify processdefinition file (xml) and an outputfilename"); // } // // } // catch (ArrayIndexOutOfBoundsException e) // { // System.out.println("***ArrayIndexOutOfBoundsException: Please specify processdefinition.xml, openoffice_template.od*, newfile_for_processdefinitions.odt\n" + e.toString()); // } /*---------------------------- get options from ini-file ----------------------------*/ java.io.File inifile = new java.io.File( WhereAmI.getInstallDirectoryAbsolutePath(Commitit.class) + "/" + "../etc/pkraft-commitit.ini"); if (inifile.exists()) { try { ini = new Ini(inifile); } catch (InvalidFileFormatException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } else { System.err.println("ini file does not exist: " + inifile.getAbsolutePath()); System.exit(1); } /*---------------------------- create boolean options ----------------------------*/ Option ohelp = new Option("help", "print this message"); /*---------------------------- create argument options ----------------------------*/ Option oinstance = OptionBuilder.withArgName("FILE").hasArg() .withDescription("[mandatory] process instance file") // .isRequired() .create("instance"); Option ostep = OptionBuilder.withArgName("STEPNAME").hasArg() .withDescription("[optional, default: root] process step to commit to") // .isRequired() .create("step"); Option ofile = OptionBuilder.withArgName("FILE").hasArg() .withDescription("[optional] this file will be committed as file. key will be set to 'default'") // .isRequired() .create("file"); Option okey = OptionBuilder.withArgName("KEY").hasArg() .withDescription( "[optional, default: default] this string will be considered as the key for the commit.") // .isRequired() .create("key"); Option ovariable = OptionBuilder.withArgName("VALUE").hasArg() .withDescription("[optional] this string will be committed as a variable.") // .isRequired() .create("variable"); /*---------------------------- create options object ----------------------------*/ Options options = new Options(); options.addOption(ohelp); options.addOption(oinstance); options.addOption(ostep); options.addOption(ofile); options.addOption(okey); options.addOption(ovariable); /*---------------------------- create the parser ----------------------------*/ CommandLineParser parser = new GnuParser(); try { // parse the command line arguments commandline = parser.parse(options, args); } catch (Exception exp) { // oops, something went wrong System.err.println("Parsing failed. Reason: " + exp.getMessage()); exiter(); } /*---------------------------- usage/help ----------------------------*/ if (commandline.hasOption("help")) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("commit", options); System.exit(0); } /*---------------------------- ueberpruefen ob eine schlechte kombination von parametern angegeben wurde ----------------------------*/ if (!(commandline.hasOption("instance"))) { System.out.println("option -instance is mandatory."); exiter(); } else if (!(commandline.hasOption("dir")) && !(commandline.hasOption("file")) && !(commandline.hasOption("varfile")) && !(commandline.hasOption("varname")) && !(commandline.hasOption("varvalue")) && !(commandline.hasOption("variable"))) { System.out.println( "at least one of these options needed. -dir -file -varfile -variable -varname -varvalue."); exiter(); } else if ((commandline.hasOption("varname") && !(commandline.hasOption("varvalue"))) || (!(commandline.hasOption("varname")) && commandline.hasOption("varvalue"))) { System.out.println("use options -varname and -varvalue only in combination with each other."); exiter(); } /*---------------------------- die lizenz ueberpruefen und ggf abbrechen ----------------------------*/ // check for valid license ArrayList<String> allPortAtHost = new ArrayList<String>(); allPortAtHost.add(ini.get("license-server", "license-server-1")); allPortAtHost.add(ini.get("license-server", "license-server-2")); allPortAtHost.add(ini.get("license-server", "license-server-3")); MyLicense lic = new MyLicense(allPortAtHost, "1", "user-edition", "0.1"); // lizenz-logging ausgeben for (String actLine : (ArrayList<String>) lic.getLog()) { System.err.println(actLine); } // abbruch, wenn lizenz nicht valide if (!lic.isValid()) { System.exit(1); } /*---------------------------- die eigentliche business logic ----------------------------*/ // setzen des steps String stepname = "root"; if (commandline.hasOption("step")) { stepname = commandline.getOptionValue("step"); } // setzen des key String key = "default"; if (commandline.hasOption("key")) { key = commandline.getOptionValue("key"); } Process p1 = new Process(); p1.setInfilebinary(commandline.getOptionValue("instance")); System.out.println("info: reading process instance " + commandline.getOptionValue("instance")); Process p2 = p1.readBinary(); p2.setOutfilebinary(commandline.getOptionValue("instance")); // step ueber den namen heraussuchen Step step = p2.getStep(stepname); if (step == null) { System.err.println("step not found: " + stepname); exiter(); } // den Commit 'by-process-commitit' heraussuchen oder einen neuen Commit dieses Namens erstellen Commit commit = step.getCommit("by-hand"); if (commit == null) { commit = new Commit(step); commit.setName("by-process-commitit"); } // committen if (commandline.hasOption("file")) { File file = new File(); file.setKey(key); file.setGlob(commandline.getOptionValue("file")); commit.addFile(file); commit.doIt(); } if (commandline.hasOption("variable")) { Variable variable = new Variable(); variable.setKey(key); variable.setValue(commandline.getOptionValue("variable")); commit.addVariable(variable); commit.doIt(); } p2.writeBinary(); System.out.println("info: writing process instance " + p2.getOutfilebinary()); }
From source file:io.anserini.index.IndexTweetsUpdatePlace.java
@SuppressWarnings("static-access") public static void main(String[] args) throws Exception { Options options = new Options(); options.addOption(new Option(HELP_OPTION, "show help")); options.addOption(new Option(OPTIMIZE_OPTION, "merge indexes into a single segment")); options.addOption(new Option(STORE_TERM_VECTORS_OPTION, "store term vectors")); options.addOption(OptionBuilder.withArgName("collection").hasArg() .withDescription("source collection directory").create(COLLECTION_OPTION)); options.addOption(/*from ww w. j a v a2s.c om*/ OptionBuilder.withArgName("dir").hasArg().withDescription("index location").create(INDEX_OPTION)); options.addOption(OptionBuilder.withArgName("file").hasArg().withDescription("file with deleted tweetids") .create(DELETES_OPTION)); options.addOption(OptionBuilder.withArgName("id").hasArg().withDescription("max id").create(MAX_ID_OPTION)); CommandLine cmdline = null; CommandLineParser parser = new GnuParser(); try { cmdline = parser.parse(options, args); } catch (ParseException exp) { System.err.println("Error parsing command line: " + exp.getMessage()); System.exit(-1); } if (cmdline.hasOption(HELP_OPTION) || !cmdline.hasOption(COLLECTION_OPTION) || !cmdline.hasOption(INDEX_OPTION)) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp(IndexTweetsUpdatePlace.class.getName(), options); System.exit(-1); } String collectionPath = cmdline.getOptionValue(COLLECTION_OPTION); String indexPath = cmdline.getOptionValue(INDEX_OPTION); System.out.println(collectionPath + " " + indexPath); LOG.info("collection: " + collectionPath); LOG.info("index: " + indexPath); long startTime = System.currentTimeMillis(); File file = new File(collectionPath); if (!file.exists()) { System.err.println("Error: " + file + " does not exist!"); System.exit(-1); } final FieldType textOptions = new FieldType(); textOptions.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS); textOptions.setStored(true); textOptions.setTokenized(true); if (cmdline.hasOption(STORE_TERM_VECTORS_OPTION)) { textOptions.setStoreTermVectors(true); } final StatusStream stream = new JsonStatusCorpusReader(file); final Directory dir = new SimpleFSDirectory(Paths.get(cmdline.getOptionValue(INDEX_OPTION))); final IndexWriterConfig config = new IndexWriterConfig(ANALYZER); config.setOpenMode(IndexWriterConfig.OpenMode.APPEND); final IndexWriter writer = new IndexWriter(dir, config); System.out.print("Original # of docs " + writer.numDocs()); int updateCount = 0; Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { try { stream.close(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { dir.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("Shutting down"); } }); int cnt = 0; Status status; try { while ((status = stream.next()) != null) { if (status.getPlace() != null) { // Query q = NumericRangeQuery.newLongRange(TweetStreamReader.StatusField.ID.name, status.getId(), // status.getId(), true, true); // System.out.print("Deleting docCount="+writer.numDocs()); // writer.deleteDocuments(q); // writer.commit(); // System.out.print(" Deleted docCount="+writer.numDocs()); Document doc = new Document(); doc.add(new LongField(StatusField.ID.name, status.getId(), Field.Store.YES)); doc.add(new LongField(StatusField.EPOCH.name, status.getEpoch(), Field.Store.YES)); doc.add(new TextField(StatusField.SCREEN_NAME.name, status.getScreenname(), Store.YES)); doc.add(new Field(StatusField.TEXT.name, status.getText(), textOptions)); doc.add(new IntField(StatusField.FRIENDS_COUNT.name, status.getFollowersCount(), Store.YES)); doc.add(new IntField(StatusField.FOLLOWERS_COUNT.name, status.getFriendsCount(), Store.YES)); doc.add(new IntField(StatusField.STATUSES_COUNT.name, status.getStatusesCount(), Store.YES)); doc.add(new DoubleField(StatusField.LONGITUDE.name, status.getLongitude(), Store.YES)); doc.add(new DoubleField(StatusField.LATITUDE.name, status.getlatitude(), Store.YES)); doc.add(new StringField(StatusField.PLACE.name, status.getPlace(), Store.YES)); long inReplyToStatusId = status.getInReplyToStatusId(); if (inReplyToStatusId > 0) { doc.add(new LongField(StatusField.IN_REPLY_TO_STATUS_ID.name, inReplyToStatusId, Field.Store.YES)); doc.add(new LongField(StatusField.IN_REPLY_TO_USER_ID.name, status.getInReplyToUserId(), Field.Store.YES)); } String lang = status.getLang(); if (!lang.equals("unknown")) { doc.add(new TextField(StatusField.LANG.name, status.getLang(), Store.YES)); } long retweetStatusId = status.getRetweetedStatusId(); if (retweetStatusId > 0) { doc.add(new LongField(StatusField.RETWEETED_STATUS_ID.name, retweetStatusId, Field.Store.YES)); doc.add(new LongField(StatusField.RETWEETED_USER_ID.name, status.getRetweetedUserId(), Field.Store.YES)); doc.add(new IntField(StatusField.RETWEET_COUNT.name, status.getRetweetCount(), Store.YES)); if (status.getRetweetCount() < 0 || status.getRetweetedStatusId() < 0) { LOG.warn("Error parsing retweet fields of " + status.getId()); } } long id = status.getId(); BytesRefBuilder brb = new BytesRefBuilder(); NumericUtils.longToPrefixCodedBytes(id, 0, brb); Term term = new Term(StatusField.ID.name, brb.get()); writer.updateDocument(term, doc); // writer.addDocument(doc); updateCount += 1; if (updateCount % 10000 == 0) { LOG.info(updateCount + " statuses updated"); writer.commit(); System.out.println("Updated docCount=" + writer.numDocs()); } } } LOG.info("Total elapsed time: " + (System.currentTimeMillis() - startTime) + "ms"); } catch (Exception e) { e.printStackTrace(); } finally { writer.close(); dir.close(); stream.close(); } }