Example usage for java.lang String format

List of usage examples for java.lang String format

Introduction

In this page you can find the example usage for java.lang String format.

Prototype

public static String format(String format, Object... args) 

Source Link

Document

Returns a formatted string using the specified format string and arguments.

Usage

From source file:com.genentech.chemistry.openEye.apps.SDFSubRMSD.java

public static void main(String... args) throws IOException { // create command line Options object
    Options options = new Options();
    Option opt = new Option("in", true, "input file oe-supported");
    opt.setRequired(true);//from  w  w w  .  j a v  a2s. c o  m
    options.addOption(opt);

    opt = new Option("out", true, "output file oe-supported");
    opt.setRequired(false);
    options.addOption(opt);

    opt = new Option("fragFile", true, "file with single 3d substructure query");
    opt.setRequired(false);
    options.addOption(opt);

    opt = new Option("isMDL", false,
            "if given the fragFile is suposed to be an mdl query file, query features are supported.");
    opt.setRequired(false);
    options.addOption(opt);

    CommandLineParser parser = new PosixParser();
    CommandLine cmd = null;
    try {
        cmd = parser.parse(options, args);
    } catch (Exception e) {
        System.err.println(e.getMessage());
        exitWithHelp(options);
    }
    args = cmd.getArgs();

    if (cmd.hasOption("d")) {
        System.err.println("Start debugger and press return:");
        new BufferedReader(new InputStreamReader(System.in)).readLine();
    }

    String inFile = cmd.getOptionValue("in");
    String outFile = cmd.getOptionValue("out");
    String fragFile = cmd.getOptionValue("fragFile");

    // read fragment
    OESubSearch ss;
    oemolistream ifs = new oemolistream(fragFile);
    OEMolBase mol;
    if (!cmd.hasOption("isMDL")) {
        mol = new OEGraphMol();
        oechem.OEReadMolecule(ifs, mol);
        ss = new OESubSearch(mol, OEExprOpts.AtomicNumber, OEExprOpts.BondOrder);
    } else {
        int aromodel = OEIFlavor.Generic.OEAroModelOpenEye;
        int qflavor = ifs.GetFlavor(ifs.GetFormat());
        ifs.SetFlavor(ifs.GetFormat(), (qflavor | aromodel));
        int opts = OEMDLQueryOpts.Default | OEMDLQueryOpts.SuppressExplicitH;
        OEQMol qmol = new OEQMol();
        oechem.OEReadMDLQueryFile(ifs, qmol, opts);
        ss = new OESubSearch(qmol);
        mol = qmol;
    }

    double nSSatoms = mol.NumAtoms();
    double sssCoords[] = new double[mol.GetMaxAtomIdx() * 3];
    mol.GetCoords(sssCoords);
    mol.Clear();
    ifs.close();

    if (!ss.IsValid())
        throw new Error("Invalid query " + args[0]);

    ifs = new oemolistream(inFile);
    oemolostream ofs = new oemolostream(outFile);
    int count = 0;

    while (oechem.OEReadMolecule(ifs, mol)) {
        count++;
        double rmsd = Double.MAX_VALUE;
        double molCoords[] = new double[mol.GetMaxAtomIdx() * 3];
        mol.GetCoords(molCoords);

        for (OEMatchBase mb : ss.Match(mol, false)) {
            double r = 0;
            for (OEMatchPairAtom mp : mb.GetAtoms()) {
                OEAtomBase asss = mp.getPattern();
                double sx = sssCoords[asss.GetIdx() * 3];
                double sy = sssCoords[asss.GetIdx() * 3];
                double sz = sssCoords[asss.GetIdx() * 3];

                OEAtomBase amol = mp.getTarget();
                double mx = molCoords[amol.GetIdx() * 3];
                double my = molCoords[amol.GetIdx() * 3];
                double mz = molCoords[amol.GetIdx() * 3];

                r += Math.sqrt((sx - mx) * (sx - mx) + (sy - my) * (sy - my) + (sz - mz) * (sz - mz));
            }
            r /= nSSatoms;
            rmsd = Math.min(rmsd, r);
        }

        if (rmsd != Double.MAX_VALUE)
            oechem.OESetSDData(mol, "SSSrmsd", String.format("%.3f", rmsd));

        oechem.OEWriteMolecule(ofs, mol);
        mol.Clear();
    }

    ifs.close();
    ofs.close();

    mol.delete();
    ss.delete();
}

