1、安装
1.1、下载地址
1.2、Linux下安装
以下命令使用root用户执行,以下为单机版
新建cassandra用户
groupadd cassandra
useradd -s /bin/bash -g cassandra -d /opt/cassandra cassandra
解压并赋给cassandra权限
tar -zxvf apache-cassandra-2.2.0-bin.tar.gz -C /opt/cassandra
chown -R cassandra:cassandra /opt/cassandra
启动
cd /opt/cassandra/apache-cassandra-2.2.0/bin
su cassandra ./cassandra
如果没有报错, 出现如下信息表示启动成功
INFO 02:45:30 No gossip backlog; proceeding
关闭
ps -aux |grep cassandra
kill pid#pid为cassandra的pid
2、cassandra之旅
以下命令使用cassandra用户执行
安装好之后,那么如何使用呢?
先修改配置文件,使远程客户端能连上,
vim cassandra.yaml
将下面两个配置项的值由localhost改为IP地址,示例如下
listen_address: 192.168.0.101
rpc_address: 192.168.0.101
2.1、使用cassandra自带的命令行
cd /opt/cassandra/apache-cassandra-2.2.0/bin
./cqlsh 192.168.0.101
出现如下命令行, 则表示连接成功
cqlsh>
下面输入简单的几个cql命令, 详细的cql使用,请参见CQL for Cassandra 2.0 & 2.1
--创建keyspace, 类似SQL Database
create keyspace if not exists test_1 with replication = {'class': 'SimpleStrategy', 'replication_factor': 1};
--创建cf, 类似SQL Table
create table users (id int, user_name varchar, primary key(id));
--修改表
alter table users add age int;
--插入数据
insert into users (id, user_name) values (1, 'zhangsan');
--修改数据
update users set user_name = 'zhangsan111' where id = 1;
update users set age = 5 where id = 1;
--查询
select * from users;
select count(*) from users;
cassandra的查询具有以下约束:
第一主键 只能用=或IN查询
第二主键 支持= ,> ,<, >= ,<= 但是必须后面加 ALLOW FILTERING
索引列 只能用=查询
--删除数据
delete from users where id =1;
2.2、代码实现表的CRUD操作
我这里以JAVA举例
MAVEN依赖如下:
<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>com.league</groupId> <artifactId>cassandra</artifactId> <version>0.0.1-SNAPSHOT</version> <spring.framework.version>3.2.8.RELEASE</spring.framework.version> <repositories> <repository> <id>spring-milestone</id> <name>Spring Maven MILESTONE Repository</name> <url>http://repo.spring.io/libs-milestone</url> </repository> <repository> <id>com.springsource.repository.maven.release</id> <url>http://maven.springframework.org/release/</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> <repository> <id>oracleReleases</id> <name>Oracle Released Java Packages</name> <url>http://download.oracle.com/maven</url> </repository> <repository> <id>JBossRepo1</id> <name>Jboss1</name> <url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url> </repository> <repository> <id>JBossRepo</id> <name>Jboss</name> <url>https://repository.jboss.org/nexus/content/repositories/releases/</url> </repository> </repositories> <dependencies> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-cassandra</artifactId> <version>1.0.0.RELEASE</version> </dependency> </dependencies> </project>
import java.util.List; import java.util.UUID; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.data.cassandra.core.CassandraOperations; import com.datastax.driver.core.querybuilder.QueryBuilder; import com.datastax.driver.core.querybuilder.Select; import com.league.cassandra.pojo.Person; /** * 创建表: create table person (id varchar, name varchar, age int, primary key(id)); * */ public class App { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("demo1.xml"); CassandraOperations cassandraTemplate = context.getBean("cassandraTemplate", CassandraOperations.class); long t1 = System.currentTimeMillis(); //insert Person person = null; for (int i=0; i<100; i++){ person = new Person(UUID.randomUUID().toString(), "NAME," + (i+1), i); cassandraTemplate.insert(person); } System.out.println("insert cost " + (System.currentTimeMillis() - t1)); //query cql List<Person> persons = cassandraTemplate.select("select * from person", Person.class); print(persons); System.out.println("Count: " + persons.size()); System.out.println("query cost " + (System.currentTimeMillis() - t1)); //query select Select s = QueryBuilder.select().from("Person"); //s.limit(50);//用于返回多少条数 //s.where(QueryBuilder.eq("id", "ab081ad1-9b1c-43de-a605-1c36113a3d53")); List<Person> personsResults = cassandraTemplate.select(s, Person.class); print(personsResults); System.out.println("Count: " + personsResults.size()); System.out.println("query cost " + (System.currentTimeMillis() - t1)); cassandraTemplate.getSession().close(); context.close(); } public static void print(Person person){ System.out.println(person); } public static void print(List<Person> persons){ for (int i=0; i<persons.size(); i++){ System.out.print(i + "\t"); print(persons.get(i)); } } }
package com.league.cassandra.pojo; import org.springframework.data.cassandra.mapping.PrimaryKey; import org.springframework.data.cassandra.mapping.Table; @Table public class Person { @PrimaryKey private String id; private String name; private int age; public Person(String id, String name, int age) { this.id = id; this.name = name; this.age = age; } public String getId() { return id; } public String getName() { return name; } public int getAge() { return age; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
cassandra.properties
cassandra.contactpoints=192.168.0.101 cassandra.port=9042 cassandra.keyspace=test_1
demo1.xml
<?xml version='1.0'?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cassandra="http://www.springframework.org/schema/data/cassandra" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/cql http://www.springframework.org/schema/cql/spring-cql-1.0.xsd http://www.springframework.org/schema/data/cassandra http://www.springframework.org/schema/data/cassandra/spring-cassandra-1.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <!-- Loads the properties into the Spring Context and uses them to fill in placeholders in the bean definitions --> <context:property-placeholder location="classpath:cassandra.properties" /> <!-- REQUIRED: The Cassandra Cluster --> <cassandra:cluster contact-points="${cassandra.contactpoints}" port="${cassandra.port}" /> <!-- REQUIRED: The Cassandra Session, built from the Cluster, and attaching to a keyspace --> <cassandra:session keyspace-name="${cassandra.keyspace}" /> <!-- REQUIRED: The Default Cassandra Mapping Context used by CassandraConverter --> <cassandra:mapping /> <!-- REQUIRED: The Default Cassandra Converter used by CassandraTemplate --> <cassandra:converter /> <!-- REQUIRED: The Cassandra Template is the building block of all Spring Data Cassandra --> <cassandra:template id="cassandraTemplate" /> <!-- OPTIONAL: If you are using Spring Data Cassandra Repositories, add your base packages to scan here <cassandra:repositories base-package="org.spring.cassandra.example.repo" /> --> </beans>
spring整合cassandra,更多请参见 官方文档
关于cassandra学习过程中, 更多的参考文档如下:
NoSQL诞生的原因和优缺点 http://blog.csdn.net/chenhuajie123/article/details/9374969
Cassandra中实现SQL操作 http://dongxicheng.org/nosql/cassandra-sql/
Cassandra 2.1 数据查询语法 http://www.tuicool.com/articles/qAVBna7
CQL for Cassandra 2.0 & 2.1 http://docs.datastax.com/en/cql/3.1/cql/cql_reference/update_r.html
http://cassandra.apache.org/doc/cql3/CQL.html
datastax指南: https://academy.datastax.com/demos/brief-introduction-apache-cassandra
spring整合cassandra http://docs.spring.io/spring-data/cassandra/docs/1.0.2.RELEASE/reference/htmlsingle/#cassandra-getting-started
配置项设置: http://pimin.net/archives/297
文章参考: http://tech.chinaunix.net/a2010/1225/1142/000001142663.shtml
一网打尽当下NoSQL类型、适用场景及使用公司 http://www.csdn.net/article/2013-07-24/2816330-how-to-choose-nosql-db
cassandra实战书: http://book.51cto.com/art/201105/264261.htm
cassandra最佳实践: http://www.infoq.com/cn/articles/best-practices-cassandra-data-model-design-part2/
Cassandra – 数据结构设计概念和原则 http://my.oschina.net/silentriver/blog/182814
Cassandra研究报告 http://blog.csdn.net/zyz511919766/article/details/38683219/
相关推荐
CassandraUnit是一个Cassandra数据操作的单元测试框架,其功能与关系型数据库测试框架DBunit类似,只是其测试对象是Cassandra,下面是一个最简单使用例子: 创建一个用于存储数据的xml文件,命名为 simpleDataSet....
List<User> users = new ArrayList<>(); for (Row row : resultSet) { User user = new User(); user.setId(row.getString("id")); user.setName(row.getString("name")); user.setEmail(row.getString("email...
<artifactId>spring-boot-starter-data-cassandra</artifactId> </dependency> ``` - 接着,配置`application.properties`文件,设置Cassandra的连接信息: ``` spring.data.cassandra.contact-points=...
请注意,上述代码只是一个基本示例,实际使用时需要替换`<hostname>`、`<keyspace>`、`<username>`和`<password>`为你的Cassandra集群的实际信息。 总结来说,"cassandra2.1.2 JDBC最少依赖jar"是一个包含连接...
groupId> < artifactId>cassandra-extra</ artifactId> < version>0.0.1</ version></ dependency>嵌入式卡桑德拉EmbeddedCassandra允许在当前运行的JVM中生成一个1节点的Apache Cassandra集群。 这对于超出简单...
JavaOutputDStream<String> output = wordCountPairs.mapToPair(t -> new Tuple2<>("output_topic", t._1() + ":" + t._2())); KafkaUtils.createDirectStream(jssc, Topics.outputTopic, kafkaParams, output); ...
<artifactId>spring-boot-starter-data-cassandra</artifactId> </dependency> ``` 接下来,我们需要配置Cassandra的数据源。在`application.properties`或`application.yml`中,提供Cassandra集群的信息,如主机...
Hadoop 2.7.5是其历史版本之一,包含了对Hadoop生态系统的一系列改进和优化。在Window10操作系统上搭建和运行Hadoop环境,有助于开发者在本地环境中进行快速开发和测试,便于理解Hadoop的工作原理和操作流程。 首先...
<groupId>com.datastax.cassandra</groupId> <artifactId>cassandra-driver-core</artifactId> <version>3.x.x</version> <!-- 替换为最新稳定版本 --> </dependency> ``` 2. **创建Cluster实例**: 连接到...
在Java编程环境中,Cassandra数据库是一个高性能、分布式、NoSQL型的数据存储系统。它由Apache Software Foundation维护,设计用于处理大规模数据,具有高可用性和可扩展性。本篇将深入探讨如何使用Java来操作...
<CalloutLocation>D:\apache-cassandra-0.6.8\callouts</CalloutLocation> <StagingFileDirectory>D:\apache-cassandra-0.6.8\staging</StagingFileDirectory> ``` 5. **启动 Cassandra** 转到 `D:\apache-...
<content>这是我的第一条留言!</content> </entry> <entry> <username>用户B</username> <datetime>2022-03-02 09:45:00</datetime> <content>很高兴看到这个XML留言板!</content> </entry> <!-- 更多留言...
<artifactId>apijson-cassandra</artifactId> <version>6.1.0+</version> </dependency> ``` 对于 Gradle,则在 `build.gradle` 文件中添加: ```groovy implementation 'org.apijson:apijson-cassandra:6.1.0+'...
### Cassandra学习知识点详解 #### 一、Cassandra简介与历史 **Cassandra**是一款高性能的分布式NoSQL数据库系统,由Facebook开发并在2008年开源。它最初是为了支持Facebook的收件箱搜索功能而设计的,其核心设计...
spark-submit --master <master> --class uk.co.pinpointlabs.App --input <path> --host <host> --keyspace <keyspace> --table <table> 使用 CqlBulkOutputFormat 使用 datastax cassandra 连接器(当前不起作用...
2. 创建Cluster和Session:初始化Cassandra连接需要创建一个Cluster对象,表示一组Cassandra节点。然后,基于Cluster创建Session,用于执行实际的数据库操作。 ```java Cluster cluster = HFactory.getCluster(...
Cassandra是一个混合型的非关系的数据库,类似于Google的BigTable。其主要功能比Dynomite(分布式的Key-Value存储系统)更丰富,但支持度却不如文档存储MongoDB(介于关系数据库和非关系数据库之间的开源产品,是非...
<artifactId>cassandra-maven-plugin</artifactId> <version>2.0.0-1-klappo</version> <configuration> <cqlVersion>3.0</cqlVersion> <keyspace>userservice</keyspace> <addTestClasspath>true</...
<version>4.x.x</version> <!-- 根据最新版本号替换 --> </dependency> ``` 配置驱动时,我们需要指定Cassandra集群的地址、端口以及其他参数,这通常在应用的配置文件中完成。 三、连接管理 连接管理是Cassandra...