java - 如何在java程序中使用MongoExport命令?

标签 java mongodb

我可以在 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:无法运行程序“mongoexport”:CreateProcess error=2,系统找不到指定的文件。

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

Please check the screenshot for STDERR here

最佳答案

此处是用于抑制警告的更新代码,包括需要导出的字段(这对于 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();
    }

关于java - 如何在java程序中使用MongoExport命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43979638/

相关文章:

java - 在 gradle javadoc build 中排除 protected 方法

php - UTCDateTime::toDateTime() 方法返回 1970 日期时间

c# - C# MongoClient 是否可以在不首先序列化为 .NET 类型的情况下用于返回有效的 JSON?

javascript - 从 mongodb 作为文档检索对象类型后如何获取对象类型?

java - RxJava : "java.lang.IllegalStateException: Only one subscriber allowed!"

java - 指定任务可执行位置 "C:\Program Files (x86)\Java\jdk1.7.0_71\\bin\javac.exe"无效

java - 每当事件发生时触发 Spark 作业

Java 编译错误 : Method reference in combination with overloading

php - 避免内存限制在 MongoGridFSFile::getBytes PHP 中

node.js - 如何将 geoNear 与 mongoose 一起应用并获取两个坐标之间的距离