2017-05-15 1 views
1

MongoシェルでMongoexportコマンドを使用してコレクション全体をエクスポートできます。JavaプログラムでMongoExportコマンドを使用するには?

しかし、私はMongoexportコマンドを使ってMongoDBのコレクション全体をCSVファイルにエクスポートするJavaプログラムを作成しようとしています。

マイコード:私はにjava.io.IOExceptionが直面しているよ

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(); 
      } 
    } 
} 

:プログラムを実行することはできません "mongoexport":CreateProcessをエラー= 2、システムは指定されたファイルを見つけることができません。

誰が

Please check the screenshot for STDERR here

+0

Mongo 3.4をお使いですか?エクスポートコマンドの構文を改善するだけです。 – notionquest

+0

私はMongoを使用しています。3.2 – dev999

答えて

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プログラムでそれを試してみてください。

例: - ProcessBuilderをを使用して

mongoexport.exe --host localhost --port 27017 --db test --collection Account --csv --out D:/files/sample.csv  

代替ソリューション: -

私がtrueにprocessBuilder.redirectErrorStream(true)を設定しています。したがって、すべてのプロセスメッセージを1つのストリームで取得できます。

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(); 
    } 
+0

ありがとうございます。それは私のために働いた... – dev999

+0

上記のプログラムでは、success.readLine()はnullを取得しているので、error.readLine()ブロックに入り、whileループのエラーブロックからcsvファイルをエクスポートしています。なぜ私はそれがエラーブロックからCSVファイルをエクスポートしているが、成功ブロックからエクスポートされているのかわかりません。どんな人も同じことについて私を助けることができます... – dev999

+0

実際には、Javaコードは、成功または失敗に関係なく、ファイルに特に何も書き込んでいません。実行中のコマンドは、csvファイルにデータを書き込みます。 whileループ(成功またはエラー)は、コンソールにメッセージを表示するだけです。エラーがあれば、csvはヘッダレコードのみを持つ必要があります。 – notionquest

関連する問題