一、安装
1、安装cassandra
1.1 下载安装程序
在http://cassandra.apache.org/下载Cassandra的安装程序;
1.2 安装配置
1)解压下载后的程序apache-cassandra-0.7.3-bin.tar.gz到某个目录,如d:\cassandra;
2)修改conf目录下配置文件
A)使用记事本打开cassandra.yaml文件,修改如下参数:
data_file_directories:
- /var/lib/cassandra/data
为 - d:\cassandra\data
注意:-后面需要有空格
commitlog_directory: /var/lib/cassandra/commitlog
为:commitlog_directory: d:\cassandra\commitlog
saved_caches_directory: /var/lib/cassandra/saved_caches
为:saved_caches_directory: d:\cassandra\saved_caches
B)使用记事本打开log4j-server.properties文件,修改如下参数:
log4j.appender.R.File=/var/log/cassandra/system.log
为:log4j.appender.R.File=D:\Cassandra\logs
2、安装JRE
2.1 下载安装程序
在http://www.oracle.com/technetwork/java/javase/downloads/index.html上下载jre的安装程序;
jre和jdk区别:jre只是java环境,jdk包含了开发工具和jre;
2.2 直接双击执行jre-6u25-windows-i586.exe进行安装,默认安装在C:\Program Files\Java\jre6目录即可;
3、配置环境变量
3.1 打开windows的环境变量配置窗口
鼠标右键点击我的电脑,选择属性,在弹出界面点击【高级】页签,再点击【环境变量】按钮;
3.2 配置JAVA_HOME环境变量
在“系统变量”添加JAVA_HOME,点击【系统变量】下方的【新建】按钮,输入
变量名:JAVA_HOME
变量值:C:\Program Files\Java\jre6
3.3 配置Cassandra_HOME环境变量
在“系统变量”添加Cassandra_HOME,点击【系统变量】下方的【新建】按钮,输入
变量名:Cassandra_HOME
变量值:D:\Cassandra\apache-cassandra-0.7.3
4、启动Cassandra
开始运行中敲入cmd回车打开命令窗口,敲入cd D:\Cassandra\apache-cassandra-0.7.3\bin回车,敲入d:回车,转到D:\Cassandra\apache-cassandra-0.7.3\bin目录后,直接敲入cassandra就可以启动cassandra服务了,如下:
D:\Cassandra\apache-cassandra-0.7.3\bin>cassandra
Starting Cassandra Server
INFO 09:33:34,375 Logging initialized
INFO 09:33:34,390 Heap size: 1070399488/1070399488
INFO 09:33:34,390 JNA not found. Native methods will be disabled.
INFO 09:33:34,406 Loading settings from file:/D:/Cassandra/apache-cassandra-0.7.3/conf/cassandra.yaml
INFO 09:33:34,562 DiskAccessMode ‘auto‘ determined to be standard, indexAccessMode is standard
INFO 09:33:34,765 Creating new commitlog segment d:\cassandra\commitlog\CommitLog-1306200814765.log
INFO 09:33:34,781 Couldn‘t detect any schema definitions in local storage.
INFO 09:33:34,781 Found table data in data directories. Consider using JMX to call org.apache.cassandra.service.StorageService.loadSchemaFromYaml().
INFO 09:33:34,796 No commitlog files found; skipping replay
INFO 09:33:34,828 Upgrading to 0.7. Purging hints if there are any. Old hints will be snapshotted.
INFO 09:33:34,828 Cassandra version: 0.7.3
INFO 09:33:34,828 Thrift API version: 19.4.0
INFO 09:33:34,843 Loading persisted ring state
INFO 09:33:34,843 Starting up server gossip
INFO 09:33:34,843 switching in a fresh Memtable for LocationInfo at CommitLogContext(file=‘d:\cassandra\commitlog\CommitLog-1306200814765.log‘, position=700)
INFO 09:33:34,859 Enqueuing flush of [email protected](227 bytes,4 operations)
INFO 09:33:34,859 Writing [email protected](227 bytes, 4 operations)
INFO 09:33:35,656 Completed flushing d:\cassandra\data\system\LocationInfo-f-1-Data.db (335 bytes)
WARN 09:33:35,718 Generated random token 152144483071909207899303400696660552068. Random tokens will result in an unbalanced ring; see http://wiki.apache.org/cassandra/Operations
INFO 09:33:35,718 switching in a fresh Memtable for LocationInfo at CommitLogContext(file=‘d:\cassandra\commitlog\CommitLog-1306200814765.log‘, position=996)
INFO 09:33:35,718 Enqueuing flush of [email protected](53 bytes, 2operations)
INFO 09:33:35,734 Writing [email protected](53 bytes, 2 operations)
INFO 09:33:35,828 Completed flushing d:\cassandra\data\system\LocationInfo-f-2-Data.db (163 bytes)
INFO 09:33:35,843 Will not load MX4J, mx4j-tools.jar is not in the classpath
INFO 09:33:35,890 Binding thrift service to localhost/127.0.0.1:9160
INFO 09:33:35,890 Using TFastFramedTransport with a max frame size of 15728640bytes.
INFO 09:33:35,890 Listening for thrift clients...
二、使用
1、连接Cassandra服务
开始运行中敲入cmd回车进入命令窗口,敲入cd D:\Cassandra\apache-cassandra-0.7.3\bin后,在敲入d:回车转到bin目录中
敲入cassandra-cli命令回车,直接敲入如下命令可以连接到本机已经启动的cassandra数据库
--cassandra默认为9160端口,注意后面需要有;结束
connect localhost/9160;
2、创建keyspace
create keyspace keyspace1;
use keyspace1;
3、创建column family
create column family Standard1 with column_type = ‘Standard‘ and comparator = ‘BytesType‘;
describe keyspace keyspace1;
list standard1;
drop column family standard1;
--必须指定column_type和comparator
create column family cf1 with column_type = ‘Standard‘ and comparator = ‘BytesType‘;
create column family cf2 with column_type = ‘Standard‘ and comparator = ‘UTF8Type‘ and rows_cached = 10000;
create column family Super1 with column_type=Super and comparator=BytesType;
--设置列值
set cf1[‘key_1‘][‘column_1‘]=‘value_1‘;
set cf1[‘key_1‘][‘column_2‘]=‘value_2‘;
set cf1[‘key_2‘][‘column_1‘]=‘value_3‘;
set cf1[‘key_2‘][‘column_2‘]=‘value_4‘;
--设置列值
set cf2[‘key_1‘][‘column_1‘]=‘value_1‘;
set cf2[‘key_1‘][‘column_2‘]=‘value_2‘;
set cf2[‘key_2‘][‘column_1‘]=‘value_3‘;
set cf2[‘key_2‘][‘column_2‘]=‘value_4‘;
set super1[‘key_1‘][‘column_1‘]=‘value_1‘;
get cf2[‘key_2‘][‘column_2‘]; --提示错误Column family Super1 may only contain SuperColumns
--取列值
get cf1[‘key_1‘];
--计数
count cf1[‘key_1‘];
count cf1[‘key_2‘];
--取列值
get cf1[‘key_1‘][‘column_1‘];
--删除列值
del cf1[‘key_1‘][‘column_1‘];
list cf2[key_1:];
---二级索引的使用
1、定义:在Cassandra中,对列值(column values)的索引叫做"二级索引",它与列簇(column families)中对key的
索引不同。二级索引允许我们对列值进行查询,并且在读取和写入的时候不会引起操作阻塞。
2、使用方法:
create keyspace demo;
use demo;
--注意大小写,索引birth_date
create column family users with comparator=UTF8Type and column_metadata=
[{column_name:full_name,validation_class:UTF8Type},
{column_name:birth_date,validation_class:LongType,index_type:KEYS}];
--插入数据:
set users[a][full_name]=‘a‘;
set users[a][birth_date]=1977;
set users[b][full_name]=‘b‘;
set users[b][birth_date]=1978;
set users[c][full_name]=‘c‘;
set users[c][birth_date]=1979;
set users[d][full_name]=‘d‘;
set users[d][birth_date]=1977;
--查询birth_date=1977的数据:
get users where birth_date=1977;
--查询结果如下:
[[email protected]] get users where birth_date=1977;
-------------------
RowKey: a
=> (column=birth_date, value=1977, timestamp=1306733443578000)
=> (column=full_name, value=a, timestamp=1306733438406000)
-------------------
RowKey: d
=> (column=birth_date, value=1977, timestamp=1306733577125000)
=> (column=full_name, value=d, timestamp=1306733572687000)
2 Rows Returned.
--多个二级索引使用
set users[a][card_no]=‘111‘;
--更新列簇users设置card_no和birth_date都索引
update column family users with comparator=UTF8Type and column_metadata=
[{column_name:full_name,validation_class:UTF8Type},
{column_name:birth_date,validation_class:LongType,index_type:KEYS},
{column_name:card_no,validation_class:UTF8Type,index_type:KEYS}];
--查询证件号码为111的列
get users where card_no=‘111‘;
get users where card_no=‘111‘ and birth_date=1977;
--虽然full_name没有索引,但是也能够查出结果
get users where card_no=‘111‘ and birth_date=1977 and full_name=‘a‘;
get users where card_no=‘111‘ and full_name=‘a‘;
get users where birth_date=1977 and full_name=‘a‘;
--如果仅仅通过full_name查询则出错
get users where full_name=‘a‘;
--结果如下:
[[email protected]] get users where full_name=‘a‘;
No indexed columns present in index clause with operator EQ
--退出
quit;