Cassandra安装和初次使用
Cassandra安装和初次使用
卡珊德拉(Cassandra)又译卡桑德拉、卡珊卓,为希腊、罗马神话中特洛伊(Troy)的公主,阿波罗(Apollo)的祭司。因神蛇以舌为她洗耳或阿波罗的赐予而有预言能力,又因抗拒阿波罗,预言不被人相信。特洛伊战争后被阿伽门农(Agamemnon)俘虏,并遭克吕泰涅斯特拉(Clytaemnestra)杀害。
依赖环境
The latest version of Java 8, either the Oracle Java Standard Edition
8 or OpenJDK 8. To verify that you have the correct version of java
installed, type java -version. For using cqlsh, the latest version of
Python 2.7. To verify that you have the correct version of Python
installed, type python --version.
事实上如果你安装的是OpenJDK,然后启动Cassandra它会报警告,说不推荐使用OpenJDK,而且JDK的版本要1.8。
安装Cassandra
我选择的是下载二进制的文件,然后解压安装:
tar -xvf apache-cassandra-3.6-bin.tar.gz cassandra
哦,我的Linux发行版是CentOS 7 x64
这样你就已经安装好了Cassandra了,真是方便(日常吐槽FreeBSD,想用最新版本的Cassandra以我的水平还达不到啊)。
其他的就是看你需不需要配置环境变量以及日志了,反正我就马马虎虎开车上路了。点这里查看详细配置。
启动Cassandra
cassandra -h
Usage: /usr/local/apache-cassandra-3.10/bin/cassandra [-f] [-h] [-p pidfile] [-H dumpfile] [-E errorfile]
- -f 是前台启动
- -h 帮助信息
- -R 是以root启动
Cassandra不推荐你以root来启动它,如果你非要这么干,可以,你得用-R选项
这里我用的一下方式启动:
/usr/local/apache-cassandra-3.10/bin/cassandra -f -R
日志信息就不传上来了,反正没有报错我就当它成功了。
连接并使用Cassandra
连接Cassandra的客户端为cqlsh,先查看一下这个命令的用法:
/usr/local/apache-cassandra-3.10/bin/cqlsh -h
Usage: cqlsh.py [options] [host [port]]
CQL Shell for Apache Cassandra
Options:
--version show program's version number and exit
-h, --help show this help message and exit
-C, --color Always use color output
--no-color Never use color output
--browser=BROWSER The browser to use to display CQL help, where BROWSER
can be:
- one of the supported browsers in
https://docs.python.org/2/library/webbrowser.html.
- browser path followed by %s, example: /usr/bin
/google-chrome-stable %s
--ssl Use SSL
-u USERNAME, --username=USERNAME
Authenticate as user.
-p PASSWORD, --password=PASSWORD
Authenticate using password.
-k KEYSPACE, --keyspace=KEYSPACE
Authenticate to the given keyspace.
-f FILE, --file=FILE Execute commands from FILE, then exit
--debug Show additional debugging information
--encoding=ENCODING Specify a non-default encoding for output. (Default:
utf-8)
--cqlshrc=CQLSHRC Specify an alternative cqlshrc file location.
--cqlversion=CQLVERSION
Specify a particular CQL version, by default the
highest version supported by the server will be used.
Examples: "3.0.3", "3.1.0"
-e EXECUTE, --execute=EXECUTE
Execute the statement and quit.
--connect-timeout=CONNECT_TIMEOUT
Specify the connection timeout in seconds (default: 5
seconds).
--request-timeout=REQUEST_TIMEOUT
Specify the default request timeout in seconds
(default: 10 seconds).
-t, --tty Force tty mode (command prompt).
Connects to 127.0.0.1:9042 by default. These defaults can be changed by
setting $CQLSH_HOST and/or $CQLSH_PORT. When a host (and optional port number)
are given on the command line, they take precedence over any defaults.
这里我们可以获取一下信息
- cqlsh可以不接任何选项或者参数启动,默认的是连接127.0.0.1:9042端口
其他的选项就不细说了,反正可以直接这样连,赶快连上去看一下。输入cqlsh
然后回车。
[root@backup ~]# cqlsh
Connected to Test Cluster at 127.0.0.1:9042.
[cqlsh 5.0.1 | Cassandra 3.10 | CQL spec 3.4.4 | Native protocol v4]
Use HELP for help.
cqlsh>
嘻嘻,已经连上去了。
输入几个命令体验一把:
查看集群:
cqlsh> desc cluster; // 查看所在的集群
Cluster: Test Cluster
Partitioner: Murmur3Partitioner
查看表空间(姑且先这么称吧)
cqlsh> desc keyspaces; // 查看表空间
system_schema system_auth system myapp system_distributed system_traces
查看具体的表空间
desc keyspace system_schema; // 相当于查看建表语句吧
查看表空间下有哪些表
desc tables;
查看表空间的某张具体表
desc table system_schema.functions;
在使用cql时,如果你不指定表空间,那么你的语句中就必须显示的使用表空间.某张表
这样的语句,想要直接使用某张表就要选中表空间了。
使用某个表空间
USE 表空间名; // 是不是跟mysql很像啊
创建表空间
CREATE KEYSPACE myapp WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 3};
在这里创建表空间时可以添加某些选项,暂时先这么整,等你你深入之后再研究吧。
使用指定表空间并创建表
cqlsh:system_auth> use myapp;
cqlsh:myapp> CREATE TABLE users (
... id text PRIMARY KEY,
... name text,
... favs map<text, text>
... );
cqlsh:myapp> INSERT INTO users (id, name, favs)
... VALUES ('小明','李小明',{'fruit':'苹果','游戏':'DotA'});
cqlsh:myapp> SELECT * FROM users;
id | favs | name
------+-----------------------------------+--------
小明 | {'fruit': '苹果', '游戏': 'DotA'} | 李小明
(1 rows)
使用where子句查询
cqlsh:myapp> SELECT * FROM user WHERE id="小红";
很不幸,报错了:
'ascii' codec can't encode character u'\u5c0f' in position 72: ordinal not in range(128)
查了一下,这时Python惹得祸。
[root@backup ~]# python
Python 2.7.5 (default, Nov 6 2016, 00:28:07)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> reload(sys)
<module 'sys' (built-in)>
>>> sys.setdefaultencoding('utf-8')
>>> k=u'中'
>>> print str(unicode(k))
中
>>>
这里我在bin目录下的cqlsh.py文件中添加了如下代码
// 大概就是在这个位置加的
49 from uuid import UUID
50
51 reload(sys) // 这行以及下面的一行
52 sys.setdefaultencoding("utf8")
然后不能用cqlsh这个命令了,只能用cqlsh这个脚本来执行了
[root@backup bin]# ./cqlsh.py
Connected to Test Cluster at 127.0.0.1:9042.
[cqlsh 5.0.1 | Cassandra 3.10 | CQL spec 3.4.4 | Native protocol v4]
Use HELP for help.
cqlsh> use myapp ;
cqlsh:myapp> select * from user where id="小红";
SyntaxException: line 1:32 no viable alternative at input ';' (...* from user where id=["小]红";)
cqlsh:myapp> select * from user where id="小红"
... ;
SyntaxException: line 2:0 no viable alternative at input ';' (...* from user where id=["小]红";)
cqlsh:myapp> select * from user where id=小红;
Invalid syntax at line 1, char 29
select * from user where id=小红;
^
cqlsh:myapp> select * from user where id='小红';
InvalidRequest: Error from server: code=2200 [Invalid query] message="unconfigured table user"
cqlsh:myapp> select * from user;
InvalidRequest: Error from server: code=2200 [Invalid query] message="unconfigured table user"
cqlsh:myapp> desc myapp
CREATE KEYSPACE myapp WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '3'} AND durable_writes = true;
CREATE TABLE myapp.users (
id text PRIMARY KEY,
favs map<text, text>,
name text
) WITH bloom_filter_fp_chance = 0.01
AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}
AND comment = ''
AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32', 'min_threshold': '4'}
AND compression = {'chunk_length_in_kb': '64', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor'}
AND crc_check_chance = 1.0
AND dclocal_read_repair_chance = 0.1
AND default_time_to_live = 0
AND gc_grace_seconds = 864000
AND max_index_interval = 2048
AND memtable_flush_period_in_ms = 0
AND min_index_interval = 128
AND read_repair_chance = 0.0
AND speculative_retry = '99PERCENTILE';
cqlsh:myapp> select * from users where id='小红';
id | favs | name
------+-----------------------+--------
小红 | {'香水': '香奈儿5号'} | 楚小红
(1 rows)
cqlsh:myapp> select * from user where id="小红" ;
SyntaxException: line 1:33 no viable alternative at input ';' (...* from user where id=["小]红" ;)
cqlsh:myapp> select * from users where id="小红" ;
SyntaxException: line 1:34 no viable alternative at input ';' (...* from users where id=["小]红" ;)
cqlsh:myapp> select * from users where id='小红';
id | favs | name
------+-----------------------+--------
小红 | {'香水': '香奈儿5号'} | 楚小红
(1 rows)
具体执行细节见以上,然后不能用双引号,可以用单引号;我也是......
这里where子句查询的是id,我猜是自带索引的,那就换个列查一下:
cqlsh:myapp> select * from users where name='李小明';
InvalidRequest: Error from server: code=2200 [Invalid query] message="Cannot execute this query as it might involve data filtering and thus may have unpredictable performance. If you want to execute this query despite the performance unpredictability, use ALLOW FILTERING"
cqlsh:myapp> select * from users where name='李小明' allow FILTERING;
id | favs | name
------+-----------------------------------+--------
小明 | {'fruit': '苹果', '游戏': 'DotA'} | 李小明
(1 rows)
网上说要建索引,可我这里也没有建索引啊
建立索引
cqlsh:myapp> create INDEX on users(name);
cqlsh:myapp> select * from users where name='李小明';
id | favs | name
------+-----------------------------------+--------
小明 | {'fruit': '苹果', '游戏': 'DotA'} | 李小明
(1 rows)
好像建立了索引就不会报上面的那条错误,看来是真的要建立索引啊!
P. S.
各位,我搞错了,原来不用修改cqlsh.py的,在where中查询字符串用单引号就可以不用改文件,加字符设置了,不用~~~~
退出Cassandra
找到pid,然后kill之
ps -aux | grep cassandra
马丹,出来好长一串
以上就是初次使用了,很简单的。
Cassandra安装和初次使用的更多相关文章
- Cassandra安装及其简单试用
官方主页:http://cassandra.apache.org/ 简介: The Apache Cassandra Project develops a highly scalable second ...
- Sublime Text3安装以及初次配置
Sublime Text3安装以及初次配置 工具:官网下载:Sublime Text3 安装:直接运行安装.http://write.blog.csdn.net/postedit 激活:参考文/晚晴幽 ...
- NoSql之旅--Cassandra安装篇(一)
有点迷茫了,头脑中只想起来一句话,"那就去学习吧". 我负责的项目中有一部分用到了Cassandra,当时也看过点,但是并没有太深入的了解,既然"学习劲头"正足 ...
- Redis安装,mongodb安装,hbase安装,cassandra安装,mysql安装,zookeeper安装,kafka安装,storm安装大数据软件安装部署百科全书
伟大的程序员版权所有,转载请注明:http://www.lenggirl.com/bigdata/server-sofeware-install.html 一.安装mongodb 官网下载包mongo ...
- 【Tech】Cassandra安装和启动
1.安装 jre,配置系统环境变量: 2.安装python,配置环境变量: 3.下载cassandra,http://cassandra.apache.org/download/: 4.解压,这里我没 ...
- Git起步--git安装与初次运行git前配置
在你开始使用 Git 前,需要将它安装在你的计算机上. 即便已经安装,最好将它升级到最新的版本. 你可以通过软件包或者其它安装程序来安装,或者下载源码编译安装. 一.Git安装 1. 在linux上安 ...
- Ubuntu上装KVM:安装、初次使用
KVM 是 Linux 内核自带的虚拟机系统, 使用它,你的机器就可以变成几台机了 试用过程如下: 环境: Ubuntu 14.04 64bit 1, KVM需要CPU硬件支持虚拟化,所以首先要确认 ...
- cassandra安装配置
准备运行环境1.1 cassandra可以安装在windows和linux下,本例子安装在centos6.7的环境下.1.2 关闭防火墙.或者开放9042(默认的CQL本地服务端口).9160(默认的 ...
- 转载 | Sublime Text3 安装以及初次配置
本文引自:http://blog.csdn.net/u011272513/article/details/52088800 工具:官网下载:Sublime Text3 安装:直接运行安装.http:/ ...
随机推荐
- 详谈Format String(格式化字符串)漏洞
格式化字符串漏洞由于目前编译器的默认禁止敏感格式控制符,而且容易通过代码审计中发现,所以此类漏洞极少出现,一直没有笔者本人的引起重视.最近捣鼓pwn题,遇上了不少,决定好好总结了一下. 格式化字符串漏 ...
- NOI2016 高中OI生涯的最后一站
你乘坐的航班XXX已经抵达终点站——四川绵阳. “呼——”机舱外的天空灰沉沉的,不禁有些压抑与紧张. 一出机场,就看见南山中学的牌子,黄色衣服的志愿者们,还有热情的老师们. 感觉刚才的情绪又一扫而空了 ...
- iOS音频的后台播放总结
在没有网络的情况下,音频的后台播放比较简单,google一下可以搜到很多资料,但是如果每次歌曲的请求都是通过网络,就不成了,有时可以也扛不了几首,这里总结下实现方法,可以实现像电台一样的功能,后台播 ...
- Windows下将ISO镜像制作成U盘启动的工具(U盘启动工具/UltraISO/Rufus/Universal-USB)
说明:基于Windows的U盘启动制作都是非常的简单,在软件上指定ISO文件之后,一般都是选择写入到哪个U盘即可. 1.UltraISO 2.Rufus 3.Universal-USB 4.大白菜
- 理解Promise简单实现的背后原理
在写javascript时我们往往离不开异步操作,过去我们往往通过回调函数多层嵌套来解决后一个异步操作依赖前一个异步操作,然后为了解决回调地域的痛点,出现了一些解决方案比如事件订阅/发布的.事件监听的 ...
- mybatis学习笔记(六)使用generator生成mybatis基础配置代码和目录结构
原文:http://blog.csdn.net/oh_mourinho/article/details/51463413 创建maven项目 <span style="font-siz ...
- hdu1021(C++)
打表找规律,发现是n%4==2就是yes,否则是no #include<iostream>using namespace std;int main(){ int n; while (cin ...
- EasyMvc入门教程
EasyMvc 希望实现的目标:模块化,快速简单化,满足80%的常见需求.基于.Net Core 2.0.5开发.开发环境:VS2017,运行环境支持Window/Linux. 相关链接: 演示地址: ...
- UIViewController的生命周期及iOS程序运行顺序
当一个视图控制器被创建,并在屏幕上显示的时候. 代码的运行顺序 1. alloc 创建对象,分配空间 2.init (initWit ...
- 机器学习算法实践:朴素贝叶斯 (Naive Bayes)(转载)
前言 上一篇<机器学习算法实践:决策树 (Decision Tree)>总结了决策树的实现,本文中我将一步步实现一个朴素贝叶斯分类器,并采用SMS垃圾短信语料库中的数据进行模型训练,对垃圾 ...