📅  最后修改于: 2023-12-03 14:59:53.739000             🧑  作者: Mango
Cassandra是一个高度可扩展的分布式NoSQL数据库系统,具有高可用性、高可靠性和良好的性能。它的数据模型是基于列族的方式组织数据,具有类似于关系型数据库的结构,同时又能够轻松应对海量数据存储、高吞吐量和高并发等方面的挑战。Cassandra由Facebook公司开发,现已被Apache基金会管理。
Cassandra具有以下特性:
以下是一个使用Cassandra的Java代码示例:
import com.datastax.driver.core.*;
public class CassandraDemo {
private Cluster cluster;
private Session session;
private static final String HOST = "127.0.0.1";
private static final int PORT = 9042;
public void connect() {
cluster = Cluster.builder()
.addContactPoint(HOST)
.withPort(PORT)
.build();
session = cluster.connect();
}
public void createKeyspace(String keyspaceName, String replicationStrategy, int replicationFactor) {
String cql = "CREATE KEYSPACE IF NOT EXISTS " + keyspaceName +
" WITH replication = {" +
"'class':'" + replicationStrategy + "', " +
"'replication_factor':" + replicationFactor +
"};";
session.execute(cql);
}
public void createTable(String keyspaceName, String tableName, String[] columns) {
StringBuilder cql = new StringBuilder("CREATE TABLE IF NOT EXISTS ");
cql.append(keyspaceName).append(".").append(tableName).append(" (");
for (int i = 0; i < columns.length; i++) {
cql.append(columns[i]).append(" text");
if (i != columns.length - 1) {
cql.append(", ");
}
}
cql.append(", PRIMARY KEY (").append(columns[0]).append("));");
session.execute(cql.toString());
}
public void insert(String keyspaceName, String tableName, String[] columns, String[] values) {
StringBuilder cql = new StringBuilder("INSERT INTO ");
cql.append(keyspaceName).append(".").append(tableName).append(" (");
for (int i = 0; i < columns.length; i++) {
cql.append(columns[i]);
if (i != columns.length - 1) {
cql.append(", ");
}
}
cql.append(") VALUES (");
for (int i = 0; i < values.length; i++) {
cql.append("'").append(values[i]).append("'");
if (i != values.length - 1) {
cql.append(", ");
}
}
cql.append(");");
session.execute(cql.toString());
}
public ResultSet select(String keyspaceName, String tableName, String[] columns) {
StringBuilder cql = new StringBuilder("SELECT ");
for (int i = 0; i < columns.length; i++) {
cql.append(columns[i]);
if (i != columns.length - 1) {
cql.append(", ");
}
}
cql.append(" FROM ").append(keyspaceName).append(".").append(tableName).append(";");
return session.execute(cql.toString());
}
public void close() {
session.close();
cluster.close();
}
public static void main(String[] args) {
CassandraDemo demo = new CassandraDemo();
demo.connect();
demo.createKeyspace("test", "SimpleStrategy", 1);
demo.createTable("test", "users", new String[]{"id", "name", "email"});
demo.insert("test", "users", new String[]{"id", "name", "email"}, new String[]{"1", "Alice", "alice@example.com"});
ResultSet results = demo.select("test", "users", new String[]{"id", "name", "email"});
for (Row row : results) {
System.out.printf("%s %s %s\n", row.getString("id"), row.getString("name"), row.getString("email"));
}
demo.close();
}
}
此代码创建了一个名为“test”的keyspace,并在其中创建了一个名为“users”的表格。然后,它向该表格中插入了一条数据并检索了所有数据并将其输出。