I have a Cassandra table like this -

    create table user_record (user_id text, record_name text, record_value
blob, primary key (user_id, record_name));

What is the best way to extract all the user_id from this table? As of now,
I cannot change my data model to do this exercise so I need to find a way
by which I can extract all the user_id from the above table.

I am using Datastax Java driver in my project. Is there any other easy way
apart from code to extract all the user_id from the above table through
come cqlsh utility and dump it into some file?

I am thinking below code might timed out after some time -

    public class TestCassandra {

            private Session session = null;
            private Cluster cluster = null;

            private static class ConnectionHolder {
                static final TestCassandra connection = new TestCassandra();
            }

            public static TestCassandra getInstance() {
                return ConnectionHolder.connection;
            }

            private TestCassandra() {
                Builder builder = Cluster.builder();
                builder.addContactPoints("127.0.0.1");

                PoolingOptions opts = new PoolingOptions();
                opts.setCoreConnectionsPerHost(HostDistance.LOCAL,
opts.getCoreConnectionsPerHost(HostDistance.LOCAL));

                cluster =
builder.withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE).withPoolingOptions(opts)
                        .withLoadBalancingPolicy(new TokenAwarePolicy(new
DCAwareRoundRobinPolicy("PI")))
                        .withReconnectionPolicy(new
ConstantReconnectionPolicy(100L))
                        .build();
                session = cluster.connect();
            }

        private Set<String> getRandomUsers() {
            Set<String> userList = new HashSet<String>();

            String sql = "select user_id from testkeyspace.user_record;";

            try {
                SimpleStatement query = new SimpleStatement(sql);
                query.setConsistencyLevel(ConsistencyLevel.ONE);
                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= " + e);
            }

            return userList;
        }
    }

Adding java-driver group and Cassandra group as well to see whether there
is any better way to execute this?

Reply via email to