AdSenseHeader

In this post I am explaining how to use the IN query in Cassandra from java. If you understand this example first you need to know the PreparedStatement and  BoundStatement’s in Cassandra using the JAVA Driver.
         For IN query example I created the usersdata table in Cassandra using CQLSH. I insert the values in the usersdata table.

The fallowing diagram illustrates the data of usersdata table .


           In the above diagram usersdata table user_name is the primary key .  so user_name used in IN query example.

     First create a MAVEN project using eclipse . In that project pom you can add the fallowing dependency
                    
<dependency>
              <groupId>com.datastax.cassandra</groupId>
              <artifactId>cassandra-driver-core</artifactId>
              <version>2.1.3</version>
</dependency>

The fallowing program is explains the IN query in Cassandra from JAVA driver
 --------------------------------------------------------------------------------------------------------------

import java.util.ArrayList;
import java.util.List;
import com.datastax.driver.core.BoundStatement;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;

/**
 * @author Venkata Ramu Kandulapati
 *
 */
public class INStatementEx {
      
       public static Session session = null;
       public static final String KEYSPACE = "sample";
       public static final String TABLE_USERS_DATA = "usersdata";
      
       public static PreparedStatement inQueryPstmt = null;

//     Preparing a IN Query for cassandra
       public static final String SELECT_STMT_USING_IN = "SELECT * FROM   "+KEYSPACE+"."+TABLE_USERS_DATA+" WHERE user_name  IN :usersList ";
      
       public INStatementEx() {
              String serverIp ="127.0.0.1";
              Cluster cluster = Cluster.builder()
                           .addContactPoints(serverIp)
                           .build();
              session = cluster.connect(KEYSPACE);
              inQueryPstmt = session.prepare(SELECT_STMT_USING_IN);
       }
      
       public void useINQueryMethod() throws Exception{
              try {
                     BoundStatement inQueryBoundStmt = new BoundStatement(inQueryPstmt);
                    
                     List<String> usersList = new ArrayList<String>();
                     usersList.add("Ramu");
                     usersList.add("Raju");
                    
                     inQueryBoundStmt.setList("usersList", usersList);
                     ResultSet rs = session.execute(inQueryBoundStmt);
                     if(rs != null) {
                           List<Row> usersRows = rs.all();
                           System.out.println(usersRows);
                     }
              } catch(Exception e) {
                     throw e;
              }
       }
      
       public static void main(String args[])throws Exception {
              try {
                     INStatementEx ins = new INStatementEx();
                     ins.useINQueryMethod();
              } catch(Exception e) {
                     System.out.println("Exception in IN query"+e);
              }
       }
}


                                  In the above program first prepare a inQueryPstmt PreparedStatement for IN Statement.That PreparedStatement is initialized in BoundStatement.Then after which values you want to use in IN query that values prepared as a list that list is set to  BoundStatement.Then execute the BoundStatement .


0 comments:

Post a Comment

 
Top