【发布时间】:2016-08-04 15:17:42
【问题描述】:
如果我今天开始一个项目,我可以为 Cassandra 使用的最佳 Java 驱动程序是什么?
【问题讨论】:
-
谢谢大家。我也是这么想的!!感谢你的回复。我不能投票给任何人,因为你们所有人的答案都是一样的,我不能投票给所有人:)
如果我今天开始一个项目,我可以为 Cassandra 使用的最佳 Java 驱动程序是什么?
【问题讨论】:
奇怪的问题,官方Java驱动只有1个:https://github.com/datastax/java-driver
并不是说你有十几个选择......
【讨论】:
我们正在使用datastax cassandra driver。它提供了我们需要的所有功能。
【讨论】:
只有官方的 Java 驱动来自 datastax,它有社区版和企业版的 cassandra。
https://github.com/datastax/java-driver
不过,它也有来自 datastax 的对象映射器,它可以将您的表映射到您的 pojo 类,有点像您在休眠中的操作方式。
【讨论】:
Datastax 驱动程序是最好的,有很多支持和广泛使用。 https://github.com/datastax/java-driver
【讨论】:
这里是在java中使用Datastax驱动的CRUD操作示例
public class CassandraCRUDdemo {
/*
* CREATE KEYSPACE demo WITH REPLICATION = { 'class' : 'SimpleStrategy',
* 'replication_factor' : 1 };
*
* CREATE TABLE users ( user_name varchar PRIMARY KEY, password varchar,
* gender varchar, session_token varchar, state varchar, birth_year bigint
* );
*/
public static void main(String[] args) {
Cluster cluster;
Session session;
// Connect to the cluster and keyspace "demo"
cluster = Cluster.builder().addContactPoint("sbshad10").withPort(9042)
.build();
session = cluster.connect("demo");
// Insert one record into the users table
session.execute("INSERT INTO users (user_name, password, gender, session_token, state,birth_year) VALUES ('Jones', 'pwd', 'Austin', 'bob@example.com', 'Bob',35)");
// Use select to get the user we just entered
ResultSet results = session
.execute("SELECT * FROM users WHERE user_name='Jones'");
for (Row row : results) {
System.out.format("%s %d\n", row.getString("user_name"),
row.getLong("birth_year"));
}
// Update the same user with a new age
session.execute("update users set birth_year = 36 where user_name = 'Jones'");
// Select and show the change
results = session.execute("select * from users where user_name='Jones'");
for (Row row : results) {
System.out.format("%s %d\n", row.getString("user_name"),
row.getLong("birth_year"));
}
// Delete the user from the users table
session.execute("DELETE FROM users WHERE user_name = 'Jones'");
// Show that the user is gone
results = session.execute("SELECT * FROM users");
for (Row row : results) {
System.out.format("%s %d %s %s %s\n", row.getString("user_name"),
row.getLong("birth_year"), row.getString("state"),
row.getString("session_token"), row.getString("gender"));
}
// Clean up the connection by closing it
cluster.close();
}}
和 pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cassandra</groupId>
<artifactId>cassandra-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.datastax.cassandra/cassandra-driver-core -->
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
<version>3.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
<!-- <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId>
<version>1.2</version> </dependency> -->
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.netty/netty-all -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.0.27.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.codahale.metrics/metrics-core -->
<dependency>
<groupId>com.codahale.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>3.0.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
【讨论】: