據我所知,Elasticsearch沒有定義Ingest Pipeline的JAVA API。 作爲變通,您可以執行以下操作使用HttpURLConnection的定義從JAVA攝取管道:
URL obj = new URL("http://localhost:9200/_ingest/pipeline/timestamp");
String json = "{\n" +
" \"description\": \"describe pipeline\",\n" +
" \"processors\": [\n" +
" {\n" +
" \"set\": {\n" +
" \"field\": \"timestamp\",\n" +
" \"value\": \"{{_ingest.timestamp}}\"\n" +
" }\n" +
" }\n" +
" ]\n" +
"}";
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("PUT");
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream());
osw.write(json);
osw.flush();
osw.close();
System.out.println(con.getResponseCode() + " : " + con.getResponseMessage());
if (con != null)
con.disconnect();
而現在,下面是用於索引文檔中的Java代碼,通過將它們通過創建「時間戳」攝取管道:
TransportClient client = buildTransPortClient();
Map<String, Object> object = new HashMap<String, Object>();
object.put("user","kimchy");
object.put("postDate",new Date());
object.put("message","trying out Elasticsearch");
IndexResponse response = client.prepareIndex("test", "test", "100")
.setSource(object)
.setPipeline("timestamp")
.get();
System.out.println(response);
根據我的理解,你已經定義了你的Ingest Pipeline「timestamp」。現在,您只需要通過在每次「時間戳」Pipeline中傳遞索引文檔來使用JAVA API。對? –
謝謝您的回覆!不完全的。我們想將整個代碼翻譯成java。包括管道的定義。 – Tameem
但是,爲什麼你需要每次定義管道定義? 它是一種元數據信息或映射,需要在數據插入或操作之前定義一次。 –