From source file:edu.indiana.d2i.sloan.internal.CreateVMSimulator.java

public static void main(String[] args) {
    CreateVMSimulator simulator = new CreateVMSimulator();

    CommandLineParser parser = new PosixParser();

    try {/* w  w w.  j  ava 2  s  .  com*/
        CommandLine line = simulator.parseCommandLine(parser, args);

        String imagePath = line.getOptionValue(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.IMAGE_PATH));
        int vcpu = Integer.parseInt(line.getOptionValue(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.VCPU)));
        int mem = Integer.parseInt(line.getOptionValue(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.MEM)));

        if (!HypervisorCmdSimulator.resourceExist(imagePath)) {
            logger.error(String.format("Cannot find requested image: %s", imagePath));
            System.exit(ERROR_CODE.get(ERROR_STATE.IMAGE_NOT_EXIST));
        }

        if (!hasEnoughCPUs(vcpu)) {
            logger.error(String.format("Don't have enough cpus, requested %d", vcpu));
            System.exit(ERROR_CODE.get(ERROR_STATE.NOT_ENOUGH_CPU));
        }

        if (!hasEnoughMem(mem)) {
            logger.error(String.format("Don't have enough memory, requested %d", mem));
            System.exit(ERROR_CODE.get(ERROR_STATE.NOT_ENOUGH_MEM));
        }

        String wdir = line.getOptionValue(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.WORKING_DIR));

        if (HypervisorCmdSimulator.resourceExist(wdir)) {
            logger.error(String.format("Working directory %s already exists ", wdir));
            System.exit(ERROR_CODE.get(ERROR_STATE.VM_ALREADY_EXIST));
        }

        // copy VM image to working directory
        File imageFile = new File(imagePath);
        FileUtils.copyFile(imageFile, new File(HypervisorCmdSimulator.cleanPath(wdir) + imageFile.getName()));

        // write state as property file so that we can query later
        Properties prop = new Properties();

        prop.put(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.IMAGE_PATH), imagePath);
        prop.put(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.VCPU), String.valueOf(vcpu));
        prop.put(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.MEM), String.valueOf(mem));
        prop.put(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.WORKING_DIR), wdir);
        prop.put(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.VNC_PORT),
                line.getOptionValue(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.VNC_PORT)));
        prop.put(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.SSH_PORT),
                line.getOptionValue(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.SSH_PORT)));
        prop.put(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.LOGIN_USERNAME),
                line.getOptionValue(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.LOGIN_USERNAME)));
        prop.put(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.LOGIN_PASSWD),
                line.getOptionValue(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.LOGIN_PASSWD)));

        // write VM state as shutdown
        prop.put(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.VM_STATE), VMState.SHUTDOWN.toString());
        // write VM mode as undefined
        prop.put(CMD_FLAG_VALUE.get(CMD_FLAG_KEY.VM_MODE), VMMode.NOT_DEFINED.toString());

        prop.store(
                new FileOutputStream(new File(
                        HypervisorCmdSimulator.cleanPath(wdir) + HypervisorCmdSimulator.VM_INFO_FILE_NAME)),
                "");

        // do other related settings
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            logger.error(e.getMessage());
        }

        // success
        System.exit(0);

    } catch (ParseException e) {
        logger.error(String.format("Cannot parse input arguments: %s%n, expected:%n%s",
                StringUtils.join(args, " "), simulator.getUsage(100, "", 5, 5, "")));

        System.exit(ERROR_CODE.get(ERROR_STATE.INVALID_INPUT_ARGS));
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
        System.exit(ERROR_CODE.get(ERROR_STATE.IO_ERR));
    }
}

From source file:com.github.checkstyle.Main.java

/**
 * Entry point./*  w  ww  .j  a  v  a 2  s  . c  o  m*/
 * @param args command line arguments.
 */
