Cassandra入门简介

2018-06-12  本文已影响0人  小小韩_小小红
1. 什么是Apache Cassandra?

Apache Cassandra是一个开源,分布式和分散式/分布式存储系统(数据库),用于管理遍布世界各地的大量结构化数据。它提供高可用性的服务,没有单点故障。下面列出了Apache Cassandra的一些值得注意的地方:

2. Cassandra的特点

Cassandra因其卓越的技术特性而变得如此受欢迎。下面给出了Cassandra的一些特性:

3. 相关概念

keyspace -> table –> column,对应关系型数据库 database -> table -> column
存储结构:
CREATE TABLE mykeyspace.mytable (
  key1 text,
  key2 text,
  key3 text,
  column1 bigint,
  column2 int,
  column3 timestamp,
  PRIMARY KEY (key1, key2, key3);
    )
    key1: partitionKey,分区主键,用来决定Cassandra会使用集群中的哪个结点来记录该数据,每个Partition Key对应着一个特定的Partition;

    key2, key3 clusterKey,集群主键,Clustering Key用来在Partition内部排序,默认为ASC。如果一个Primary Key只包含一个域,那么其将只拥有Partition Key而没有Clustering Key。
4. Cassandra安装

依赖:jdk、python,并且要保证jvm(java命令)和jdk(javac命令)版本一致,python的版本也会根据你下载的Cassandra版本有要求,具体请查看官方文档:http://cassandra.apache.org/doc/latest/,jdk和python的安装我这里就不演示,大家自行安装;这里只说一下Cassandra的安装过程;

5. 数据类型

下面只是简单介绍了一下,具体用法请查看官方文档:https://cassandra.apache.org/doc/latest/cql/types.html#grammar-token-collection_type

CREATE TYPE phone (
    country_code int,
    number text,
)
CREATE TYPE address (
    street text,
    city text,
    zip text,
    phones map<text, phone>
)
CREATE TABLE user (
    name text PRIMARY KEY,
    addresses map<text, frozen<address>>
)
INSERT INTO user (name, addresses)
          VALUES ('z3 Pr3z1den7', {
              'home' : {
                  street: '1600 Pennsylvania Ave NW',
                  city: 'Washington',
                  zip: '20500',
                  phones: { 'cell' : { country_code: 1, number: '202 456-1111' },
                            'landline' : { country_code: 1, number: '...' } }
              },
              'work' : {
                  street: '1600 Pennsylvania Ave NW',
                  city: 'Washington',
                  zip: '20500',
                  phones: { 'fax' : { country_code: 1, number: '...' } }
              }
          })
CREATE TABLE durations (
    event text,
    duration tuple<int, text>,
)
INSERT INTO durations (event, duration) VALUES ('ev1', (3, 'hours'));
6. 基本操作

具体教程可以参考w3cshool教程https://www.w3cschool.cn/cassandra/cassandra_create_keyspace.html。这里只介绍几种常用基本命令:

7. 常见错误解决方法
8. Cassandra-java基本操作

可以参考http://www.cnblogs.com/youzhibing/p/6607082.html

9. Cassandra高级操作

可以参考: cassandra高级操作之索引、排序以及分页


参考文章:

上一篇 下一篇

猜你喜欢

热点阅读