Subrata Saha
Subrata Saha

Reputation: 454

Java Driver for Cassandra?

If i start a project today What is best Java Driver i can use for Cassandra ?

Upvotes: 1

Views: 2921

Answers (5)

Vikas Singh
Vikas Singh

Reputation: 419

Here is the example of CRUD operation using Datastax driver in java

public class CassandraCRUDdemo {

/*
 * CREATE KEYSPACE demo WITH REPLICATION = { 'class' : 'SimpleStrategy',
 * 'replication_factor' : 1 };
 * 
 * CREATE TABLE users ( user_name varchar PRIMARY KEY, password varchar,
 * gender varchar, session_token varchar, state varchar, birth_year bigint
 * );
 */

public static void main(String[] args) {

    Cluster cluster;
    Session session;

    // Connect to the cluster and keyspace "demo"
    cluster = Cluster.builder().addContactPoint("sbshad10").withPort(9042)
            .build();
    session = cluster.connect("demo");

    // Insert one record into the users table
    session.execute("INSERT INTO users (user_name, password, gender, session_token, state,birth_year) VALUES ('Jones', 'pwd', 'Austin', '[email protected]', 'Bob',35)");

    // Use select to get the user we just entered
    ResultSet results = session
            .execute("SELECT * FROM users WHERE user_name='Jones'");
    for (Row row : results) {
        System.out.format("%s %d\n", row.getString("user_name"),
                row.getLong("birth_year"));
    }

    // Update the same user with a new age
    session.execute("update users set birth_year = 36 where user_name = 'Jones'");
    // Select and show the change
    results = session.execute("select * from users where user_name='Jones'");
    for (Row row : results) {
        System.out.format("%s %d\n", row.getString("user_name"),
                row.getLong("birth_year"));

    }

    // Delete the user from the users table
    session.execute("DELETE FROM users WHERE user_name = 'Jones'");
    // Show that the user is gone
    results = session.execute("SELECT * FROM users");
    for (Row row : results) {
        System.out.format("%s %d %s %s %s\n", row.getString("user_name"),
                row.getLong("birth_year"), row.getString("state"),
                row.getString("session_token"), row.getString("gender"));
    }

    // Clean up the connection by closing it
    cluster.close();
}}

and the pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cassandra</groupId>
<artifactId>cassandra-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>

<dependencies>
    <!-- https://mvnrepository.com/artifact/com.datastax.cassandra/cassandra-driver-core -->
    <dependency>
        <groupId>com.datastax.cassandra</groupId>
        <artifactId>cassandra-driver-core</artifactId>
        <version>3.1.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>19.0</version>
    </dependency>
    <!-- <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> 
        <version>1.2</version> </dependency> -->
    <!-- https://mvnrepository.com/artifact/log4j/log4j -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.14</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.6.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.netty/netty-all -->
    <dependency>
        <groupId>io.netty</groupId>
        <artifactId>netty-all</artifactId>
        <version>4.0.27.Final</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.codahale.metrics/metrics-core -->
    <dependency>
        <groupId>com.codahale.metrics</groupId>
        <artifactId>metrics-core</artifactId>
        <version>3.0.2</version>
    </dependency>


</dependencies>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

Upvotes: 0

hck3r
hck3r

Reputation: 199

Datastax driver is the best with lot of support and extensively used. https://github.com/datastax/java-driver

Upvotes: 1

Abhishek Anand
Abhishek Anand

Reputation: 1992

Only official Java driver is from datastax, which has both community and enterprise editions of cassandra.

https://github.com/datastax/java-driver

However, it has object mappers available from datastax too, which can map your tables to your pojo classes, somewhat, how you do in hibernate.

Upvotes: 1

Beri
Beri

Reputation: 11600

We are using datastax cassandra driver. It provides all the functionality we need.

Upvotes: 2

doanduyhai
doanduyhai

Reputation: 8812

Weird question, there is only 1 official Java driver: https://github.com/datastax/java-driver

It's not like you have dozen of choices ...

Upvotes: 5

Related Questions