2
votes

I'm able to export an entire collection using Mongoexport command in the Mongo shell.

But, I'm trying to write a java program which uses a Mongoexport command to export entire collection of MongoDB into a CSV file.

My code:

public class MongoExportSample {
    public static void main(String[] args) {

         String db = "pack";
         String col = "col";
         String Host="localhost";
         String Port="27017";
         String fileName = "D:/user/sample.csv";
         String command = "mongoexport --host Host --port Port --db " + db + " --collection " + col + " --csv --out " + fileName + "";

         try {
             Process process=Runtime.getRuntime().exec(command);
             int waitFor = process.waitFor();
             System.out.println("waitFor:: "+waitFor);
             BufferedReader success=new BufferedReader(new InputStreamReader(process.getInputStream()));
             BufferedReader error=new BufferedReader(new InputStreamReader(process.getErrorStream()));

             String s="";
             while ((s = success.readLine()) != null) {
             System.out.println(s);
             }

             while ((s = error.readLine()) != null) {
             System.out.println("Std ERROR : " + s);
             }
             } catch (Exception e) {
             e.printStackTrace();
             }
    }
}

I'm facing java.io.IOException: Cannot run program "mongoexport": CreateProcess error=2, The system cannot find the file specified.

Can anyone please help me out regarding the same ...

Please check the screenshot for STDERR here

1
Are you using Mongo 3.4? Just to improve the syntax of the export command. - notionquest
I'm using Mongo 3.2 - dev999

1 Answers

2
votes

Here is the updated code to suppress the warnings, included the fields that need to be exported (this is mandatory for CSV mode) and added the absolute path to mongoexport.exe.

public static void main(String[] args) {

        String db = "pack";
        String col = "col";
        String Host = "localhost";
        String Port = "27017";
        String fileName = "D:/files/sample.csv";

        String command = "C:\\Program Files\\MongoDB\\Server\\3.4\\bin\\mongoexport.exe --host " + Host + " --port " + Port + " --db " + db + " --collection " + col + " --type=csv --fields _id,email,createdAt, --out " + fileName + "";

        try {
            System.out.println(command);
            Process process = Runtime.getRuntime().exec(command);
            int waitFor = process.waitFor();
            System.out.println("waitFor:: " + waitFor);
            BufferedReader success = new BufferedReader(new InputStreamReader(process.getInputStream()));
            BufferedReader error = new BufferedReader(new InputStreamReader(process.getErrorStream()));

            String s = "";
            while ((s = success.readLine()) != null) {
                System.out.println(s);
            }

            while ((s = error.readLine()) != null) {
                System.out.println("Std ERROR : " + s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Debug Notes:-

In case if you hit up with any problem, please first check whether the command is valid and then try it in the Java program.

Example:-

mongoexport.exe --host localhost --port 27017 --db test --collection Account --csv --out D:/files/sample.csv    

Alternate solution using ProcessBuilder:-

I have set processBuilder.redirectErrorStream(true) to true. So, you will get all the process message in one stream.

public static void main(String[] args) {

        String db = "pack";
        String col = "col";
        String Host = "localhost";
        String Port = "27017";
        String fileName = "D:/files/sample.csv";

    String command = "C:\\Program Files\\MongoDB\\Server\\3.4\\bin\\mongoexport.exe --host " + Host + " --port " + Port + " --db " + db + " --collection " + col + " --type=csv --fields _id,email,createdAt, --out " + fileName + "";

    try {
        System.out.println(command);

        StringTokenizer st = new StringTokenizer(command);
        String[] cmdarray = new String[st.countTokens()];
        for (int i = 0; st.hasMoreTokens(); i++)
            cmdarray[i] = st.nextToken();

        ProcessBuilder processBuilder = new ProcessBuilder(cmdarray);
        processBuilder.redirectErrorStream(true);

        Process process = processBuilder.start();
        BufferedReader processOutput = new BufferedReader(new InputStreamReader(process.getInputStream()));

        String s = "";
        while ((s = processOutput.readLine()) != null) {
            System.out.println(s);
        }


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