简体   繁体   English

如何通过在Java中的后请求中发送文件将数据导入Elasticsearch?

[英]How can I import data into elasticsearch by sending a file in post request in java?

Currently I am using following cmd to import data into elasticsearch 目前我正在使用以下cmd将数据导入elasticsearch

curl -XPOST 'localhost:9200/index/_bulk?pretty' --data-binary @required.json

ElasticSearch Bulk API ElasticSearch批量API

Now I am creating a java console application and I need to send this file in a post request. 现在,我正在创建一个Java控制台应用程序,我需要在发布请求中发送此文件。 I am able to do this using SoapUI ie attaching a file with header 我可以使用SoapUI来执行此操作,即使用头文件附加文件

Content-Type: text/javascript 内容类型:文本/ javascript

Doing this request from SOAPUI is successful and data is loaded. 来自SOAPUI的此请求成功完成,并且已加载数据。


I have looked into how to do a post request and I have got to following code. 我已经研究了如何进行发布请求,并且必须遵循以下代码。 So my questions are : 所以我的问题是:

  1. How would we attach a file to the request ? 我们如何将文件附加到请求?
  2. If file attachment is not possible, what is other possible solution ? 如果无法附加文件,还有什么其他可能的解决方案?

     URL url = new URL("http://localhost:9200/index/_bulk/"); HttpURLConnection httpCon = (HttpURLConnection) url.openConnection(); httpCon.setDoOutput(true); httpCon.setRequestMethod("POST"); httpCon.setRequestProperty("Content-Type", "text/javascript"); // (or text/plain) httpCon.setRequestProperty("Accept", "application/json"); OutputStreamWriter out = new OutputStreamWriter(httpCon.getOutputStream()); out.write(...filedata...); // <------ How to Put file data in output stream ? out.flush(); out.close(); 

How to write file contents to output 如何将文件内容写入输出

//out.write(...filedata...); // <------ How to Put file data in output stream ?

// Create a path to your file
Path path = Paths.get("D:/TEMP/bulk.json"); // <------ your file name

// Open a BufferedReader to your file - change CharSet if necessary
BufferedReader fReader = Files.newBufferedReader(path, Charset.defaultCharset());

String data = null;
// Read each line, append "\n" to it as required by Bulk API and write it to the server
while ((data = fReader.readLine()) != null) {
    out.write(data + "\n"); // <------ Put each line in output stream
    out.flush();            // <------ You may do this outside the loop once
}

out.close();

Note: There are multiple ways of writing file contents to an outputstream. 注意: 有多种方法可以将文件内容写入输出流。 I have just specified one. 我刚刚指定了一个。 You may explore others. 您可以探索其他人。

bulk.json bulk.json

{ "index" : { "_index" : "test", "_type" : "string", "_id" : "10" } }
{ "field1" : "value10" }
{ "index" : { "_index" : "test", "_type" : "string", "_id" : "20" } }
{ "field1" : "value20" }
{ "index" : { "_index" : "test", "_type" : "string", "_id" : "30" } }
{ "field1" : "value30" }

Test 测试

http://localhost:9200/test/string/10
{"_index":"test","_type":"string","_id":"10","_version":3,"found":true,"_source":{ "field1" : "value10" }}

Also, since your client is Java, have you considered using the ElasticSearch Java Client API? 另外,由于您的客户端是Java,您是否考虑过使用ElasticSearch Java客户端API? I don't know it well but it should support Bulk requests. 我不太清楚,但它应该支持批量请求。 Look at the client.bulk(BulkRequest) method. 查看client.bulk(BulkRequest)方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM