Skip to content

Instantly share code, notes, and snippets.

@jriley555
Forked from beccam/GettingStarted.java
Created September 13, 2016 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jriley555/6d71b855c6ef8420d292f5d96bba7dd6 to your computer and use it in GitHub Desktop.
Save jriley555/6d71b855c6ef8420d292f5d96bba7dd6 to your computer and use it in GitHub Desktop.
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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment