Java - Converting Cassandra ResultSet to Java 8 Stream

less than 1 minute read

1
2
3
4
5
6
7
8
Statement stmt = QueryBuilder().select().all().from('table');
ResultSet rs = session.execute(stmt);
StreamSupport.stream(
    Spliterators.spliteratorUnknownSize(
                rs.iterator(), Spliterator.ORDERED), false)
.forEach(row -> {
   ...
});

As input stream is sorted by partition key and input order is important, the following example can be used.

1
2
3
4
5
6
7
8
Statement stmt = QueryBuilder().select().all().from('table');
ResultSet rs = session.execute(stmt);
StreamSupport.stream(
    Spliterators.spliteratorUnknownSize(
                rs.iterator(), Spliterator.ORDERED | Spliterator.SORTED), false)
.forEach(row -> {
   ...
});