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/