Skip to content
Related Articles

Related Articles

Improve Article

Application connectivity with Cassandra

  • Last Updated : 25 Nov, 2019

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
Recommended Articles
Page :