和cassandra数据库交互的方式有两种,一种是通过类似于cassandra-cli命令的thrift api,或者通过cassandra提供的cql(cassandra query lanugage),.

注意:cassandra-cli客户端命令从cassandra V2.2已经弃用,所以想使用cassandra-cli命令的话只能安装cassandra V2.2之前的版本。cassandra-cli命令比较难懂,和传统的sql有很大的区别,对于我们学习起来是比较头疼的事情。cql api的语法风格类似于sql,该api屏蔽了cassandra底层架构,将底层的数据结构以sql的形式展现出来。推荐学习cassandra的时候一定好好研究一下该thrift api的使用,cassandra-cli可以帮助我们深入了解cassandra的内部存储结构。

1. keyspace(键空间)

首先查看当前cluster默认的data center

[cassandra@sht-sgmhadoopcm- bin]$ nodetool status
Datacenter: EAST
================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
-- Address Load Tokens Owns (effective) Host ID Rack
UN 172.16.101.54 51.66 KB 100.0% d821e5e5-e99d-41a9-b11f-a0fd0d3d9b05 RAC1

可以看到当前集群默认有一个data center EAST,当前cassandra节点在RAC1机柜,该data center的定义涉及到两个配置文件

$CASSANDRA_HOME/conf/cassandra.yaml的“endpoint_snitch: PropertyFileSnitch“参数

$CASSANDRA_HOME/conf//cassandra-topology.properties

查看创建keyspace的语法并创建 mytest keyspace

[default@unknown] help create keyspace;
...................
Examples:
create keyspace Keyspace2
with placement_strategy = 'org.apache.cassandra.locator.SimpleStrategy'
and strategy_options = {replication_factor:4};
create keyspace Keyspace3
with placement_strategy = 'org.apache.cassandra.locator.NetworkTopologyStrategy'
and strategy_options={DC1:2, DC2:2};
create keyspace Keyspace4
with placement_strategy = 'org.apache.cassandra.locator.OldNetworkTopologyStrategy'
and strategy_options = {replication_factor:1}; [default@unknown] create keyspace mytest with placement_strategy = 'org.apache.cassandra.locator.NetworkTopologyStrategy' and strategy_options={EAST:1};
78c6f3e9-622c-3ae8-86d2-7733518ca7ff [default@unknown] describe mytest; WARNING: CQL3 tables are intentionally omitted from 'describe' output.
See https://issues.apache.org/jira/browse/CASSANDRA-4377 for details. Keyspace: mytest:
Replication Strategy: org.apache.cassandra.locator.NetworkTopologyStrategy
Durable Writes: true
Options: [EAST:1]
Column Families:

2. column family(列族)

[default@unknown] use mytest;
[default@mytest] create column family users with column_type=Standard and comparator=UTF8Type and key_validation_class=UTF8Type and default_validation_class=UTF8Type;
bd4ffef6-9e93-38ec-a4fc-62b4fc00604c
[default@mytest] describe users; WARNING: CQL3 tables are intentionally omitted from 'describe' output.
See https://issues.apache.org/jira/browse/CASSANDRA-4377 for details. ColumnFamily: users
Key Validation Class: org.apache.cassandra.db.marshal.UTF8Type
Default column value validator: org.apache.cassandra.db.marshal.UTF8Type
Cells sorted by: org.apache.cassandra.db.marshal.UTF8Type
GC grace seconds: 864000
Compaction min/max thresholds: 4/32
Read repair chance: 0.0
DC Local Read repair chance: 0.1
Caching: KEYS_ONLY
Default time to live: 0
Bloom Filter FP chance: default
Index interval: default
Speculative Retry: NONE
Built indexes: []
Compaction Strategy: org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy
Compression Options:
sstable_compression: org.apache.cassandra.io.compress.LZ4Compressor

通过上述创建users列族可以看到,我们仅仅是指定了列族级别的属性(相当于表的属性),但是该列族没有任何列。

我们也可以通过meta_data选项在创建column family的时候加入预定义的列以及索引信息

[default@mytest] create column family students
... with column_type = 'Standard'
... and comparator = UTF8Type
... and key_validation_class=UTF8Type
... and default_validation_class=UTF8Type
... and column_metadata = [
... {column_name: age, validation_class: UTF8Type}
... {column_name: birthday, validation_class: UTF8Type, index_type: KEYS,index_name: IDXbirthday}
... {column_name: first, validation_class: UTF8Type}
... {column_name: last, validation_class: UTF8Type}
... ];
[default@mytest] describe students; WARNING: CQL3 tables are intentionally omitted from 'describe' output.
See https://issues.apache.org/jira/browse/CASSANDRA-4377 for details. ColumnFamily: students
Key Validation Class: org.apache.cassandra.db.marshal.UTF8Type
Default column value validator: org.apache.cassandra.db.marshal.UTF8Type
Cells sorted by: org.apache.cassandra.db.marshal.UTF8Type
GC grace seconds: 864000
Compaction min/max thresholds: 4/32
Read repair chance: 0.0
DC Local Read repair chance: 0.1
Caching: KEYS_ONLY
Default time to live: 0
Bloom Filter FP chance: default
Index interval: default
Speculative Retry: NONE
Built indexes: [students.IDXbirthday]
Column Metadata:
Column Name: first
Validation Class: org.apache.cassandra.db.marshal.UTF8Type
Column Name: birthday
Validation Class: org.apache.cassandra.db.marshal.UTF8Type
Index Name: IDXbirthday
Index Type: KEYS
Column Name: age
Validation Class: org.apache.cassandra.db.marshal.UTF8Type
Column Name: last
Validation Class: org.apache.cassandra.db.marshal.UTF8Type
Compaction Strategy: org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy
Compression Options:
sstable_compression: org.apache.cassandra.io.compress.LZ4Compressor