public static void main(String... args) {
    int errorCounter;
    List<String> publicationErrors = null;
    try {
        final CliProcessor cliProcessor = new CliProcessor(args);
        cliProcessor.process();
        if (cliProcessor.hasErrors()) {
            printListOf(cliProcessor.getErrorMessages());
            errorCounter = cliProcessor.getErrorMessages().size();
        } else {
            final CliOptions cliOptions = cliProcessor.getCliOptions();
            final Result notesBuilderResult = runNotesBuilder(cliOptions);
            errorCounter = notesBuilderResult.getErrorMessages().size();
            if (errorCounter == 0) {
                runPostGeneration(notesBuilderResult.getReleaseNotes(), cliOptions);
                publicationErrors = runPostPublication(cliOptions);
            }
        }
    } catch (ParseException | GitAPIException | IOException | TemplateException ex) {
        errorCounter = 1;
        System.out.println(ex.getMessage());
        CliProcessor.printUsage();
    }
    if (errorCounter == 0) {
        if (publicationErrors != null && !publicationErrors.isEmpty()) {
            System.out.println(String.format("%nPublication ends with %d errors:", publicationErrors.size()));
            printListOf(publicationErrors);
        } else {
            System.out.println(String.format("%nExecution succeeded!"));
        }
    } else {
        System.out.println(String.format("%nGeneration ends with %d errors.", errorCounter));
        System.exit(ERROR_EXIT_CODE);
    }
}

From source file:net.orzo.App.java

/**
 *
 *///from   w w w.j a v  a  2 s.  co m
public static void main(final String[] args) {
    final App app = new App();
    Logger log = null;
    CommandLine cmd;

    try {
        cmd = app.init(args);

        if (cmd.hasOption("h")) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp(
                    "orzo [options] user_script [user_arg1 [user_arg2 [...]]]\n(to generate a template: orzo -t [file path])",
                    app.cliOptions);

        } else if (cmd.hasOption("v")) {
            System.out.printf("Orzo.js version %s\n", app.props.get("orzo.version"));

        } else if (cmd.hasOption("t")) {
            String templateSrc = new ResourceLoader().getResourceAsString("net/orzo/template1.js");

            File tplFile = new File(cmd.getOptionValue("t"));
            FileWriter tplWriter = new FileWriter(tplFile);
            tplWriter.write(templateSrc);
            tplWriter.close();

            File dtsFile = new File(
                    String.format("%s/orzojs.d.ts", new File(tplFile.getAbsolutePath()).getParent()));
            FileWriter dtsWriter = new FileWriter(dtsFile);
            String dtsSrc = new ResourceLoader().getResourceAsString("net/orzo/orzojs.d.ts");
            dtsWriter.write(dtsSrc);
            dtsWriter.close();

        } else if (cmd.hasOption("T")) {
            String templateSrc = new ResourceLoader().getResourceAsString("net/orzo/template1.js");
            System.out.println(templateSrc);

        } else {

            // Logger initialization
            if (cmd.hasOption("g")) {
                System.setProperty("logback.configurationFile", cmd.getOptionValue("g"));

            } else {
                System.setProperty("logback.configurationFile", "./logback.xml");
            }
            log = LoggerFactory.getLogger(App.class);
            if (cmd.hasOption("s")) { // Orzo.js as a REST and AMQP service
                FullServiceConfig conf = new Gson().fromJson(new FileReader(cmd.getOptionValue("s")),
                        FullServiceConfig.class);
                Injector injector = Guice.createInjector(new CoreModule(conf), new RestServletModule());
                HttpServer httpServer = new HttpServer(conf, new JerseyGuiceServletConfig(injector));
                app.services.add(httpServer);

                if (conf.getAmqpResponseConfig() != null) {
                    // response AMQP service must be initialized before receiving one
                    app.services.add(injector.getInstance(AmqpResponseConnection.class));
                }

                if (conf.getAmqpConfig() != null) {
                    app.services.add(injector.getInstance(AmqpConnection.class));
                    app.services.add(injector.getInstance(AmqpService.class));
                }

                if (conf.getRedisConf() != null) {
                    app.services.add(injector.getInstance(RedisStorage.class));
                }

                Runtime.getRuntime().addShutdownHook(new ShutdownHook(app));
                app.startServices();

            } else if (cmd.hasOption("d")) { // Demo mode
                final String scriptId = "demo";
                final SourceCode demoScript = SourceCode.fromResource(DEMO_SCRIPT);
                System.err.printf("Running demo script %s.", demoScript.getName());
                CmdConfig conf = new CmdConfig(scriptId, demoScript, null, cmd.getOptionValue("p", null));
                TaskManager tm = new TaskManager(conf);
                tm.startTaskSync(tm.registerTask(scriptId, new String[0]));

            } else if (cmd.getArgs().length > 0) { // Command line mode
                File userScriptFile = new File(cmd.getArgs()[0]);
                String optionalModulesPath = null;
                String[] inputValues;
                SourceCode userScript;

                // custom CommonJS modules path
                if (cmd.hasOption("m")) {
                    optionalModulesPath = cmd.getOptionValue("m");
                }

                if (cmd.getArgs().length > 0) {
                    inputValues = Arrays.copyOfRange(cmd.getArgs(), 1, cmd.getArgs().length);
                } else {
                    inputValues = new String[0];
                }

                userScript = SourceCode.fromFile(userScriptFile);
                CmdConfig conf = new CmdConfig(userScript.getName(), userScript, optionalModulesPath,
                        cmd.getOptionValue("p", null));
                TaskManager tm = new TaskManager(conf);
                String taskId = tm.registerTask(userScript.getName(), inputValues);
                tm.startTaskSync(taskId);
                if (tm.getTask(taskId).getStatus() == TaskStatus.ERROR) {
                    tm.getTask(taskId).getFirstError().getErrors().stream().forEach(System.err::println);
                }

            } else {
                System.err.println("Invalid parameters. Try -h for more information.");
                System.exit(1);
            }
        }

    } catch (Exception ex) {
        System.err.printf("Orzo.js crashed with error: %s\nSee the log for details.\n", ex.getMessage());
        if (log != null) {
            log.error(ex.getMessage(), ex);

        } else {
            ex.printStackTrace();
        }
    }
}

