我可以在 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,系统找不到指定的文件。
谁能帮我解决同样的问题......
最佳答案
此处是用于抑制警告的更新代码,包括需要导出的字段(这对于 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/