Associate result of bulk get with id

i am using java 3.2 sdk and making async bulk call.

      List<String> docsToFetch = Arrays.asList("airline_112", "airline_1191", "airline_1203");

      List<GetResult> successfulResults = Collections.synchronizedList(new ArrayList<>());
      Map<String, Throwable> erroredResults = new ConcurrentHashMap<>();

      Flux.fromIterable(docsToFetch).flatMap(key -> reactiveCollection.get(key).onErrorResume(e -> {
        erroredResults.put(key, e);
        return Mono.empty();
      })).doOnNext(successfulResults::add).last().block();

as a result i have List, but now how do i associate it back to the id? for example which getResult belongs to airline_1191? Can we fix this on the documentation page?

You could do it exactly like you already do with the erroredResults:

	List<String> docsToFetch = Arrays.asList("airline_112", "airline_1191", "airline_1203");

	Map<String, GetResult> successfulResults = new ConcurrentHashMap<>();
	Map<String, Throwable> erroredResults = new ConcurrentHashMap<>();

	Flux.fromIterable(docsToFetch).flatMap(key -> reactiveCollection.get(key).onErrorResume(e -> {
		erroredResults.put(key, e);
		return Mono.empty();
	}).doOnNext(getResult -> successfulResults.put(key, getResult))).last().block();
1 Like

@helper_bro @mico.piira is spot on. The “trick” is to do the success and error parts inside the flatMap where you have your key instead of chaining it afterwards. That way you have access to it and all good.

Note that we are thinking about adding the Id and other attributes into the Result objects for convenience, so in the future you’ll also be able to do what you tried at first. But then again, the other approach works exactly the same way, just one level nested deeper.

1 Like

@daschl is it possible to update the wiki. for someone who is new to reactive java, it was a night mare to figure out.

@helper_bro well I think we can enhance the docs, but there is a fine line between documenting the SDK and re-documenting reactor. Since technically what was described here has nothing to do with the SDK per se but is how you work with project reactor underneath?