簡體   English   中英

無法從Java的“遠程位置”將csv文件導入到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();
        }
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM