Source:
public void validate(String sourceTableName, String targetTableName, int threadsCount) { Session session = null; Session targetSession = null; ExecutorService executorService = null; try { int progressCounter = 0; executorService = Executors.newFixedThreadPool(threadsCount); session = sourceCassandraConnectionProvider.getSession(); List<String> primaryKeys = getPrimaryKeysNames(sourceTableName, session); ResultSet rs = getPrimaryKeysFromSourceTable(sourceTableName, session, primaryKeys); Iterator<Row> iterator = rs.iterator(); targetSession = targetCassandraConnectionProvider.getSession(); BoundStatement matchingBoundStatement = prepareBoundStatementForMatchingTargetTable(targetTableName, targetSession, primaryKeys); while (iterator.hasNext()) { Row row = iterator.next(); Callable<Long> callable = new RowValidatorJob(targetSession, primaryKeys, matchingBoundStatement, row); Future<Long> future = executorService.submit(callable); future.get(); progressCounter++; if (progressCounter % PROGRESS_COUNTER == 0) System.out.println("The data is matching properly till now and the progress will continue for source table " + sourceTableName + " and target table " + targetTableName + " ...."); } } catch (Exception e) { e.printStackTrace(); } finally { if (session != null) session.close(); if (targetSession != null) targetSession.close(); sourceCassandraConnectionProvider.closeConnections(); targetCassandraConnectionProvider.closeConnections(); if (executorService != null) executorService.shutdown(); } }

Target:
public void validate(String sourceTableName, String targetTableName, int threadsCount) { Session targetSession = null; ExecutorService executorService = null; try { long progressCounter = 0l; executorService = Executors.newFixedThreadPool(threadsCount); List<String> primaryKeys = CassandraHelper.getPrimaryKeysNames(sourceCassandraConnectionProvider, sourceTableName, SELECT_COLUMN_NAMES); ResultSet rs = CassandraHelper.getPrimaryKeysFromSourceTable(sourceCassandraConnectionProvider, sourceTableName, primaryKeys); Iterator<Row> iterator = rs.iterator(); targetSession = targetCassandraConnectionProvider.getSession(); BoundStatement matchingBoundStatement = CassandraHelper.prepareBoundStatementForMatchingTargetTable(targetCassandraConnectionProvider, targetTableName, primaryKeys); List<Row> rows = new ArrayList<>(); List<Future> futures = new ArrayList<>(); while (iterator.hasNext()) { Row row = iterator.next(); rows.add(row); progressCounter++; if (progressCounter % PROGRESS_COUNTER == 0) { Callable<Void> callable = new RowsValidatorJob(targetSession, primaryKeys, matchingBoundStatement, rows); Future<Void> future = executorService.submit(callable); futures.add(future); if (futures.size() == threadsCount) { waiteForJobsToFinish(sourceTableName, targetTableName, progressCounter, futures); futures = new ArrayList<>(); } rows = new ArrayList<>(); } } if (!futures.isEmpty()) waiteForJobsToFinish(sourceTableName, targetTableName, progressCounter, futures); if (!rows.isEmpty()) { executeTheRowsJob(targetSession, executorService, primaryKeys, matchingBoundStatement, rows); } LOGGER.info("The data For for source table " + sourceTableName + " and target table " + targetTableName + " was validated correctly!"); } catch (Exception e) { LOGGER.error("ERROR happened: " + e.getMessage() + " and The data for source table " + sourceTableName + " and target table " + targetTableName + " was NOT validated properly!"); } finally { if (targetSession != null) targetSession.close(); sourceCassandraConnectionProvider.closeConnections(); targetCassandraConnectionProvider.closeConnections(); if (executorService != null) executorService.shutdown(); } }