无法将 csv 文件导入 java 的远程位置可用的 mongodb?

Not able to import csv file into mongodb available in Remote location from java?

Runtime r = Runtime.getRuntime();
String cmd = "C:\Program Files (x86)\MongoDB\Server\3.0\bin\mongoimport -d dummydb -c Employee --type csv --file /home/mongodb/one.csv --headerline";
r.exec(cmd);

当我 运行 在 linux 机器上执行相同的命令时,导入了 csv 文件。但是,从 java 独立我无法插入。 你能帮忙解决一下吗?

请在更改集合名称、数据库名称和文件路径后尝试以下代码。它应该可以工作。

注:-

需要提及 mongoimport.exe - 如以下代码所示。

代码:-

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class MongoImportUtil {

    public static void main(String[] args) {

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

        String command = "C:\Program Files\MongoDB\Server\3.4\bin\mongoimport.exe --host " + Host + " --port "
                + Port + " --db " + db + " --collection " + col + " --headerline --type=csv --file " + 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();
        }
    }

}