Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Jun 5, 2015

And also @ line 50.

@mateenk

This comment has been minimized.

Copy link

commented Jun 19, 2015

@vitoyuan thanks for the update.

@pavankumarp89

This comment has been minimized.

Copy link

commented Aug 21, 2015

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

@jone30rw

This comment has been minimized.

Copy link

commented Feb 3, 2016

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

@cdshinde

This comment has been minimized.

Copy link

commented Mar 16, 2016

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

This comment has been minimized.

Copy link

commented May 16, 2016

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

This comment has been minimized.

Copy link

commented May 17, 2016

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
You can’t perform that action at this time.