Open In App
Related Articles

Application connectivity with Cassandra

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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 CassandraTo 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 Python 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() 

Last Updated : 05 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads