I want to load larget row of data, so my plan is divide the statement to parts, divided by timestamp, and than run it asynchronously.
...
// List to save ResultSets
List<CompletableFuture<AsyncResultSet>> pending = new ArrayList<>();
for(Range range : ranges) {
System.out.println("Asynchronous execute query will be called soon!");
pending.add(executeQuery(session, preparedStatement, range));
}
...
private static CompletableFuture<AsyncResultSet> executeQuery(CqlSession session,
PreparedStatement preparedStatement, Range range) {
return session
.executeAsync(preparedStatement.bind()
.setInstant("startDateTime", range.getStartDateTime().toInstant())
.setInstant("endDateTime", range.getEndDateTime().toInstant())
.setPageSize(1000000))
.toCompletableFuture()
.whenCompleteAsync((asyncResultSet, throwable) -> {
if (throwable == null) {
System.out.println("Range " + range.getStart() + " to " + range.getEnd() +
" has " + asyncResultSet.remaining() + " records.");
fetchResultSet(asyncResultSet, throwable);
if(asyncResultSet.hasMorePages()) {
asyncResultSet.fetchNextPage().whenComplete(LoadCassandraAsync::fetchResultSet);
}
} else {
throwable.printStackTrace();
}
}, Executors.newFixedThreadPool(4))
.exceptionally(throwable -> {
throwable.printStackTrace();
return null;
});
}
I will get randomly exit code 0 (not from main method), indicated it closed. Or, I will get nothing after some fetching, just like there is a thread running but does not do anything.
If I commented "row fetching" part, I got:
...
Asynchronous execute query will be called soon!
Asynchronous execute query will be called soon!
Asynchronous execute query will be called soon!
Asynchronous execute query will be called soon!
Range 2020-02-14 00:00:00+0700 to 2020-02-14 01:00:00+0700 has 102974 records.
Range 2020-02-14 01:00:00+0700 to 2020-02-14 02:00:00+0700 has 98201 records.
Range 2020-02-14 06:00:00+0700 to 2020-02-14 07:00:00+0700 has 104529 records.
Range 2020-02-14 08:00:00+0700 to 2020-02-14 09:00:00+0700 has 105257 records.
...
I think it means the executeQuery()
method worked well.
What I did incorrectly?