其中

  • column_type定义列的类型,默认为标准列
  • comparator 在查询数据时返回的列的排序方式,注意这里是列名,不是列的值,可以根据long、byte、UTF8等其他排序方式进行,同时也意味着当列名显示在命令行界面时,它们将显示为UTF8Type(可读)文本。在Cassandra中是无法按照关系型数据中的值来排序的,必须按照列名来排序,以便能从一个很宽的行里高效取出一列,而无需把每列都读进内存。
  • key_validation_class 定义row key(主键)的数据类型
  • default_validation_class 定义value的数据类型

3. set&get/list&del/count(插入更新&查询&删除&统计)

语法 set column family[‘row key‘]['key']='value'

[default@mytest] set users['zhangpeng']['first']='zhang';
Value inserted.
Elapsed time: 3.83 msec(s).
[default@mytest] set users['zhangpeng']['last']='peng';
Value inserted.
Elapsed time: 2.69 msec(s).
[default@mytest] set users['zhangpeng']['age']='';
Value inserted.
Elapsed time: 1.97 msec(s).
[default@mytest] set users['wangxing']['first']='wang';
Value inserted.
Elapsed time: 1.64 msec(s).
[default@mytest] set users['wangxing']['last']='xing';
Value inserted.
Elapsed time: 1.4 msec(s).
[default@mytest] set users['wangxing']['age']='';
Value inserted.
Elapsed time: 1.38 msec(s).
[default@mytest] set users['wangxing']['sex']='male';
Value inserted.
Elapsed time: 1.38 msec(s). [default@mytest] get users['zhangpeng'];
=> (name=age, value=18, timestamp=1527690907938000)
=> (name=first, value=zhang, timestamp=1527690887611000)
=> (name=last, value=peng, timestamp=1527690898435000)
Returned 3 results.
Elapsed time: 3.68 msec(s). [default@mytest] get users['wangxing'];
=> (name=age, value=19, timestamp=1527690946003000)
=> (name=first, value=wang, timestamp=1527690926424000)
=> (name=last, value=xing, timestamp=1527690934083000)
=> (name=sex, value=male, timestamp=1527690962938000)
Returned 4 results.
Elapsed time: 3.46 msec(s). [default@mytest] set users[wangxing]['age']='';
Value inserted.
Elapsed time: 10 msec(s).
[default@mytest] get users['wangxing'];
=> (name=age, value=17, timestamp=1527691267345000)
=> (name=first, value=wang, timestamp=1527690926424000)
=> (name=last, value=xing, timestamp=1527690934083000)
=> (name=sex, value=male, timestamp=1527690962938000)
Returned 4 results.
Elapsed time: 3.67 msec(s).
[default@mytest] del users['wangxing']['sex'];
cell removed.
Elapsed time: 14 msec(s).
[default@mytest] get users['wangxing'];
=> (name=age, value=17, timestamp=1527691267345000)
=> (name=first, value=wang, timestamp=1527690926424000)
=> (name=last, value=xing, timestamp=1527690934083000)
Returned 3 results.
Elapsed time: 4.84 msec(s). [default@mytest] count users['wangxing'];
3 cells
[default@mytest] count users['zhangpeng'];
3 cells
[default@mytest] count users['wangxing'];
3 cells
[default@mytest] list users;
Using default limit of 100
Using default cell limit of 100
-------------------
RowKey: wangxing
=> (name=age, value=19, timestamp=1527692441512000)
=> (name=first, value=wang, timestamp=1527692429554000)
=> (name=last, value=xing, timestamp=1527692435344000)
-------------------
RowKey: zhangpeng
=> (name=age, value=18, timestamp=1527692421898000)
=> (name=first, value=zhang, timestamp=1527692409752000)
=> (name=last, value=peng, timestamp=1527692416150000) 2 Rows Returned.
Elapsed time: 6.55 msec(s).

4. index

默认情况下我们不可以通过where限制条件查询数据,否则会报如下错误,我们可以为需要where条件的列增加索引,但是只局限于where的等值查询

