-
-
Save beccam/fd8080820a7f08bac9d4 to your computer and use it in GitHub Desktop.
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", | |
"[email protected]", "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(); | |
} | |
} | |
And also @ line 50.
@vitoyuan thanks for the update.
Can you share the code to handle connection pooling. Does Cassandra automatically increases the number of connections when there is a load.
line #10 => Row rows; is not used.
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
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?
line 18 error: should use the builder method:
new TokenAwarePolicy(DCAwareRoundRobinPolicy.builder().build()))
This is a workaround think should use the init method first.
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