-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDataStaxDriver.java
More file actions
65 lines (49 loc) · 2.21 KB
/
Copy pathDataStaxDriver.java
File metadata and controls
65 lines (49 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import com.datastax.driver.core.*;
import com.datastax.driver.core.policies.DowngradingConsistencyRetryPolicy;
import com.datastax.driver.core.policies.LoggingRetryPolicy;
import com.datastax.driver.core.policies.TokenAwarePolicy;
import com.datastax.driver.core.querybuilder.QueryBuilder;
import com.datastax.driver.mapping.Mapper;
import com.datastax.driver.mapping.MappingManager;
import static com.datastax.driver.core.querybuilder.QueryBuilder.*;
import java.util.Date;
import java.util.UUID;
public class DataStaxDriver {
private static String clusterAddress = "127.0.0.1";
Cluster cluster;
public Session connect() {
cluster = Cluster
.builder()
.addContactPoint(clusterAddress)
.build();
return cluster.connect("killr_video");
}
public void hardCodedInsert(Session session) {
session.execute("INSERT INTO users ( user_id, created_date, email, first_name, last_name) VALUES (14c532ac-f5ae-479a-9d0a-36604732e01d, '2015-07-22 00:00:00', 'tim.berglund@datastax.com', 'Tim', 'Berglund')");
}
public void preparedInsert(Session session,
String email,
String firstName,
String lastName) {
PreparedStatement smt = session.prepare("INSERT INTO users (user_id, created_date, email, first_name, last_name) VALUES (?, ?, ?, ?, ?)");
BoundStatement bound = new BoundStatement(smt);
BoundStatement bs = bound.bind(UUID.randomUUID(), new Date(),email, firstName, lastName);
session.execute(bs);
}
public void printUsers(Session session) {
Statement allUsers = QueryBuilder.select().all().from("killr_video", "users");
ResultSet rs = session.execute(allUsers);
for(Row row : rs) {
System.out.printf("%s %s %s\n",
row.getString("email"),
row.getString("first_name"),
row.getString("last_name"));
}
}
public static void main(String args[]) {
DataStaxDriver d = new DataStaxDriver();
Session session = d.connect();
d.preparedInsert(session, "tim@timberglund.com", "Tim", "Berglund");
d.printUsers(session);
}
}