Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Getting Started with Apache Cassandra and Java
import com.datastax.driver.core.*;
public class GettingStarted {
public static void main(String[] args) {
Cluster cluster;
Session session;
// Connect to the cluster and keyspace "demo"
cluster = Cluster.builder().addContactPoint("127.0.0.1").build();
session = cluster.connect("demo");
// Insert one record into the users table
session.execute("INSERT INTO users (lastname, age, city, email, firstname) VALUES ('Jones', 35, 'Austin', 'bob@example.com', 'Bob')");
// Use select to get the user we just entered
ResultSet results = session.execute("SELECT * FROM users WHERE lastname='Jones'");
for (Row row : results) {
System.out.format("%s %d\n", row.getString("firstname"), row.getInt("age"));
}
// Update the same user with a new age
session.execute("update users set age = 36 where lastname = 'Jones'");
// Select and show the change
results = session.execute("select * from users where lastname='Jones'");
for (Row row : results) {
System.out.format("%s %d\n", row.getString("firstname"), row.getInt("age"));
}
// Delete the user from the users table
session.execute("DELETE FROM users WHERE lastname = '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("lastname"), row.getInt("age"), row.getString("city"), row.getString("email"), row.getString("firstname"));
}
// Clean up the connection by closing it
cluster.close();
}
}
@iabhinay

This comment has been minimized.

Copy link

@iabhinay iabhinay commented Feb 13, 2016

I am getting below error when i execute the file. I am using java 1.8:

[root@dbasandbox-test-1.avad tmp]# java -classpath cassandra-java-driver-2.0.2/:cassandra-java-driver-2.0.2/lib/:. GettingStarted
Exception in thread "main" java.lang.UnsupportedClassVersionError: GettingStarted : Unsupported major.minor version 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)

@LazyCoder88

This comment has been minimized.

Copy link

@LazyCoder88 LazyCoder88 commented May 16, 2016

I am getting the below error when I am trying to execute this program.

Exception in thread "main" com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: /127.0.0.1:9042 (com.datastax.driver.core.ConnectionException: [/127.0.0.1:9042] Unexpected error during transport initialization (com.datastax.driver.core.TransportException: [/127.0.0.1:9042] Unexpected exception triggered (java.lang.IndexOutOfBoundsException: Not enough readable bytes - Need 4, maximum is 0))))
at com.datastax.driver.core.ControlConnection.reconnectInternal(ControlConnection.java:196)
at com.datastax.driver.core.ControlConnection.connect(ControlConnection.java:79)
at com.datastax.driver.core.Cluster$Manager.init(Cluster.java:1104)
at com.datastax.driver.core.Cluster.init(Cluster.java:121)
at com.datastax.driver.core.Cluster.connect(Cluster.java:198)
at com.datastax.driver.core.Cluster.connect(Cluster.java:226)
at com.practice.GettingStarted.main(GettingStarted.java:11)

Could you please help me out?

Thanks.

@titsi

This comment has been minimized.

Copy link

@titsi titsi commented Sep 13, 2016

Has anyone solved the problem? I have the same problem.

@durgaswaroop

This comment has been minimized.

Copy link

@durgaswaroop durgaswaroop commented Oct 4, 2016

It is probably because the version of the driver being used is old. Use the recent version of the Java driver

@sreeladdu5

This comment has been minimized.

Copy link

@sreeladdu5 sreeladdu5 commented Dec 19, 2016

Please check the version of java used for compiling the src code and jars added in classpath. It basically occurs when two versions of java are used in program.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment