【问题标题】:How to use MongoExport command in a java program?如何在 java 程序中使用 MongoExport 命令?
【发布时间】:2017-05-17 19:58:35
【问题描述】:

我可以在 Mongo shell 中使用 Mongoexport 命令导出整个集合。

但是,我正在尝试编写一个 java 程序,它使用 Mongoexport 命令将整个 MongoDB 集合导出到 CSV 文件中。

我的代码:

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();
             }
    }
}

我遇到 java.io.IOException: Cannot run program "mongoexport": CreateProcess error=2, The system cannot find the file specified.

谁能帮我解决同样的问题...

Please check the screenshot for STDERR here

【问题讨论】:

  • 您使用的是 Mongo 3.4 吗?只是为了改进导出命令的语法。
  • 我使用的是 Mongo 3.2

标签: java mongodb


【解决方案1】:

这是抑制警告的更新代码,包括需要导出的字段(这对于 CSV 模式是强制性的)并添加了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();
        }
    }

调试说明:-

如果遇到任何问题,请先检查命令是否有效,然后在Java程序中尝试。

示例:-

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

使用 ProcessBuilder 的替代解决方案:-

我已将 processBuilder.redirectErrorStream(true) 设置为 true。因此,您将在一个流中获取所有进程消息。

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();
    }

【讨论】:

  • 在上述程序中,success.readLine() 为空,因此进入 error.readLine() 块,然后从 while 循环的错误块中导出 csv 文件。我不知道为什么它从错误块导出 csv 文件,而不是从成功块导出。任何人都可以帮助我解决同样的问题...
  • 实际上,无论成功或失败,Java 代码都不会在文件上专门写入任何内容。我们正在执行的命令将数据写入 csv 文件。 while 循环(成功或错误)只是在控制台上显示消息。如果有任何错误,csv应该只有标题记录。
  • 是的,实际上整个数据都写入了一个csv文件。成功导出所有记录。一切都按预期正常工作,但它正在从错误块导出 csv 文件,而不是从成功块导出。这就是我现在面临的问题
  • 你能显示你在控制台中看到的内容,即标准错误:后跟一些字符串吗?
  • 已在上面为 stdError 添加了一个截图。
猜你喜欢
  • 2014-06-28
  • 2011-02-11
  • 1970-01-01
  • 2013-03-28
  • 2011-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-06
相关资源
最近更新 更多