From source file:com.hortonworks.registries.storage.tool.shell.ShellMigrationInitializer.java

public static void main(String[] args) throws Exception {
    Options options = new Options();

    options.addOption(Option.builder("s").numberOfArgs(1).longOpt(OPTION_SCRIPT_ROOT_PATH)
            .desc("Root directory of script path").build());

    options.addOption(Option.builder("c").numberOfArgs(1).longOpt(OPTION_CONFIG_FILE_PATH)
            .desc("Config file path").build());

    options.addOption(Option.builder().hasArg(false).longOpt(ShellMigrationOption.MIGRATE.toString())
            .desc("Execute schema migration from last check point").build());

    options.addOption(Option.builder().hasArg(false).longOpt(ShellMigrationOption.INFO.toString())
            .desc("Show the status of the schema migration compared to the target database").build());

    options.addOption(Option.builder().hasArg(false).longOpt(ShellMigrationOption.VALIDATE.toString())
            .desc("Validate the target database changes with the migration scripts").build());

    options.addOption(Option.builder().hasArg(false).longOpt(ShellMigrationOption.REPAIR.toString()).desc(
            "Repairs the SCRIPT_CHANGE_LOG by removing failed migrations and correcting checksum of existing migration script")
            .build());/*  w w w  . ja  v a 2 s  .  c  o  m*/

    CommandLineParser parser = new BasicParser();
    CommandLine commandLine = parser.parse(options, args);

    if (!commandLine.hasOption(OPTION_SCRIPT_ROOT_PATH)) {
        usage(options);
        System.exit(1);
    }

    boolean isShellMigrationOptionSpecified = false;
    ShellMigrationOption shellMigrationOptionSpecified = null;
    for (ShellMigrationOption shellMigrationOption : ShellMigrationOption.values()) {
        if (commandLine.hasOption(shellMigrationOption.toString())) {
            if (isShellMigrationOptionSpecified) {
                System.out.println(
                        "Only one operation can be execute at once, please select one of ',migrate', 'validate', 'info', 'repair'.");
                System.exit(1);
            }
            isShellMigrationOptionSpecified = true;
            shellMigrationOptionSpecified = shellMigrationOption;
        }
    }

    if (!isShellMigrationOptionSpecified) {
        System.out.println(
                "One of the option 'migrate', 'validate', 'info', 'repair' must be specified to execute.");
        System.exit(1);
    }

    String scriptRootPath = commandLine.getOptionValue(OPTION_SCRIPT_ROOT_PATH);
    String confFilePath = commandLine.getOptionValue(OPTION_CONFIG_FILE_PATH);

    StorageProviderConfiguration storageProperties;
    try {
        Map<String, Object> conf = Utils.readConfig(confFilePath);

        StorageProviderConfigurationReader confReader = new StorageProviderConfigurationReader();
        storageProperties = confReader.readStorageConfig(conf);
    } catch (IOException e) {
        System.err.println("Error occurred while reading config file: " + confFilePath);
        System.exit(1);
        throw new IllegalStateException("Shouldn't reach here");
    }

    ShellMigrationHelper schemaMigrationHelper = new ShellMigrationHelper(
            ShellFlywayFactory.get(storageProperties, scriptRootPath));
    try {
        schemaMigrationHelper.execute(shellMigrationOptionSpecified);
        System.out.println(String.format("\"%s\" option successful", shellMigrationOptionSpecified.toString()));
    } catch (Exception e) {
        System.err.println(String.format("\"%s\" option failed : %s", shellMigrationOptionSpecified.toString(),
                e.getMessage()));
        System.exit(1);
    }

}

