3
votes

I am using Cassandra 1.2.5 and Secondary Index. When I run prepared statement no data returned. I have data. Also for the index column I do have duplicate values. What I am doing is retruning a list of video_id based on a user_id. The tables Describe looks like this: [default@video] describe videos;

WARNING: CQL3 tables are intentionally omitted from 'describe' output. See https://issues.apache.org/jira/browse/CASSANDRA-4377 for details.

ColumnFamily: videos
  Key Validation Class: org.apache.cassandra.db.marshal.IntegerType
  Default column value validator: org.apache.cassandra.db.marshal.IntegerType
  Columns sorted by: org.apache.cassandra.db.marshal.UTF8Type
  GC grace seconds: 864000
  Compaction min/max thresholds: 4/32
  Read repair chance: 0.1
  DC Local Read repair chance: 0.0
  Populate IO Cache on flush: false
  Replicate on write: true
  Caching: ALL
  Bloom Filter FP chance: default
  Built indexes: [videos.videos_user_id_idx]
  Column Metadata:
    Column Name: video_id
      Validation Class: org.apache.cassandra.db.marshal.IntegerType
    Column Name: user_id
      Validation Class: org.apache.cassandra.db.marshal.IntegerType
      Index Name: videos_user_id_idx
      Index Type: KEYS
  Compaction Strategy: org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy
  Compression Options:
    sstable_compression: org.apache.cassandra.io.compress.SnappyCompressor

My code looks like this: int concurrency = 3; //final BoundStatement query = null; try { // Create session to hosts Cluster cluster = new Cluster.Builder().addContactPoints(String.valueOf("localhost")).build();

           // final int maxRequestsPerConnection = 10;
          //  int maxConnections = concurrency / maxRequestsPerConnection + 1;

            int maxConnections = 3;
            PoolingOptions pools = cluster.getConfiguration().getPoolingOptions();
            pools.setMaxSimultaneousRequestsPerConnectionThreshold(HostDistance.LOCAL, concurrency);
            pools.setCoreConnectionsPerHost(HostDistance.LOCAL, maxConnections);
            pools.setMaxConnectionsPerHost(HostDistance.LOCAL, maxConnections);
            pools.setCoreConnectionsPerHost(HostDistance.REMOTE, maxConnections);
            pools.setMaxConnectionsPerHost(HostDistance.REMOTE, maxConnections);


            Session session = cluster.connect();


            //get list of video ids
            String cql1 = "SELECT video_id from video.videos WHERE user_id=?";
            com.datastax.driver.core.PreparedStatement stmt = session.prepare(cql1);
            BoundStatement b = stmt.bind();
            BigInteger i = BigInteger.valueOf(9);
            b.setVarint("user_id",i);
            long start, end;                
            start = System.nanoTime();
            com.datastax.driver.core.ResultSet rs1 = session.execute(b);
            end = System.nanoTime();
            System.out.println("Datastax driver CQL Query prepared overall time ns:"
                    + (end - start));

            while(rs1.iterator().hasNext()) {
                 System.out.println("user_id:" + rs1.iterator().next().getVarint("video_id"));
           }

Note even if I change the statement to replace the ? with a value of 9 I still get no rows back.

Any Ideas what I am doing wrong??

Thanks, -Tony

1

1 Answers

1
votes

Try this code to retrieve data:

Cluster cluster = Cluster.builder()
                                 .addContactPoint("127.0.0.1")
                              // .addContactPoint("some.other.ip")
                                 .build();
Session session = cluster.connect();

String statement = "SELECT * FROM pixel.user;";
// String statement = "SELECT video_id from video.videos WHERE user_id=9";

session.execute(statement);
ResultSet rs = session.execute(statement);
for(Row r : rs.all())
    System.out.println(r.toString());

Once you got the basics down, its time for the bound statement:

int user_id = 9;
String statement = "SELECT * from video.videos WHERE user_id=?";
PreparedStatement pStatement = session.prepare(statement);
BoundStatement boundStatement = new BoundStatement(pStatement);

PreparedStatement ps = session.prepare(statement);
BoundStatement bs = ps.bind();
bs.bind(user_id); // a csv list: bs.bind(9, "string val of second ?, etc...");

// session.execute(bs);
ResultSet rs = session.execute(bs);