cassandra数据库使用(二)–cql进行数据基本操作

cassandra数据库使用(二)–cql进行数据基本操作

文章目录

  • cassandra数据库使用(二)--cql进行数据基本操作
      • 一、介绍
      • 二、cassandrar的库表定义
        • 2.1 keyspace
        • 2.2 table
      • 三、cassandra的数据类型
      • 四、创建cassandra的cql会话连接
      • 五、keyspace操作
        • 5.1 创建keyspace
        • 5.2 keyspace查看
        • 5.3 删除keyspace
      • 六、table操作
      • 七、数据操作
      • 八、数据的上传和下载到文件

一、介绍

这里介绍cassandra使用cql(cassandra query language)进行表和数据操作。

二、cassandrar的库表定义

cassandra是面向列的分布式的nosql数据库。定义和mysql类似。

2.1 keyspace

相当于数据库schema

2.2 table

和mysql中的table定义一样

三、cassandra的数据类型

cassandra数据类型除了基本类型外,还添加了集合类型,如list、set、map。

下面列出常用的类型(第一列为cassandra中的类似,第二列为相应描述,第三列为java中的类型),

################## 常用字段类型 ##########################
int 32位整数 int
bigint 64整数 long
float 32位浮点数 flaot
counter 计数器,支持原子性增减,不能直接赋值 long
double 64位浮点数 double
boolean 布尔  boolean
decimal 高精度小数  BigDecimal
list 列表 List
set 集合 Set
map 键值对 map
text utf8编码的字符串 String
varchar 与text一样
timestamp 日期 Date
############################################################

四、创建cassandra的cql会话连接

切到cassandra安装根目录下

命令:
bin/cqlsh host port 

参数:
-e 要执行的语句
-u 用户名
-p 密码
-k 指定keyspace

如:

#!/usr/bin/env bash

cqlsh_bin="cassandra/bin/cqlsh"
host="192.168.0.1"
port=9042
cqlsh_conn="${cqlsh_bin} ${host} ${port}"

五、keyspace操作

5.1 创建keyspace

命令为: create keyspace keyspace名称 with 参数

如:

${cqlsh_conn} -e "create keyspace key_space_2
                        with replication={'class':'SimpleStrategy', 'replication_factor':2 }
                        and durable_writes=true"

参数解析:

class:SimpleStrategy(简单策略,一个数据中心) NetworkTopologyStrategy(网络拓扑策略,多个数据中心)
replication_factor:副本数
durable_writes:是否持久写入,默认为true

5.2 keyspace查看

查看指定keyspace命令: desc keyspace名称
如:

${cqlsh_conn} -e "desc key_space_1;"

查看有哪些keyspace命令: desc keyspaces

${cqlsh_conn} -e "desc keyspaces;"

5.3 删除keyspace

命令:drop keyspace keyspace名称

如:

${cqlsh_conn} -e "drop keyspace key_space_1;"

六、table操作

包括表的查看、创建和删除、字段的增加和修改、索引的创建和删除。
如下(详情请查看注释,keyspace也可通过-k参数指定):

################################### table ##################################
#查看指定table
${cqlsh_conn} -e "desc key_space_1.stu;"

#创建table stu
cassandra_create_stu_table(){
     
    ${cqlsh_conn} <<EOF
        CREATE TABLE key_space_1.stu (
            stu_id int PRIMARY KEY,
            stu_age int,
            stu_name text,
            school map,
            cource_set set,
            teacher_list list
        );
EOF
}
cassandra_create_stu_table

#删除指定table
${cqlsh_conn} -e "drop table key_space_1.stu;"

#修改table 增加字段
${cqlsh_conn} -e "alter table key_space_1.stu add stu_age_1 text;"

#修改table 删除字段
${cqlsh_conn} -e "alter table key_space_1.stu drop stu_age_1;"

#创建索引
${cqlsh_conn} -e "create index stu_name_idx on key_space_1.stu(stu_name);"

#删除索引
${cqlsh_conn}  -e "drop index key_space_1.stu_name_idx;"
###########################################################################

七、数据操作

包括数据的增删改查,详情查看注释,如下:

################################## data  ###########################################
#插入数据
${cqlsh_conn}  -e "insert into key_space_1.stu(stu_id, stu_age, stu_name) values(1, 18,'apple1');"

#更新数据
${cqlsh_conn}  -e "update key_space_1.stu set stu_age=19 where stu_id=1;"

#删除数据
${cqlsh_conn}  -e "delete from key_space_1.stu where stu_id=1;"

#删除指定表所有数据
${cqlsh_conn}  -e "truncate key_space_1.stu;"

#删除数据 指定列
${cqlsh_conn}  -e "delete stu_age from key_space_1.stu where stu_id=1;"

#查看数据
${cqlsh_conn}  -e "select * from key_space_1.stu;"
############################################################################

八、数据的上传和下载到文件

可以通过文件,将数据上传到cassandra中。可从cassandra下载数据到文件中。
操作如下:

################################# import export data ###########
stu_file="tmp/stu_file.csv"

#下载数据到指定csv文件
${cqlsh_conn} -e "copy key_space_1.stu(stu_id, stu_age, stu_name) to '${stu_file}'";

#下载数据到指定csv文件 参数: pagesize=单个页面的行数,默认1000  encoding=字符编码,默认utf8 maxoutputsize=单个文件最大行数, 默认-1,不限制
${cqlsh_conn} -e "copy key_space_1.stu(stu_id, stu_age, stu_name) to '${stu_file}' with maxoutputsize=2";

#上传数据到表中 参数: maxrows=导入最大行数 skiprows=跳过初始行数 skipcols=要忽略的列名,以逗号分隔
${cqlsh_conn} -e "copy key_space_1.stu(stu_id, stu_age, stu_name) from '${stu_file}' with maxrows=5";
##############################################################################################

你可能感兴趣的:(cassandra)