cassandra-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Vivek Mishra <vivek.mis...@impetus.co.in>
Subject Java Cassandra Driver: Using CQL
Date Mon, 07 Feb 2011 12:08:53 GMT
Hi,
Recently I worked on implementation of java jdbc driver for cassandra using CQL.
Given below is an example code base(with basic features)  about how to use it:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;



public class Cass
{
                public static void main(String[] args) {
                      java.sql.Connection con =null;
                                try {
                                     String selectQ = "SELECT \"first\", \"last\" FROM Users
WHERE KEY=\"jsmith\"";
                                                Class.forName("com.impetus.jdbc.cassandra.api.CassandraDriver");
//             con = DriverManager.getConnection("jdbc:cassandra:localhost@9160");
                                                con = DriverManager.getConnection("jdbc:cassandra:root/root@localhost:9160/root1");

             //With Statement
             scrollResultset(withStatement(con));

             //With PreparedStatement.
             scrollResultset(withPreparedStatement(con, selectQ));

             //Update/INSERT
             withUpdateStatement(con);
             String updateSelect =  "SELECT \"firstN\", \"lastN\" FROM Users WHERE KEY=\"jsmith\"";
             scrollResultset(withPreparedStatement(con,updateSelect));

             //Delete
             withDeleteStatement(con);
             scrollResultset(withPreparedStatement(con,updateSelect));

                                } catch (ClassNotFoundException e) {
                                                e.printStackTrace();
                                }
catch (SQLException e) {
                                                e.printStackTrace();
                                } finally{
                                                if(con !=null) {
                                                                try {
                                                                                con.close();
                                                                } catch (SQLException e) {
                                                                                // TODO Auto-generated
catch block
                                                                                e.printStackTrace();
                                                                }
                                                                con = null;
                                                }
                                }
                }

                /**
                *
                 * @param con
                * @return
                * @throws SQLException
                */
                private static ResultSet withStatement(Connection con) throws SQLException{
//                            String useQ = "USE Keyspace1";
                     String selectQ = "SELECT \"first\", \"last\" FROM Users WHERE KEY=\"jsmith\"";
       Statement stmt = con.createStatement();
//       stmt.execute(useQ);
       return stmt.executeQuery(selectQ);
                }

                /**
                *
                 * @param con
                * @return
                * @throws SQLException
                */
                private static ResultSet withPreparedStatement(Connection con, String selectQ)
throws SQLException{
//                            String useQ = "USE Keyspace1";
//         PreparedStatement statement = con.prepareStatement(useQ);
//         statement.execute();
                                PreparedStatement statement = con.prepareStatement(selectQ);
         return statement.executeQuery();

                }

                /**
                *
                 * @param rSet
                * @throws SQLException
                */
                private static void scrollResultset(ResultSet rSet) throws SQLException {

                                while(rSet.next()){
                 System.out.println(rSet.getString(0));
                 System.out.println(rSet.getString("last"));
                 System.out.println(rSet.getString("lastN"));

         }
                }

                private static void withUpdateStatement(Connection con) throws SQLException
{
                                String updateQ =  "UPDATE Users SET \"firstN\" = \"vivekn\",
\"lastN\" = \"mishran\" WHERE KEY = \"jsmith\"";
                                PreparedStatement statement = con.prepareStatement(updateQ);
        statement.execute();
                }

                private static void withDeleteStatement(Connection con) throws SQLException
{
                                String deleteQ = "DELETE \"firstN\", \"lastN\" FROM Users
WHERE KEY=\"jsmith\"";
                                PreparedStatement statement = con.prepareStatement(deleteQ);
        statement.execute();
                }
}


I am not sure if there is any JIRA related to this. With such basic things I have this in
place.(A lot of work needs to be done on this.)

Please let me know, if I can add this project in relation with any Cassandra JIRA, else planning
to open source it on google code.


Vivek

________________________________

Impetus to Present Big Data -- Analytics Solutions and Strategies at O'Reilly Strata Conference
(Feb 1-3) in Santa Clara, CA. Our Big Data technology evangelist to speak on 'Deriving Intelligence
From Large Data - Using Hadoop and Applying Analytics'.

Impetus to organize and host CloudCamp, Delhi on Feb 12. CloudCamp is an unconference where
early adopters of Cloud Computing technologies exchange ideas.

Click http://www.impetus.com to know more.


NOTE: This message may contain information that is confidential, proprietary, privileged or
otherwise protected by law. The message is intended solely for the named addressee. If received
in error, please destroy and notify the sender. Any use of this email is prohibited when received
in error. Impetus does not represent, warrant and/or guarantee, that the integrity of this
communication has been maintained nor that the communication is free of errors, virus, interception
or interference.

Mime
View raw message