gpt4 book ai didi

java - Reactive Spring Boot API 包装了 Elasticsearch 的异步批量索引

转载 作者:行者123 更新时间:2023-12-02 22:54:09 24 4
gpt4 key购买 nike

我正在为一个新项目开发原型(prototype)。这个想法是为 Elasticsearch 中的批量索引文档提供响应式 Spring Boot 微服务。 Elasticsearch 提供了一个高级 Rest 客户端,它提供了一种异步方法来批量处理索引请求。提到了使用监听器的异步传递回调 here .回调分批接收索引响应(每个请求)。我正在尝试将此响应作为 Flux 发送回客户端。我想出了一些基于 this blog post 的东西.

Controller

@RestController
public class AppController {

@SuppressWarnings("unchecked")
@RequestMapping(value = "/test3", method = RequestMethod.GET)
public Flux<String> index3() {
ElasticAdapter es = new ElasticAdapter();
JSONObject json = new JSONObject();
json.put("TestDoc", "Stack123");
Flux<String> fluxResponse = es.bulkIndex(json);
return fluxResponse;
}

弹性适配器
@Component
class ElasticAdapter {
String indexName = "test2";
private final RestHighLevelClient client;
private final ObjectMapper mapper;
private int processed = 1;

Flux<String> bulkIndex(JSONObject doc) {
return bulkIndexDoc(doc)
.doOnError(e -> System.out.print("Unable to index {}" + doc+ e));
}

private Flux<String> bulkIndexDoc(JSONObject doc) {
return Flux.create(sink -> {
try {
doBulkIndex(doc, bulkListenerToSink(sink));
} catch (JsonProcessingException e) {
sink.error(e);
}
});
}


private void doBulkIndex(JSONObject doc, BulkProcessor.Listener listener) throws JsonProcessingException {

System.out.println("Going to submit index request");
BiConsumer<BulkRequest, ActionListener<BulkResponse>> bulkConsumer =
(request, bulkListener) ->
client.bulkAsync(request, RequestOptions.DEFAULT, bulkListener);
BulkProcessor.Builder builder =
BulkProcessor.builder(bulkConsumer, listener);
builder.setBulkActions(10);
BulkProcessor bulkProcessor = builder.build();
// Submitting 5,000 index requests ( repeating same JSON)
for (int i = 0; i < 5000; i++) {
IndexRequest indexRequest = new IndexRequest(indexName, "person", i+1+"");
String json = doc.toJSONString();
indexRequest.source(json, XContentType.JSON);
bulkProcessor.add(indexRequest);
}
System.out.println("Submitted all docs
}


private BulkProcessor.Listener bulkListenerToSink(FluxSink<String> sink) {
return new BulkProcessor.Listener() {

@Override
public void beforeBulk(long executionId, BulkRequest request) {
}

@SuppressWarnings("unchecked")
@Override
public void afterBulk(long executionId, BulkRequest request, BulkResponse response) {

for (BulkItemResponse bulkItemResponse : response) {
JSONObject json = new JSONObject();
json.put("id", bulkItemResponse.getResponse().getId());
json.put("status", bulkItemResponse.getResponse().getResult

sink.next(json.toJSONString());
processed++;
}
if(processed >= 5000) {
sink.complete();
}
}

@Override
public void afterBulk(long executionId, BulkRequest request, Throwable failure) {
failure.printStackTrace();
sink.error(failure);
}
};
}


public ElasticAdapter() {
// Logic to initialize Elasticsearch Rest Client
}
}

我使用 FluxSink 创建响应的 Flux 以发送回客户端。在这一点上,我不知道这是否正确。

我的期望是调用客户端应该以 10 个批处理接收响应(因为批量处理器以 10 个批处理处理它 - builder.setBulkActions(10); )。我尝试使用 Spring Webflix Client 使用端点。但无法解决。这是我尝试过的

网络客户端
public class FluxClient {

public static void main(String[] args) {
WebClient client = WebClient.create("http://localhost:8080");
Flux<String> responseFlux = client.get()
.uri("/test3")
.retrieve()
.bodyToFlux(String.class);
responseFlux.subscribe(System.out::println);
}
}

正如我所料,控制台上没有打印任何内容。我尝试使用 System.out.println(responseFlux.blockFirst()); .它在最后将所有响应打印为一个批处理,而不是在 .

如果我的方法是正确的,那么正确的消费方法是什么?对于我心目中的解决方案,这个客户端将驻留在另一个 Web 应用程序中。

注:我对 Reactor API 的理解有限。使用的 elasticsearch 版本是 6.8。

最佳答案

因此,对您的代码进行了以下更改。

在 ElasticAdapter 中,

public Flux<Object> bulkIndex(JSONObject doc) {
return bulkIndexDoc(doc)
.subscribeOn(Schedulers.elastic(), true)
.doOnError(e -> System.out.print("Unable to index {}" + doc+ e));
}

在 Flux 上调用 subscribeOn(Scheduler, requestOnSeparateThread),从 ​​ https://github.com/spring-projects/spring-framework/issues/21507 了解它

在 FluxClient 中,
Flux<String> responseFlux = client.get()
.uri("/test3")
.headers(httpHeaders -> {
httpHeaders.set("Accept", "text/event-stream");
})
.retrieve()
.bodyToFlux(String.class);
responseFlux.delayElements(Duration.ofSeconds(1)).subscribe(System.out::println);

添加了“Accept” header 作为“text/event-stream”并延迟了 Flux 元素。

通过上述更改,能够从服务器获得实时响应。

关于java - Reactive Spring Boot API 包装了 Elasticsearch 的异步批量索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60981954/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com