Example to question in title:

List<UUID> ids = dao1.getContactsOfUser(userId);
List<String> contact_names = ids.stream
    .map(uid -> dao2.getContactByUid(uid)) //is it ok? 
    .filter(contact -> contact.getPhone!=null)
    .map(contact -> contact.getFullName())

I know that it works, and even works fine, but is there no snags?


Is it OK to do IO inside java 8 streams? Depends on what you mean by OK.

»OK« in the sense of does it work: yes why shouldn't it.

»OK« in the sense of should I: No!

That is not encouraged. It doesn't make sense,retrieving first a user with a call to the database and then retrive contacts with n more requests simply because you wanted the name of the contact, when one request will do?

Enhance your query in joining in the contacts' name and build an aggregate column, which contains the information needed.

| improve this answer | |
  • Firstly, in is just example. Secondly - what if there is no JOIN's? I.e. what if it is dao to Cassandra? – Ivan Zelenskyy Feb 26 '16 at 10:05
  • The problem stays the same: minimize the requests. Perhaps this helps: datastax.com/2015/03/… or spark.apache.org/sql – Thomas Junk Feb 26 '16 at 10:08
  • As I understand relationship between Cassandra and Spark your way doesn't minimize requests, only wrap them in one request to Spark, but Spark anyway does exactly same set of requests to Cassandra. – Ivan Zelenskyy Feb 26 '16 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.