Created
June 27, 2014 19:54
-
-
Save beccam/06c3283e5ee4a480a555 to your computer and use it in GitHub Desktop.
Getting Started with Apache Cassandra and Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', '[email protected]', '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(); | |
} | |
} |
Has anyone solved the problem? I have the same problem.
It is probably because the version of the driver being used is old. Use the recent version of the Java driver
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
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.