一、安裝
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 Memtable-LocationInfo@31556811(227 bytes,4 operations)
INFO 09:33:34,859 Writing Memtable-LocationInfo@31556811(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 Memtable-LocationInfo@22975191(53 bytes, 2operations)
INFO 09:33:35,734 Writing Memtable-LocationInfo@22975191(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;
--查詢結果如下:
[default@demo] 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';
--結果如下:
[defualt@demo] get users where full_name='a';
No indexed columns present in index clause with operator EQ
--退出
quit;