Skip to content

Instantly share code, notes, and snippets.

@beccam
Last active July 21, 2017 21:02
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save beccam/fd8080820a7f08bac9d4 to your computer and use it in GitHub Desktop.
Save beccam/fd8080820a7f08bac9d4 to your computer and use it in GitHub Desktop.
Getting Started with Apache Cassandra and Java Part II
import com.datastax.driver.core.*;
public class GettingStartedTwo {
public static void main(String[] args) {
Cluster cluster;
Session session;
ResultSet results;
Row rows;
// Connect to the cluster and keyspace "demo"
cluster = Cluster
.builder()
.addContactPoint("localhost")
.withRetryPolicy(DefaultRetryPolicy.INSTANCE)
.withLoadBalancingPolicy(
new TokenAwarePolicy(new DCAwareRoundRobinPolicy()))
.build();
session = cluster.connect("demo");
// Insert one record into the users table
PreparedStatement statement = session.prepare(
"INSERT INTO users" + "(lastname, age, city, email, firstname)"
+ "VALUES (?,?,?,?,?);");
BoundStatement boundStatement = new BoundStatement(statement);
session.execute(boundStatement.bind("Jones", 35, "Austin",
"bob@example.com", "Bob"));
// Use select to get the user we just entered
Statement select = QueryBuilder.select().all().from("demo", "users")
.where(eq("lastname", "Jones"));
results = session.execute(select);
for (Row row : results) {
System.out.format("%s %d \n", row.getString("firstname"),
row.getInt("age"));
}
// Update the same user with a new age
Statement update = QueryBuilder.update("demo", "users")
.with(QueryBuilder.set("age", 36))
.where((QueryBuilder.eq("lastname", "Jones")));
session.execute(update);
// Select and show the change
select = QueryBuilder.select().all().from("demo", "users")
.where(eq("lastname", "Jones"));
results = session.execute(select);
for (Row row : results) {
System.out.format("%s %d \n", row.getString("firstname"),
row.getInt("age"));
}
// Delete the user from the users table
Statement delete = QueryBuilder.delete().from("users")
.where(QueryBuilder.eq("lastname", "Jones"));
results = session.execute(delete);
// Show that the user is gone
select = QueryBuilder.select().all().from("demo", "users");
results = session.execute(select);
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();
}
}
@vitostan
Copy link

vitostan commented Jun 5, 2015

Hi, this is Vito, just 4 reminding, there's a word missing @ line 35, "...(eq(..." should be "...(QueryBuilder.eq(...", cus' I confronted with a compile error while I was learning your code. :P

@vitostan
Copy link

vitostan commented Jun 5, 2015

And also @ line 50.

@mateenk
Copy link

mateenk commented Jun 19, 2015

@vitoyuan thanks for the update.

@pavankumarp89
Copy link

Can you share the code to handle connection pooling. Does Cassandra automatically increases the number of connections when there is a load.

@themhassany
Copy link

line #10 => Row rows; is not used.

@cdshinde
Copy link

Could you tell what is the Cassandra Driver that you have tested this against and if this is tested on Apache cassandra or DSE?
I was trying to work with Cassandra Java driver 3.0 and DataStax Distritribution for Apache Cassandra 3.3, and I was getting an runtime error : Type class com.datastax.driver.core.DataType$NativeType is not supported

@LazyCoder88
Copy link

I am getting compilation errors at line 18(DCAwareRoundRobinPolicy: ), 34(QueryBuilder.select), 43(QueryBuilder.update), 58(QueryBuilder.delete) and 64(QueryBuilder.select). Could anyone please help me out?

@tcpip4000
Copy link

line 18 error: should use the builder method:

new TokenAwarePolicy(DCAwareRoundRobinPolicy.builder().build()))

This is a workaround think should use the init method first.

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