Application connectivity with Cassandra

In this article we will learn Basic access to Cassandra with code such that how to set up development environment and how to use code to perform CQL statements.

In case of high availability and scalability Cassandra is always the best choice to connect your application with Cassandra database.
let’s have a look.


Figure – Application connectivity with Cassandra

To connect with cassandra there are many Driver available. few are listed below.

In programming language to connect application with database there is a programming Pattern.
Three Easy steps are following :



  1. Create a connection (which is called a Session)
  2. Use the session to execute the query.
  3. Be sure to close the connection/session.

Let’s understand with example one by one.

In Java programming language to connect application with Cassandra Database using Cloud used the following steps:

  • Step-1:
    To create the session used the following Java code.

    try (DseSession session = DseSession.builder()
                .withCloudSecureConnectBundle
                  ("/path/to/secure-connect-database_name.zip")
                 // Database Credentials
                .withAuthCredentials("DBUserName", "DBPassword") 
                .build()) { 
  • Step-2:
    To execute the CQL used the following Java code.

    session.execute(
        SimpleStatement.builder("SELECT password 
                                 FROM keyspace-name.Table-name 
                                 WHERE email = ?")
            .addPossitionalValues("name@datastax.com")
            .build()); 
  • Step-3:
    To close the Session used the following Java code.

    // Close happens automatically here 
    // - otherwise use session.close()
    session.close() 

In Python programming language to connect application with Cassandra Database using Cloud used the following steps:

  • Step-1:
    To create the session used the following Python code.

    cluster = Cluster(
              cloud = {'secure_connection_bundle' 
                          : '/path / to / secure-connect-database_name.zip'},
              auth_provider = PlainTextAuthProvider('DBUsername', 'DBPassword'))        
               # Database Credentials
             session = cluster.connect() 
  • Step-2:
    To execute the CQL used the following Pyhton code.

    session.execute(("SELECT password 
                      FROM keyspace-name.Table-name 
                      WHERE email = % s, ('name@datastax.com')) 
  • Step-3:
    To close the Session used the following Python code.

    session.shutdown() 

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.