如何使用java上传elasticsearch中的json数据或文件?

4smxwvx5  于 2021-07-13  发布在  ElasticSearch
关注(0)|答案(2)|浏览(524)

这是我的示例json数据,来自.json文件,现在我想动态批量插入elasticsearch,以便我可以对其执行操作..有人能帮我用java代码动态添加此数据..这只是一块5-6个对象,像这样我有500多个对象

[{

"data1" : "developer",
"data2" : "categorypos",
"data3" : "1001"
},
{

"data1" : "developer",
"data1" : "developerpos",
"data1" : "1002"
},
{

"data1" : "developer",
"data2" : "developpos",
"data3" : "1003"
},
{

"data1" : "support",
"data2" : "datapos",
"data3" : "1004"
}
]
2lpgd968

2lpgd9681#

在ElasticSearch中提供了批量操作,下面是可能有帮助的文档https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html

pxy2qtax

pxy2qtax2#

您需要从应用程序中读取文件,遍历数组,并将每个文档发送到elasticsearch。
要执行最新的操作,应该使用bulk processor类。

BulkProcessor bulkProcessor = BulkProcessor.builder(
            (request, bulkListener) -> esClient.bulkAsync(request, RequestOptions.DEFAULT, bulkListener),
            new BulkProcessor.Listener() {
                @Override
                public void beforeBulk(long executionId, BulkRequest request) { }
                @Override
                public void afterBulk(long executionId, BulkRequest request, BulkResponse response) { }
                @Override
                public void afterBulk(long executionId, BulkRequest request, Throwable failure) { }
            })
            .setBulkActions(10000)
            .setFlushInterval(TimeValue.timeValueSeconds(5))
            .build();

对于每个 json 文件,电话:

bulkProcessor.add(new IndexRequest("INDEXNAME").source(json, XContentType.JSON));

相关问题