Cassandra 入门

来源:互联网 发布:windows网络命令 编辑:程序博客网 时间:2024/06/01 07:27

一. 基本概念

1. Primary Key, Partition Key, Clustering Key

以下面的Table为例子:

create table sample(

      k_part_one text,

      k_part_two int,

      k_clust_one text,

      k_clust_two int,

      k_clust_three uuid,

      data text,

      PRIMARY KEY((k_part_one,k_part_two),k_clust_one, k_clust_two, k_clust_three)     

  ) with CLUSTERING ORDER BY (k_clust_one DESC, k_clust_two ASC);


Primary Key=Partition Key + Clustering Key, Partition Key是(k_part_one,k_part_two),Clustering Key是k_clust_one, k_clust_two, k_clust_three

Partition Key决定了数据存储在集群的哪个节点,Primary Key决定数据在当前节点的排序。


2. 集合类型

1) set      如:set<text>,  有序集合 {'f@baggins.com', 'baggins@gmail.com'}

2) list      如:list<text>,  数组 ['rivendell', 'rohan' ]

3) map   如:map<timestamp, text>, 字典 { '2013-9-22 12:01' : 'birthday wishes to Bilbo', '2013-10-1 18:00': 'Check into Inn of Pracing Pony'}


3. 自定义类型(UDT)

CREATE TYPE mykeyspace.address (
  street text,
  city text,
  zip_code int,
  phones set<text>
);


二. 安装

下面以在windows 7 x64 安装Cassandra 2.1.14为例

1) 下载JDK并安装至C:\Program Files\Java\jdk1.8.0_91

http://download.oracle.com/otn-pub/java/jdk/8u91-b15/jdk-8u91-windows-x64.exe?AuthParam=1465374689_e7b7ab5575db24a9221c618360f4fa5d

2) 下载apache cassandra 2.1.14 , 并解压缩到D:\apache-cassandra-2.1.14

http://mirror.bit.edu.cn/apache/cassandra/2.1.14/apache-cassandra-2.1.14-bin.tar.gz

3) 修改D:\apache-cassandra-2.1.14\bin下的两个配置文件

a. cassandra.bat

在 @REM limitations under the License.行下添加下面两行

set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_91
set CASSANDRA_HOME=D:\apache-cassandra-2.1.14

b. cassandra.in.bat

 @REM  limitations under the License.行下添加下面两行

set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_91
set CASSANDRA_HOME=D:\apache-cassandra-2.1.14

4) 在D:\apache-cassandra-2.1.14\下创建以下目录

 data

 data\data

 data\commitlog

 也可以通过修改conf\cassandra.yaml配置文件,将目录创建在其它地方。


# Directories where Cassandra should store data on disk.  Cassandra
# will spread data evenly across them, subject to the granularity of
# the configured compaction strategy.
# If not set, the default directory is $CASSANDRA_HOME/data/data.
# data_file_directories:
#     - /var/lib/cassandra/data


# commit log.  when running on magnetic HDD, this should be a
# separate spindle than the data directories.
# If not set, the default directory is $CASSANDRA_HOME/data/commitlog.
# commitlog_directory: /var/lib/cassandra/commitlog


5)修改配置文件conf\cassandra.yaml

a.  开启账号密码验证,使用cassandra/cassandra登录后,可以创建新用户

authenticator: AllowAllAuthenticator > authenticator: PasswordAuthenticator

b. 设置对外监听ip, 否者只能以localhost访问

rpc_address: localhost > rpc_address: 10.128.42.166

6) 启动Cassandra

      打开cmd, 运行 D:\apache-cassandra-2.1.14\bin\cassandra.bat

7)  验证Cassandra

      打开cmd, 切换到 D:\apache-cassandra-2.1.14\bin\目录

       查看服务器状态:nodetool status

        查看服务详细信息:nodetool info

8) 安装Python (cqlsh)

下载Python安装程序https://www.python.org/ftp/python/2.7.11/python-2.7.11.amd64.msi, 安装的时候注意勾选 Add Python  PATH的选项。

安装完毕后,到D:\apache-cassandra-2.1.14\pylib目录下执行 "python setup.py install".

这样就可以使用cqlsh客户端了。


三. Cassandra常用端口

  • 7199 JMX monitoring port (cassandra.bat)
  • 7000 Inter-node cluster (cassandra.yaml)
  • 7001 SSL inter-node cluster (cassandra.yaml)
  • 9042 CQL Native Transport Port (cassandra.yaml)
  • 9160 Thrift (cassandra.yaml)

四.  客户端工具

图形化客户端工具推荐使用DBeaver, 下载地址:http://dbeaver.jkiss.org/download/


五. 参考资料

http://www.slideshare.net/jaykumarpatel/cassandra-data-modeling-best-practices?next_slideshow=1

http://www.ebaytechblog.com/2012/07/16/cassandra-data-modeling-best-practices-part-1/

http://www.ebaytechblog.com/2012/08/14/cassandra-data-modeling-best-practices-part-2/

http://www.slideshare.net/patrickmcfadin/advanced-data-modeling-with-apache-cassandra

http://www.datastax.com/dev/blog/basic-rules-of-cassandra-data-modeling

http://docs.datastax.com/en/cql/3.1/cql/cql_using/start_cql_win_t.html

http://datastax.github.io/csharp-driver/features/components/core/




0 0
原创粉丝点击