[default@mytest] get users where age='';
No indexed columns present in index clause with operator EQ
[default@mytest] update column family users with column_metadata=[{column_name : age, validation_class : UTF8Type, index_type : KEYS,index_name : IDXage}];
b2a8c0af-4162-3590-acda-70ccdb7d363c
[default@mytest] get users where age='';
-------------------
RowKey: wangxing
=> (name=age, value=17, timestamp=1527692751954000)
=> (name=first, value=wang, timestamp=1527692429554000)
=> (name=last, value=xing, timestamp=1527692435344000) 1 Row Returned.
Elapsed time: 70 msec(s).

通过cassandra-cli客户端了解cassandra的内部数据结构的更多相关文章

  1. redis底层设计(一)——内部数据结构

    redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set ...

  2. redis内部数据结构

    redis内部数据结构,是指redis在自身的构建中,基于这些特定的内部数据结构进行的. 简单动态字符串:Simple Dynamic String 双端链表 字典:Dictonary 跳跃表:ski ...

  3. redis 源码阅读 内部数据结构--字符串

    redis的内部数据结构主要有:字符串,双端链表,字典,跳跃表. 这里主要记录redise字符串的设计.相关的源码位于:src/sds.h 和 src/sds.c.   一 字符串 sds的结构体 s ...

  4. 你真的懂redis的数据结构了吗?redis内部数据结构和外部数据结构揭秘

    Redis有哪些数据结构? 字符串String.字典Hash.列表List.集合Set.有序集合SortedSet. 很多人面试时都遇到过这种场景吧? 其实除了上面的几种常见数据结构,还需要加上数据结 ...

  5. [转]Redis内部数据结构详解-sds

    本文是<Redis内部数据结构详解>系列的第二篇,讲述Redis中使用最多的一个基础数据结构:sds. 不管在哪门编程语言当中,字符串都几乎是使用最多的数据结构.sds正是在Redis中被 ...

  6. redis内部数据结构和外部数据结构揭秘

    Redis有哪些数据结构? 字符串String.字典Hash.列表List.集合Set.有序集合SortedSet. 很多人面试时都遇到过这种场景吧? 其实除了上面的几种常见数据结构,还需要加上数据结 ...

  7. Redis学习笔记-Redis内部数据结构

    Redis内部数据结构 Redis和其他key-value数据库的很大区别是它支持非字符串类型的value值.它支持的value值的类型如下: sds (simple dynamic string) ...

  8. 你真的懂了redis的数据结构吗?redis内部数据结构和外部数据结构揭秘

    原文链接:https://mp.weixin.qq.com/s/hKpAxPE-9HJgV6GEdV4WoA Redis有哪些数据结构? 字符串String.字典Hash.列表List.集合Set.有 ...

  9. 探索Redis设计与实现6:Redis内部数据结构详解——skiplist

    本文转自互联网 本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutorial ...

随机推荐

  1. CPU指令集设计RISC和CISC

    CPU指令集 硬件实现具有速度快,成本高,灵活性差,软件实现与之相反.因此出现频率高的基本功能首选硬件实现.指令集的不同反映了设计原理.制造技术和系统类别. RISC 精简指令集计算机(Reduced ...

  2. C#判断数据类型的简单示例代码

    ; Console.WriteLine( "i is an int? {0}",i.GetType()==typeof(int)); Console.WriteLine( &quo ...

  3. perl hash array 嵌套 push

    $hash{"A"}=["pp"];想变成:$hash{"A"}=["p","q"];因为 $has ...

  4. Spring (一)

    Spring是一个开源框架,是一个基于IOC和AOP来架构多层的JavaEE 架构 默认是单例模式 IOC就是 Inversion of Control public class Girl { pri ...

  5. eclipse maven maven-compiler-plugin 报错 完全解决

    报错如下: Maven install失败 Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:comp ...

  6. Windows下及Mac下的IntelliJ IDEA快捷键

    Mac 键盘符号说明 ⌘ == Command ⇧ == Shift ⇪ == Caps Lock ⌥ == Option ⌃ == Control ↩ == Return/Enter ⌫ == De ...

  7. [转][osg]关于PagedLOD 加载卸载机制

    你的PagedLOD 为什么没有卸载 转自:http://bbs.osgchina.org/forum.php?mod=viewthread&tid=7612&highlight=Pa ...

  8. R语言barplot双坐标作图

    需要注意的是,设置其中的柱子的宽度,间隔的宽度.有公式如下 width为柱子的宽度 space为间隔宽度 barnumbers 为柱子数量 那么xlim的设置右侧范围为:(width + space) ...

  9. arcpy导入错误 问题 “ImportError: No module named arcpy”

    如果阁下也出现如下图的错误,用本文的方法也许可以解决问题 首先,打开你python的安装位置,如下图所示的路径,找到desktop10.3.pth文件,打开查看,将你arcgis的相关路径,共3个绝对 ...

  10. jS冒泡优化

    <script>    //冒泡优化    将一个数组中的值从小到大排列    var arr=[65,85,12,36,75,46,50];    var sorted=true;   ...