I've got this requirement to iterate over millions of records returned in resultset and process them and store them in some data structure. I have not been able to find any relevant example or reference for the same. JOOQ seems to be doing something that I may want but it seems it is not free. I was hoping if using java 8 streams I might be able to achieve it, but not example or writeup seems to give me any directions to head towards. I am open to other alternatives as well.
Based on this SO reference: resultset parallel I did attempted below but it did not give me any performance improvement as could be seen below in the performance metrics.
CODE:
Sequential Iteration:
while(rs.next()) {
System.out.println(rs.getString(1));
}
Using streams and spliterator:
Stream<String> s = StreamSupport.stream(new Spliterators.AbstractSpliterator<String>(Long.MAX_VALUE,
Spliterator.ORDERED) {
@Override
public boolean tryAdvance(Consumer<? super String> action) {
try {
if (!rs.next())
return false;
action.accept(rs.getString(1));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
}, true);
s.forEach(System.out::println);
Total number of records: 3759
Time taken by Sequential: ~ 83.8 secs
Time taken by Streams: ~ 83.5 secs
Can anyone review this and tell me if I've not implemented streams correctly.