Professional Documents
Culture Documents
Cassandra - Datastax Java Driver - Treselle Systems
Cassandra - Datastax Java Driver - Treselle Systems
Cassandra - Datastax Java Driver - Treselle Systems
BLOG (866)-571-3757
BLOG HOME | BIG DATA | CASSANDRA
CONTACT US
– DATASTAX JAVA DRIVER
Big Data
1 Introduction
The NoSQL phenomenon has become the center of attraction 2 Use Cases
3 Solution
in the past few years because there is a rising demand to
3.1 Pre-requisites:
accommodate high volumes of real-time data. Hence, major 3.2 Read the following to
internet companies have popularized the use of data storage understand the terms, data
storage structure, and different
solutions that differ from traditional RDBMS. types of Data Model in
Cassandra
3.3 Design a Data Model in a
Apache Cassandra convenient way to retrieve it fast
3.4 Create a Java Program to
do the DML operation
One good solution for data storage is Apache Cassandra- a
4 Conclusion
distributed database management system. It was originally 5 Reference
developed by Face book. Cassandra is an integration of a
schema-flexible data model, (from Google’s BigTable) with a
fully distributed, shared-nothing design (from Amazon’s Dynamo). This structure of Cassandra offers
high availability, linear scalability and high performance while relaxing some consistency guarantees.
This blog deals with Cassandra’s interaction using Datastax Java driver, to create a perfect data model
for our application.
Datastax is the one of the Java client driver for Apache Cassandra. This driver works exclusively with
the Cassandra Query Language version 3 (CQL3) which is similar to SQL, and Cassandra’s binary
protocol. CQL3 is considered to be simple and better suited API for Cassandra than thrift API. Other
Cassandra client drivers appear to be complex while interacting with Cassandra, and writing Queries.
Use Cases
Let’s have a use case starting from the basic DML operation with Cassandra, using Datastax Java
driver in Java.
Pre-requisites
Understand the terms, and data storage structure of Cassandra.
Design a perfect Data Model in a convenient way to read it fast
Create a Java Program to do DML operations
www.treselle.com/blog/cassandra-datastax-java-driver/ 1/7
9/26/2019 Cassandra – Datastax Java Driver | Treselle Systems
The best way to approach data modeling for Cassandra is to start with the queries and work backwards
from there. Think about the actions our application needs to perform, how we want to access the data,
and then design column families to support those access patterns.
To understand the Cassandra data model, we need to get accustomed with the conventions of RDMS
(Relational Data Base Management System) and their naming structure in Cassandra.
The following table shows the terms used in RDBMS and Cassandra
RDBMS
Cassandra
If we are familiar with JAVA then it is easy to understand how Cassandra stores the data.
It stores the data as a Map of a Map: an outer Map keyed by a row key, and an inner Map keyed by a
Column name/key, where both maps are sorted.
Column name/key will vary for all rows of a Column family but Column datatype will be same for the
entire column family. This is because the Column name/key stores value.
In Cassandra, there is no limitation in Column size as we can store billions of columns with a single row
key. These types of columns are called wide-rows.
www.treselle.com/blog/cassandra-datastax-java-driver/ 2/7
9/26/2019 Cassandra – Datastax Java Driver | Treselle Systems
There are many ways to model data in Cassandra
Indexing is not an afterthought, anymore (866)-571-3757 CONTACT US
Think of physical storage structure
Design a Data Model in a convenient way to retrieve it fast Big Data Technology & Integration QA
www.treselle.com/blog/cassandra-datastax-java-driver/ 3/7
9/26/2019 Cassandra – Datastax Java Driver | Treselle Systems
67 PreparedStatement prepare_statement = null;
(866)-571-3757 CONTACT US
68 prepare_statement = this.session.prepare(properties
69 .getProperty("INSERT_ALL"));
70 boundStatement = new BoundStatement(prepare_statement); Big Data Technology & Integration QA
71 this.session.execute(boundStatement.bind(deptName, EmployeeId,
72 EmployeeName));
73 }
74
75 /**
76 * @param deptName
77 * @param EmployeeId
78 * update the data to using the deptname
79 */
80 public void update(String deptName, String EmployeeName, int id) {
81 BoundStatement boundStatement = null;
82 PreparedStatement prepare_statement = null;
83 prepare_statement = this.session.prepare(properties
84 .getProperty("UPDATE_NAME"));
85 boundStatement = new BoundStatement(prepare_statement);
86 this.session.execute(boundStatement.bind(EmployeeName, deptName, id));
87 }
88
89 public void delete(String deptName, int id) {
90 BoundStatement boundStatement = null;
91 PreparedStatement prepare_statement = null;
92 prepare_statement = this.session.prepare(properties
93 .getProperty("DELETE_EMP"));
94 boundStatement = new BoundStatement(prepare_statement);
95 this.session.execute(boundStatement.bind(deptName, id));
96 }
97
98 /**
99 * @param propertiesFileName
100 * @return java.util.Properties Object Load the values from File to Proper
101 * Object
102 */
103 private Properties loadProperties(String propertiesFileName) {
104 Properties prop = new Properties();
105 try {
106 prop.load(new FileInputStream(propertiesFileName + ".properties"))
107 } catch (IOException ex) {
108 ex.printStackTrace();
109 System.err.println(ex.getMessage());
110 }
111
112 return prop;
113 }
114
115 public static void main(String[] args) {
116 DMLOperations object = new DMLOperations();
117 properties = object.loadProperties("queries");
118 object.connect(properties.getProperty("SERVER_IP"),
119 properties.getProperty("keyspace"));
120 object.insertAll("bigdata", 03, "sam");
121 object.insertAll("bigdata", 05, "santhosh");
122 object.insertAll("java", 04, "joe");
123 System.err.println("Inserted ");
124 object.selectALL();
125 object.update("bigdata", "samKrish", 03);
126 System.err.println("Updated ");
127 object.selectALL();
128 object.delete("bigdata", 03);
129 System.err.println("Deleted");
130 object.selectALL();
131 object.disconnect();
132 }
133
134 }
queries.properties
1 SELECT_ALL=SELECT * FROM dmloperations;
2 INSERT_ALL = insert into dmloperations (dept_name, emp_id, emp_name ) VALUES (?,
3 UPDATE_NAME = update dmloperations SET emp_name =? where dept_name=? and emp_id=
4 DELETE_EMP = delete from dmloperations where dept_name=? and emp_id=?;
5 SERVER_IP=127.0.0.1
6 keyspace=dmlkeyspace
www.treselle.com/blog/cassandra-datastax-java-driver/ 4/7
9/26/2019 Cassandra – Datastax Java Driver | Treselle Systems
Challenges:
(866)-571-3757 CONTACT US
Throws java.lang.UnsupportedClassVersionError org/apache/cassandra/transport/FrameCompressor :
Unsupported major.minor version 51.0 because while trying to connect Cassandra with the
Big Data Technology & Integration QA
combination of higher version of Cassandra with lower version of Datastax Java driver.
Solution: Check version of Datastax java driver in the classpath and update it to the current or
upgraded version of Cassandra.
Throws NoHostAvailableException because the classpath Cassandra version is not matched with the
Cassandra instance version.
Solution: Change the Cassandra classpath jars to the existing Cassandra version we are trying to
connect.
Throws com.datastax.driver.core.exceptions.InvalidTypeException: Invalid type for value 0 of CQL type
text, expecting class Java.lang.String but class java.lang.Long provided this is because of the miss
match of data while binding with the data type of the specific column.
1 Exception com.datastax.driver.core.exceptions.InvalidTypeException:
2 Invalid type for value 0 of CQL type text, expecting class java.lang.String but
3 at com.datastax.driver.core.BoundStatement.bind
4 (BoundStatement.java:185)
5 ..2 more
Solution: Check with the data type we passed and while binding with the CQL query using Bound
Statement.
Conclusion
We are able to connect Cassandra through Datastax Java driver and complete DML operation using
CQL (Cassandra Query Language) in Java, which is comparatively easy as the jdbc.odbc driver in
MySQL.
Reference
Apache Cassandra: http://wiki.apache.org/cassandra/GettingStarted
Cassandra and Datastax:http://www.datastax.com/documentation/cassandra/2.0/webhelp/index.html
CQL Queries: https://cassandra.apache.org/doc/cql3/CQL.html
10487 Views 2 Views Today
10487 Views
Twitter
Facebook
Google +r
LinkedIN
www.treselle.com/blog/cassandra-datastax-java-driver/ 5/7
9/26/2019 Cassandra – Datastax Java Driver | Treselle Systems
LOG IN WITH
OR SIGN UP WITH DISQUS ?
Name
Data Analysis Using Apache Hive and Apache Drill vs Amazon Athena – A
Apache Pig Comparison on Data Partitioning
1 comment • 2 years ago 1 comment • 2 years ago
mickael — is it done with cloudera? right Gati Dash — So can we say Apache Drill is
more efficient until and unless we have a
specific usecase for Athena??]
European
Headquarters:
London EC3R 7LP
Asia-Pacific Office:
Singapore, 048624
www.treselle.com/blog/cassandra-datastax-java-driver/ 6/7
9/26/2019 Cassandra – Datastax Java Driver | Treselle Systems
www.treselle.com/blog/cassandra-datastax-java-driver/ 7/7