From source file:AmazonKinesisSample.java

public static void main(String[] args) throws Exception {
    init();//from  w  ww .jav a  2s .  c  o  m

    final String myStreamName = "myFirstStream";
    final Integer myStreamSize = 1;

    // Create a stream. The number of shards determines the provisioned throughput.

    CreateStreamRequest createStreamRequest = new CreateStreamRequest();
    createStreamRequest.setStreamName(myStreamName);
    createStreamRequest.setShardCount(myStreamSize);

    kinesisClient.createStream(createStreamRequest);
    // The stream is now being created.
    LOG.info("Creating Stream : " + myStreamName);
    waitForStreamToBecomeAvailable(myStreamName);

    // list all of my streams
    ListStreamsRequest listStreamsRequest = new ListStreamsRequest();
    listStreamsRequest.setLimit(10);
    ListStreamsResult listStreamsResult = kinesisClient.listStreams(listStreamsRequest);
    List<String> streamNames = listStreamsResult.getStreamNames();
    while (listStreamsResult.isHasMoreStreams()) {
        if (streamNames.size() > 0) {
            listStreamsRequest.setExclusiveStartStreamName(streamNames.get(streamNames.size() - 1));
        }

        listStreamsResult = kinesisClient.listStreams(listStreamsRequest);
        streamNames.addAll(listStreamsResult.getStreamNames());

    }
    LOG.info("Printing my list of streams : ");

    // print all of my streams.
    if (!streamNames.isEmpty()) {
        System.out.println("List of my streams: ");
    }
    for (int i = 0; i < streamNames.size(); i++) {
        System.out.println(streamNames.get(i));
    }

    LOG.info("Putting records in stream : " + myStreamName);
    // Write 10 records to the stream
    for (int j = 0; j < 10; j++) {
        PutRecordRequest putRecordRequest = new PutRecordRequest();
        putRecordRequest.setStreamName(myStreamName);
        putRecordRequest.setData(ByteBuffer.wrap(String.format("testData-%d", j).getBytes()));
        putRecordRequest.setPartitionKey(String.format("partitionKey-%d", j));
        PutRecordResult putRecordResult = kinesisClient.putRecord(putRecordRequest);
        System.out.println("Successfully putrecord, partition key : " + putRecordRequest.getPartitionKey()
                + ", ShardID : " + putRecordResult.getShardId());
    }

    // Delete the stream.
    LOG.info("Deleting stream : " + myStreamName);
    DeleteStreamRequest deleteStreamRequest = new DeleteStreamRequest();
    deleteStreamRequest.setStreamName(myStreamName);

    kinesisClient.deleteStream(deleteStreamRequest);
    // The stream is now being deleted.
    LOG.info("Stream is now being deleted : " + myStreamName);
}

From source file:AmazonKinesisCreate.java

