所需jar包:http://download.csdn.net/detail/nangongyanya/9738844
package com.cassandra;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
public class Connect {
// 此示例使用同步API,需要下载额外的Guava.jar
public static void main(String[] args) {
Cluster cluster = null;
try {
// Cluster对象是驱动程序的主入口点。 它保存了实际Cassandra集群(特别是元数据)的已知状态。
// 这个类是线程安全的,你应该创建一个单独的实例(每个目标Cassandra集群),并在你的应用程序中共享它;
cluster = Cluster.builder().addContactPoint("127.0.0.1").build();
// Session用来执行查询。 同样,它是线程安全的,应该重复使用;
Session session = cluster.connect();
// 我们使用execute来向Cassandra发送查询。 这将返回一个ResultSet,它本质上是一个Row对象的集合。
// 在下一行,我们提取第一行(在这种情况下是唯一的一行);
ResultSet rs = session
.execute("select release_version from system.local");
Row row = rs.one();
// 我们从行中提取第一个(和唯一的)列的值;
System.out.println(row.getString("release_version"));
} finally {
if (cluster != null)
// 最后,我们在完成后关闭集群。 这也将关闭从此群集创建的任何会话。
// 此步骤很重要,因为它释放了底层资源(TCP连接,线程池...)。
// 在实际应用程序中,通常在关闭时执行此操作(例如,取消部署webapp时)。
cluster.close();
}
}
}
Cluster cluster = Cluster.builder()
.withClusterName("myCluster")
.addContactPoint("127.0.0.1")
.build();
Initializer myInitializer = ... // your implementation
Cluster cluster = Cluster.buildFrom(myInitializer);
Session session = cluster.connect();
session.execute("select * from myKeyspace.myTable where id = 1");
Session session = cluster.connect("myKeyspace");
session.execute("select * from myTable where id = 1");
session.execute("select * from otherKeyspace.otherTable where id = 1");
ResultSet rs = session.execute("select release_version from system.local");
for (Row row : rs) {
// process the row
}
Row row = rs.one();
Row row = session.execute("select first_name, last_name from users where id = 1").one();
// The two are equivalent:
String firstName = row.getString(0);
String firstName = row.getString("first_name");
Integer age = row.isNull("age") ? null : row.getInt("age");
// Assuming given_names is a list:
List givenNames = row.getList("given_names", String.class);
// Assuming teams is a set>:
TypeToken> listOfStrings = new TypeToken>() {};
Set> teams = row.getSet("teams", listOfStrings);
for (ColumnDefinitions.Definition definition : row.getColumnDefinitions()) {
System.out.printf("Column %s has type %s%n",
definition.getName(),
definition.getType());
}
CQL3 data type | Getter name | Java type |
ascii | getString | java.lang.String |
bigint | getLong | long |
blob | getBytes | java.nio.ByteBuffer |
boolean | getBool | boolean |
counter | getLong | long |
date | getDate | LocalDate |
decimal | getDecimal | java.math.BigDecimal |
double | getDouble | double |
float | getFloat | float |
inet | getInet | java.net.InetAddress |
int | getInt | int |
list | getList | java.util.List |
map | getMap | java.util.Map |
set | getSet | java.util.Set |
smallint | getShort | short |
text | getString | java.lang.String |
time | getTime | long |
timestamp | getTimestamp | java.util.Date |
timeuuid | getUUID | java.util.UUID |
tinyint | getByte | byte |
tuple | getTupleValue | TupleValue |
user-defined types | getUDTValue | UDTValue |
uuid | getUUID | java.util.UUID |
varchar | getString | java.lang.String |
varint | getVarint | java.math.BigInteger |