2015-06-12 33 views
1

我有一個java應用程序,我只是在讀取文件並將數據插入到elasticsearch索引中。Elasticsearch REST api通過curl不能從java.runtime.exec工作

這是我正在使用的代碼。

 in = new BufferedReader(new FileReader("file.txt")); 
     line = in.readLine(); 
     while ((line = in.readLine()) != null) { 
      System.out.println("Command: curl -XPOST 'http://localhost:9200/esensor/data' -d '{ \"temp\": \""+line+"\" }'"); 
      p = Runtime.getRuntime().exec("curl -XPOST 'http://localhost:9200/esensor/data' -d '{ \"temp\": \""+line+"\" }'"); 
      p.waitFor(); 

      BufferedReader err = new BufferedReader(new InputStreamReader(p.getErrorStream())); 
      while ((line = err.readLine()) != null) { 
       System.out.println(line); 
      } 
      break; 
     } 
     in.close(); 

但這不起作用。捲毛是給這個錯誤。

curl: (1) Protocol 'http not supported or disabled in libcurl 
    % Total % Received % Xferd Average Speed Time Time  Time Current 
           Dload Upload Total Spent Left Speed 

    0  0 0  0 0  0  0  0 --:--:-- --:--:-- --:--:--  0curl: (6) Could not resolve host: "temp" 

    0  0 0  0 0  0  0  0 --:--:-- --:--:-- --:--:--  0curl: (6) Could not resolve host: "17.35757" 
curl: (3) [globbing] unmatched close brace/bracket in column 1 

但看到我打印了正在執行的命令。如果我手動複製在控制檯粘貼,然後一切工作正常。爲什麼會發生?我做錯了什麼?

+0

爲什麼不使用elasticsearch的客戶端? [ElasticSearch客戶端](https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/client.html) – chengpohi

+0

相關:http://stackoverflow.com/questions/16486649/shell- bash-brace-expansion-with-javas-runtime-exec –

+0

@RC它與我的問題有何關係? –

回答

0

的關鍵是錯誤消息從捲曲:

curl: (1) Protocol 'http not supported or disabled in libcurl 

協議名稱前出現的單引號表明,捲曲獲取作爲參數的一部分進行傳遞,因而它不工作的方式你想要它,而不是給捲曲一個格式不正確的URL。

+0

您能否給我正確的命令,我必須在runtime.exec中發送?請我找不到任何工作。 –

+0

Runtime.getRuntime().exe顯然不支持常規shell風格的引用。你最好仔細閱讀細節。我不知道,我只是在解釋說什麼捲曲...... –