我可以在Mongo shell中使用 Mongoexport 命令导出整个集合。
但是,我正在尝试编写一个使用Mongoexport命令将整个MongoDB集合导出到CSV文件的java程序。
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,系统无法找到指定的文件。
任何人都可以帮我解决同样的事情......
答案 0 :(得分: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();
}