I am querying all my 10 tables to get the user id from them and loading all the user id's into HashSet so that I can have unique user id.
As of now it is sequentially. We go to one table and extract all the user_id from it and load it in hash set and then second and third table and keep going.
private Set<String> getRandomUsers() {
Set<String> userList = new HashSet<String>();
// is there any way to make this parallel?
for (int table = 0; table < 10; table++) {
String sql = "select * from testkeyspace.test_table_" + table + ";";
try {
SimpleStatement query = new SimpleStatement(sql);
query.setConsistencyLevel(ConsistencyLevel.QUORUM);
ResultSet res = session.execute(query);
Iterator<Row> rows = res.iterator();
while (rows.hasNext()) {
Row r = rows.next();
String user_id = r.getString("user_id");
userList.add(user_id);
}
} catch (Exception e) {
System.out.println("error= " + ExceptionUtils.getStackTrace(e));
}
}
return userList;
}
Is there any way to make this multithreaded so that for each table they get the data from my table in parallel? At the end, I need userList hashset which should have all the unique user id from all the 10 tables.
I am working with Cassandra database and connection is made only once so I don't need to create multiple connections.