public static void main(String[] args) throws Exception {
    init();/*from   w  w w.  ja va 2s.  c o m*/

    final String myStreamName = "philsteststream";
    final Integer myStreamSize = 1;

    // Create a stream. The number of shards determines the provisioned throughput.

    CreateStreamRequest createStreamRequest = new CreateStreamRequest();
    createStreamRequest.setStreamName(myStreamName);
    createStreamRequest.setShardCount(myStreamSize);

    // pt
    kinesisClient.createStream(createStreamRequest);

    // The stream is now being created.
    LOG.info("Creating Stream : " + myStreamName);
    waitForStreamToBecomeAvailable(myStreamName);

    // list all of my streams
    ListStreamsRequest listStreamsRequest = new ListStreamsRequest();
    listStreamsRequest.setLimit(10);
    ListStreamsResult listStreamsResult = kinesisClient.listStreams(listStreamsRequest);
    List<String> streamNames = listStreamsResult.getStreamNames();
    while (listStreamsResult.isHasMoreStreams()) {
        if (streamNames.size() > 0) {
            listStreamsRequest.setExclusiveStartStreamName(streamNames.get(streamNames.size() - 1));
        }

        listStreamsResult = kinesisClient.listStreams(listStreamsRequest);
        streamNames.addAll(listStreamsResult.getStreamNames());

    }
    LOG.info("Printing my list of streams : ");

    // print all of my streams.
    if (!streamNames.isEmpty()) {
        System.out.println("List of my streams: ");
    }
    for (int i = 0; i < streamNames.size(); i++) {
        System.out.println(streamNames.get(i));
    }

    LOG.info("Putting records in stream : " + myStreamName);
    // Write 10 records to the stream
    for (int j = 0; j < 10; j++) {

        try {
            PutRecordRequest putRecordRequest = new PutRecordRequest();
            putRecordRequest.setStreamName(myStreamName);
            putRecordRequest.setData(ByteBuffer.wrap(String.format("testData-%d", j).getBytes()));
            putRecordRequest.setPartitionKey(String.format("partitionKey-%d", j));
            PutRecordResult putRecordResult = kinesisClient.putRecord(putRecordRequest);
            System.out.println("Successfully putrecord, partition key : " + putRecordRequest.getPartitionKey()
                    + ", ShardID : " + putRecordResult.getShardId());
            Thread.sleep(1000);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    // Delete the stream.

    /*
    LOG.info("Deleting stream : " + myStreamName);
    DeleteStreamRequest deleteStreamRequest = new DeleteStreamRequest();
    deleteStreamRequest.setStreamName(myStreamName);
            
    kinesisClient.deleteStream(deleteStreamRequest);
    // The stream is now being deleted.
    LOG.info("Stream is now being deleted : " + myStreamName);
            
    LOG.info("Streaming completed" + myStreamName);
    */

}

From source file:edu.cmu.lti.oaqa.knn4qa.apps.CollectionSplitter.java

public static void main(String[] args) {
    Options options = new Options();

    options.addOption("i", null, true, "Input file");
    options.addOption("o", null, true, "Output file prefix");
    options.addOption("p", null, true, "Comma separated probabilities e.g., 0.1,0.2,0.7.");
    options.addOption("n", null, true, "Comma separated part names, e.g., dev,test,train");

    CommandLineParser parser = new org.apache.commons.cli.GnuParser();

    try {//w ww. j a  v  a 2 s. co m
        CommandLine cmd = parser.parse(options, args);

        InputStream input = null;

        if (cmd.hasOption("i")) {
            input = CompressUtils.createInputStream(cmd.getOptionValue("i"));
        } else {
            Usage("Specify Input file");
        }

        ArrayList<Double> probs = new ArrayList<Double>();
        String[] partNames = null;

        if (cmd.hasOption("p")) {
            String parts[] = cmd.getOptionValue("p").split(",");

            try {
                double sum = 0;
                for (String s : parts) {
                    double p = Double.parseDouble(s);
                    if (p <= 0 || p > 1)
                        Usage("All probabilities must be in the range (0,1)");
                    sum += p;
                    probs.add(p);
                }

                if (Math.abs(sum - 1.0) > Float.MIN_NORMAL) {
                    Usage("The sum of probabilities should be equal to 1, but it's: " + sum);
                }
            } catch (NumberFormatException e) {
                Usage("Can't convert some of the probabilities to a floating-point number.");
            }
        } else {
            Usage("Specify part probabilities.");
        }

        if (cmd.hasOption("n")) {
            partNames = cmd.getOptionValue("n").split(",");

            if (partNames.length != probs.size())
                Usage("The number of probabilities is not equal to the number of parts!");
        } else {
            Usage("Specify part names");
        }

        BufferedWriter[] outFiles = new BufferedWriter[partNames.length];

        if (cmd.hasOption("o")) {
            String outPrefix = cmd.getOptionValue("o");

            for (int partId = 0; partId < partNames.length; ++partId) {
                outFiles[partId] = new BufferedWriter(new OutputStreamWriter(
                        CompressUtils.createOutputStream(outPrefix + "_" + partNames[partId] + ".gz")));
            }
        } else {
            Usage("Specify Output file prefix");
        }

        System.out.println("Using probabilities:");
        for (int partId = 0; partId < partNames.length; ++partId)
            System.out.println(partNames[partId] + " : " + probs.get(partId));
        System.out.println("=================================================");

        XmlIterator inpIter = new XmlIterator(input, YahooAnswersReader.DOCUMENT_TAG);

        String oneRec = inpIter.readNext();
        int docNum = 1;
        for (; !oneRec.isEmpty(); ++docNum, oneRec = inpIter.readNext()) {
            double p = Math.random();

            if (docNum % 1000 == 0) {
                System.out.println(String.format("Processed %d documents", docNum));
            }

            BufferedWriter out = null;

            for (int partId = 0; partId < partNames.length; ++partId) {
                double pp = probs.get(partId);
                if (p <= pp || partId + 1 == partNames.length) {
                    out = outFiles[partId];
                    break;
                }
                p -= pp;
            }

            oneRec = oneRec.trim() + System.getProperty("line.separator");
            out.write(oneRec);
        }
        System.out.println(String.format("Processed %d documents", docNum - 1));
        // It's important to close all the streams here!
        for (BufferedWriter f : outFiles)
            f.close();
    } catch (ParseException e) {
        Usage("Cannot parse arguments");
    } catch (Exception e) {
        System.err.println("Terminating due to an exception: " + e);
        System.exit(1);
    }
}

From source file:edu.lternet.pasta.datapackagemanager.ProvenanceBackfiller.java

/**
 * Main program to backfill the provenance table. 
 * It takes no command arguments. /*from w w  w .ja v  a 2 s.  c om*/
 * Should be run in the DataPackageManager top-level directory.
 * 
 * @param args   Any arguments passed to the program are ignored.
 */
public static void main(String[] args) {
    int backfillCount = 0;
    int derivedDataPackageCount = 0;
    int recordsInserted = 0;
    final boolean evaluateMode = false;

    try {
        ConfigurationListener configurationListener = new ConfigurationListener();
        configurationListener.initialize(dirPath);
        DataPackageRegistry dpr = DataPackageManager.makeDataPackageRegistry();
        ProvenanceIndex provenanceIndex = new ProvenanceIndex(dpr);
        EmlPackageIdFormat epif = new EmlPackageIdFormat();

        List<EmlPackageId> emlPackageIds = getAllDataPackageRevisions(dpr);

        for (EmlPackageId emlPackageId : emlPackageIds) {
            DataPackageMetadata dataPackageMetadata = new DataPackageMetadata(emlPackageId);
            if (dataPackageMetadata != null) {
                File levelOneEMLFile = dataPackageMetadata.getMetadata(evaluateMode);
                String emlDocument = FileUtils.readFileToString(levelOneEMLFile);
                String packageId = epif.format(emlPackageId);
                System.err.println("  " + packageId);

                try {
                    ArrayList<String> sourceIds = provenanceIndex.insertProvenanceRecords(packageId,
                            emlDocument);
                    if ((sourceIds != null) && (sourceIds.size() > 0)) {
                        derivedDataPackageCount++;
                        recordsInserted += sourceIds.size();
                    }
                } catch (ProvenanceException e) {
                    logger.error(e.getMessage());
                }

                backfillCount++;
            }
        }
    } catch (Exception e) {
        logger.error(
                String.format("An error occurred during provenance backfill processing: %s", e.getMessage()));
        e.printStackTrace();
    }

    System.err.println(String.format(
            "Finished provenance backfill processing. Provenance generated and stored for %d resource(s).",
            backfillCount));
    System.err.println(
            String.format("Total number of derived data packages detected: %d.", derivedDataPackageCount));
    System.err.println(String.format("Total records inserted into provenance table: %d.", recordsInserted));
}

From source file:com.netflix.aegisthus.tools.SSTableExport.java

@SuppressWarnings("rawtypes")
public static void main(String[] args) throws IOException {
    String usage = String.format("Usage: %s <sstable>", SSTableExport.class.getName());

    CommandLineParser parser = new PosixParser();
    try {/*from   w  w  w .jav  a2s . c o m*/
        cmd = parser.parse(options, args);
    } catch (ParseException e1) {
        System.err.println(e1.getMessage());
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(usage, options);
        System.exit(1);
    }

    if (cmd.getArgs().length != 1) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(usage, options);
        System.exit(1);
    }
    Map<String, AbstractType> convertors = null;
    if (cmd.hasOption(COLUMN_NAME_TYPE)) {
        try {
            convertors = new HashMap<String, AbstractType>();
            convertors.put(SSTableScanner.COLUMN_NAME_KEY,
                    TypeParser.parse(cmd.getOptionValue(COLUMN_NAME_TYPE)));
        } catch (ConfigurationException e) {
            System.err.println(e.getMessage());
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp(usage, options);
            System.exit(1);
        } catch (SyntaxException e) {
            System.err.println(e.getMessage());
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp(usage, options);
            System.exit(1);
        }
    }
    Descriptor.Version version = null;
    if (cmd.hasOption(OPT_VERSION)) {
        version = new Descriptor.Version(cmd.getOptionValue(OPT_VERSION));
    }

    if (cmd.hasOption(INDEX_SPLIT)) {
        String ssTableFileName;
        DataInput input = null;
        if ("-".equals(cmd.getArgs()[0])) {
            ssTableFileName = System.getProperty("aegisthus.file.name");
            input = new DataInputStream(new BufferedInputStream(System.in, 65536 * 10));
        } else {
            ssTableFileName = new File(cmd.getArgs()[0]).getAbsolutePath();
            input = new DataInputStream(
                    new BufferedInputStream(new FileInputStream(ssTableFileName), 65536 * 10));
        }
        exportIndexSplit(ssTableFileName, input);
    } else if (cmd.hasOption(INDEX)) {
        String ssTableFileName = new File(cmd.getArgs()[0]).getAbsolutePath();
        exportIndex(ssTableFileName);
    } else if (cmd.hasOption(ROWSIZE)) {
        String ssTableFileName = new File(cmd.getArgs()[0]).getAbsolutePath();
        exportRowSize(ssTableFileName);
    } else if ("-".equals(cmd.getArgs()[0])) {
        if (version == null) {
            System.err.println("when streaming must supply file version");
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp(usage, options);
            System.exit(1);
        }
        exportStream(version);
    } else {
        String ssTableFileName = new File(cmd.getArgs()[0]).getAbsolutePath();
        FileInputStream fis = new FileInputStream(ssTableFileName);
        InputStream inputStream = new DataInputStream(new BufferedInputStream(fis, 65536 * 10));
        long end = -1;
        if (cmd.hasOption(END)) {
            end = Long.valueOf(cmd.getOptionValue(END));
        }
        if (cmd.hasOption(OPT_COMP)) {
            CompressionMetadata cm = new CompressionMetadata(
                    new BufferedInputStream(new FileInputStream(cmd.getOptionValue(OPT_COMP)), 65536),
                    fis.getChannel().size());
            inputStream = new CompressionInputStream(inputStream, cm);
            end = cm.getDataLength();
        }
        DataInputStream input = new DataInputStream(inputStream);
        if (version == null) {
            version = Descriptor.fromFilename(ssTableFileName).version;
        }
        SSTableScanner scanner = new SSTableScanner(input, convertors, end, version);
        if (cmd.hasOption(OPT_MAX_COLUMN_SIZE)) {
            scanner.setMaxColSize(Long.parseLong(cmd.getOptionValue(OPT_MAX_COLUMN_SIZE)));
        }
        export(scanner);
        if (cmd.hasOption(OPT_MAX_COLUMN_SIZE)) {
            if (scanner.getErrorRowCount() > 0) {
                System.err.println(String.format("%d rows were too large", scanner.getErrorRowCount()));
            }
        }
    }
}