Application connectivity with Cassandra - GeeksforGeeks

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.
To connect with cassandra there are many Driver available. few are listed below.
Java
Python
Node.js
C#
In programming language to connect application with database there is a programming Pattern.Three Easy steps are following :
Create a connection (which is called a Session)Use the session to execute the query.Be sure to close the connection/session.
Create a connection (which is called a Session)
Use the session to execute the query.
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()) { 
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()); 
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() 
// 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() 
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')) 
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() 
session.shutdown() 
Apache
DBMS
DBMS
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
SQL Trigger | Student Database
CTE in SQL
Introduction of B-Tree
Difference between Clustered and Non-clustered index
SQL Interview Questions
Introduction of ER Model
Data Preprocessing in Data Mining
SQL | GROUP BY
Introduction of DBMS (Database Management System) | Set 1
SQL | Views