Cassandra--CQL

  本篇博客主要讲Cassandra的基本操作,主要是通过cqlsh客户端命令。这个也是我们常用的基本操作,后面再会讲解一下通过java api的方式操作Cassandra。
下面让我们一起来熟悉吧!

启动cqlsh

  • 启动cqlsh,如果rpc_address不是设置为0.0.0.0,直接输入cqlsh是会拒绝连接的
    Connection error: (‘Unable to connect to any servers’, {‘127.0.0.1’: error(111, “Tried connecting to [(‘127.0.0.1’, 9042)]. Last error: Connection refused”)})

  • 需要指定ip,具体操作如下图(我们上篇博客设置为节点的ip)

Keyspace键空间操作

查看所有keyspace

  • 命令:describe keyspaces 或者 desc keyspaces;
    系统默认了五个键空间:system_traces system_schema system_auth system system_distributed
    系统默认表

查看某个keyspace

  • 命令:describe keyspace keyspacesName 或者 desc keyspace keyspacesName
    查看某个keyspace

创建keyspace

  • 注意:cassandra关键字不区分大小写

    1
    2
    cqlsh> CREATE KEYSPACE IF NOT EXISTS testSpace                 
    ... WITH REPLICATION = {'class': 'SimpleStrategy','replication_factor':1};

    创建一个自己的keyspace

修改删除keyspace

  • 命令:alter keyspace keyspaceName with replication ...
  • 删除:drop keyspace keyspaceName

    1
    2
    3
    cqlsh> alter keyspace testspace 
    ... with replication={'class': 'SimpleStrategy', 'replication_factor':2};
    cqlsh> drop keyspace testspace;

    修改删除keyspace

Table

创建表

  • 注意:table必须定义一个PRIMARY KEY。一个PRIMARY KEY可以由一个或多个columns组成

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    cqlsh> use testSpace;
    cqlsh:testspace> create table table1(
    ... id int primary key,
    ... name text
    ... );

    ## 也可以写出
    cqlsh> use testSpace;
    cqlsh:testspace> create table table1(
    ... id int ,
    ... name text ,
    ... primary key(id) ,
    ... );

查看表结构

  • desc或者describe都行:desc table tableName

修改表

  1. 修改表的属性:ALTER TABLE <tableName> WITH Properties;

  2. 增加表的列:ALTER TABLE <tableName> ADD columnName columnType;

  3. 增加表的列:ALTER TABLE <tableName> DROP columnName;

插入表数据

  • 和sql语句相同:insert into table(column,...) values(value,...)

删除表数据

  • 命令:delete from table where ...,必须要有where,例如
    1
    delete from table1 where id = 1;

清空表数据:

  • 命令:TRUNCATE table,RUNCATE语句将会完全删除所有table的数据

index索引

function

type

当前网速较慢或者你使用的浏览器不支持博客特定功能,请尝试刷新或换用Chrome、Firefox等现代浏览器