Douglas McIlwraith
Douglas McIlwraith

Reputation: 31

Calling mongoexport from Java through Runtime with a query parameter (Encoding issue?)

I've recently come across this issue and wondered if anybody could help me shed some light on it.

I'm trying to perform a mongoexport from a Java application, with the export restricted to a particular date range. I've constructed my query command, and passed this to Runtime.exec. This returns with a code 2, saying "too many positional options".

However, if I take the string which was passed to exec (logged out below), and run it on the command line, it works perfectly!

I've narrowed this down to the "query" parameter -- if I don't construct the command with this, the command will be executed perfectly through Runtime.exec()

I'm guessing its some encoding issue to do with the quotes in the query parameter, but I can't for the life of me figure out how to fix it.

Here's the code:

@Override
public void doWork() { 
    logger.info("Doing work");

    //get the host for performing the mongo dump
    String mongohome = GlimmerServer.config.getString("mongo.home");
    String host = GlimmerServer.config.getString("mongo.dumphost");
    String port = GlimmerServer.config.getString("mongo.dumpport");
    String db = GlimmerServer.config.getString("mongo.dumpdb");
    String collection = "stats_advert_daily";
    String query = "'{date : new Date(1320451200000)}'"; //needs to be a proper query for mongo
    String outputlocation = "/tmp/output.txt"; //needs to be asigned a random number name       

    String command = String.format(mongohome+"/bin/mongoexport " +
            "--host %s " +
            "--port %s " +
            "--db %s " +        
            "--collection %s " +                
            "--query %s " +
            "--fields _id,account_rid " +               
            "--out %s " +           
            "--slaveOk true " +         
            "--csv " +
            "-vvvvv",
            host,port,db,collection,query,outputlocation);

    logger.info(command);

    try{            
            Runtime rt = Runtime.getRuntime();              
            Process pr = rt.exec(command);
            StreamGobbler errorGobbler = new StreamGobbler(pr.getErrorStream(),"ERROR",logger);
            StreamGobbler outputGobbler = new StreamGobbler(pr.getInputStream(),"OUTPUT",logger);
            errorGobbler.start();
            outputGobbler.start();
            int exitVal = pr.waitFor();

            logger.info(String.format("Process executed with exit code %d",exitVal));

    }catch(Exception e){
        logger.error(String.format("Error running task. Exception %s", e.toString()));
    }       

}

All help appreciated!

Cheers, Doug

Upvotes: 2

Views: 2085

Answers (2)

Gaurav Shewale
Gaurav Shewale

Reputation: 1

    String db = "Doctors";
    String col = "patients";
    String Host = "localhost";
    String Port = "27017";
    String fileName = "/home/gshewale/Documents/list.csv";
    String name="\"name\"";
    String patientname="\"Gaurav\"";

String command = "mongoexport --host " + Host + " --port " + Port + " --db " + db + " --collection " + col + " --type=csv --fields name --query {"+name+":"+patientname+"} --out " + fileName + "";

try {
    System.out.println(command);
    Process process = Runtime.getRuntime().exec(command);
    int waitFor = process.waitFor();
    System.out.println("waitFor:: " + waitFor);



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

}

this works for me..!!!!

Upvotes: 0

Douglas McIlwraith
Douglas McIlwraith

Reputation: 31

Turns out it was related to this issue: Command fails in script, works in command line

String query = "'{date : new Date(1320451200000)}'";

The spaces in the query were causing some parsing issue.

Also, the single quotes are not needed. Thus the offfending code now looks like this:

String query = "{date:Date(1320451200000)}";

Now, if I copy the entire command into a shell it doesn't work (needs the single quotes), yet in runs through Runtime.exec().

Upvotes: 1

Related Questions