cassandra 學習之旅 初體驗

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依賴如下:

Xml代碼  收藏代碼
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>com.league</groupId>  
  5.     <artifactId>cassandra</artifactId>  
  6.     <version>0.0.1-SNAPSHOT</version>  
  7.     <spring.framework.version>3.2.8.RELEASE</spring.framework.version>  
  8.     <repositories>  
  9.         <repository>  
  10.             <id>spring-milestone</id>  
  11.             <name>Spring Maven MILESTONE Repository</name>  
  12.             <url>http://repo.spring.io/libs-milestone</url>  
  13.         </repository>  
  14.         <repository>  
  15.             <id>com.springsource.repository.maven.release</id>  
  16.             <url>http://maven.springframework.org/release/</url>  
  17.             <snapshots>  
  18.                 <enabled>false</enabled>  
  19.             </snapshots>  
  20.         </repository>  
  21.         <repository>  
  22.             <id>oracleReleases</id>  
  23.             <name>Oracle Released Java Packages</name>  
  24.             <url>http://download.oracle.com/maven</url>  
  25.         </repository>  
  26.         <repository>  
  27.             <id>JBossRepo1</id>  
  28.             <name>Jboss1</name>  
  29.             <url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>  
  30.         </repository>  
  31.         <repository>  
  32.             <id>JBossRepo</id>  
  33.             <name>Jboss</name>  
  34.             <url>https://repository.jboss.org/nexus/content/repositories/releases/</url>  
  35.         </repository>  
  36.     </repositories>  
  37.   
  38.     <dependencies>  
  39.         <dependency>  
  40.             <groupId>org.springframework.data</groupId>  
  41.             <artifactId>spring-data-cassandra</artifactId>  
  42.             <version>1.0.0.RELEASE</version>  
  43.         </dependency>  
  44.     </dependencies>  
  45.   
  46. </project>  

 

Java代碼  收藏代碼
  1. import java.util.List;  
  2. import java.util.UUID;  
  3.   
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5. import org.springframework.data.cassandra.core.CassandraOperations;  
  6.   
  7. import com.datastax.driver.core.querybuilder.QueryBuilder;  
  8. import com.datastax.driver.core.querybuilder.Select;  
  9. import com.league.cassandra.pojo.Person;  
  10.   
  11.   
  12.   
  13. /** 
  14.  * 創建表:  create table person (id varchar, name varchar, age int, primary key(id)); 
  15.  * 
  16.  */  
  17. public class App {  
  18.   
  19.   
  20.     public static void main(String[] args) {  
  21.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("demo1.xml");  
  22.         CassandraOperations cassandraTemplate = context.getBean("cassandraTemplate", CassandraOperations.class);  
  23.           
  24.         long t1 = System.currentTimeMillis();  
  25.           
  26.         //insert  
  27.         Person person = null;  
  28.         for (int i=0; i<100; i++){  
  29.             person = new Person(UUID.randomUUID().toString(), "NAME," + (i+1), i);  
  30.             cassandraTemplate.insert(person);  
  31.         }  
  32.         System.out.println("insert cost " + (System.currentTimeMillis() - t1));  
  33.           
  34.         //query cql  
  35.         List<Person> persons = cassandraTemplate.select("select * from person", Person.class);  
  36.         print(persons);  
  37.         System.out.println("Count: " + persons.size());  
  38.         System.out.println("query cost " + (System.currentTimeMillis() - t1));  
  39.           
  40.         //query select  
  41.         Select s = QueryBuilder.select().from("Person");  
  42.         //s.limit(50);//用於返回多少條數  
  43.         //s.where(QueryBuilder.eq("id", "ab081ad1-9b1c-43de-a605-1c36113a3d53"));   
  44.         List<Person> personsResults = cassandraTemplate.select(s, Person.class);  
  45.         print(personsResults);  
  46.         System.out.println("Count: " + personsResults.size());  
  47.         System.out.println("query cost " + (System.currentTimeMillis() - t1));  
  48.           
  49.         cassandraTemplate.getSession().close();  
  50.         context.close();  
  51.     }  
  52.       
  53.     public static void print(Person person){  
  54.         System.out.println(person);  
  55.     }  
  56.       
  57.     public static void print(List<Person> persons){  
  58.         for (int i=0; i<persons.size(); i++){  
  59.             System.out.print(i + "\t");  
  60.             print(persons.get(i));  
  61.         }  
  62.     }  
  63. }  

 

 

Java代碼  收藏代碼
  1. package com.league.cassandra.pojo;  
  2.   
  3. import org.springframework.data.cassandra.mapping.PrimaryKey;  
  4. import org.springframework.data.cassandra.mapping.Table;  
  5.   
  6. @Table  
  7. public class Person {   
  8.    
  9.  @PrimaryKey  
  10.  private String id;   
  11.    
  12.  private String name;   
  13.    
  14.  private int age;   
  15.    
  16.  public Person(String id, String name, int age) {   
  17.   this.id = id;   
  18.   this.name = name;   
  19.   this.age = age;   
  20.  }   
  21.    
  22.  public String getId() {   
  23.   return id;   
  24.  }   
  25.    
  26.  public String getName() {   
  27.   return name;   
  28.  }   
  29.    
  30.  public int getAge() {   
  31.   return age;   
  32.  }   
  33.    
  34.  @Override   
  35.  public String toString() {   
  36.   return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";   
  37.  }   
  38.    
  39. }  

 

 cassandra.properties

Properties代碼  收藏代碼
  1. cassandra.contactpoints=192.168.0.101  
  2. cassandra.port=9042  
  3. cassandra.keyspace=test_1  

 

demo1.xml

Xml代碼  收藏代碼
  1. <?xml version='1.0'?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cassandra="http://www.springframework.org/schema/data/cassandra"  
  4.   xmlns:context="http://www.springframework.org/schema/context"  
  5.   xsi:schemaLocation="http://www.springframework.org/schema/cql http://www.springframework.org/schema/cql/spring-cql-1.0.xsd  
  6.     http://www.springframework.org/schema/data/cassandra http://www.springframework.org/schema/data/cassandra/spring-cassandra-1.0.xsd  
  7.     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  8.     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">  
  9.   
  10.   <!-- Loads the properties into the Spring Context and uses them to fill   
  11.     in placeholders in the bean definitions -->  
  12.   <context:property-placeholder location="classpath:cassandra.properties" />  
  13.   
  14.   <!-- REQUIRED: The Cassandra Cluster -->  
  15.   <cassandra:cluster contact-points="${cassandra.contactpoints}"  
  16.     port="${cassandra.port}" />  
  17.   
  18.   <!-- REQUIRED: The Cassandra Session, built from the Cluster, and attaching   
  19.     to a keyspace -->  
  20.   <cassandra:session keyspace-name="${cassandra.keyspace}" />  
  21.   
  22.   <!-- REQUIRED: The Default Cassandra Mapping Context used by CassandraConverter -->  
  23.   <cassandra:mapping />  
  24.   
  25.   <!-- REQUIRED: The Default Cassandra Converter used by CassandraTemplate -->  
  26.   <cassandra:converter />  
  27.   
  28.   <!-- REQUIRED: The Cassandra Template is the building block of all Spring   
  29.     Data Cassandra -->  
  30.   <cassandra:template id="cassandraTemplate" />  
  31.   
  32.   <!-- OPTIONAL: If you are using Spring Data Cassandra Repositories, add   
  33.     your base packages to scan here  
  34.   <cassandra:repositories base-package="org.spring.cassandra.example.repo" />  
  35.    -->  
  36.   
  37. </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/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章