[英]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();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.