$ netstat -nltp |grep 9160 查看端口
$ bin/./cqlsh
建一个keyspace--表的命名空间:
CREATE KEYSPACE mykeyspace
WITH replication = {'class':'SimpleStrategy', 'replication_factor': 1};
然后切换到该keyspace:
cqlsh> use mykeyspace ;
接着建表(叫做表可能不太准确):
create TABLE users(
userid intPRIMARY KEY,
fname text,
lname text
);
需要注意的是,如果要执行带where条件的查询,那么条件中指定的列必须先建索引,否则会出错:
select * from users where lname = 'smith';
InvalidRequest: code=2200 [Invalid query]message="No secondary indexes on the restricted columns support theprovided operators: "
使用exit或quit可以退出cqlsh。
cassandra-cli 操作
首先启动cassandra,然后运行bin\cassandra-cli.bat启动客户端,默认hostname为localhost,port为9160
配置文件主要是conf\cassandra.yaml,就讲几个用得到的参数,下面给的值都是默认值。
1、cluster_name: 'Test Cluster' 这个是节点名称,单节点就不需要考虑那么多了
2、data_file_directories:'$CASSANDRA_HOME/data/data' 数据文件存储目录
3、commitlog_directory:'$CASSANDRA_HOME/data/commitlog' 日志文件存储目录
4、saved_caches_directory:'$CASSANDRA_HOME/data/saved_caches'缓存文件存储目录
5、rpc_address: localhost 远程连接的时候需要改成数据库所在IP
6、rpc_port: 9160 端口号,cassandra client使用
7、rpc_keepalive: true 是否允许远程连接
1、connect
connect hostname/port;
2、consistencylevel
详细信息参考http://www.doc88.com/p-495509829231.html
由于用的是单节点,所以我们设该值为one就可以了
[default@unknown] consistencylevel as one; Consistency level is set to 'ONE'.
3、create keyspace
[default@unknown] create keyspace keyspace1 with placement_strategy='SimpleStrat egy' and strategy_options={replication_factor:1}; 6a9f1cd8-758d-3a9a-9f0a-e799b00bb159
单节点replication_factor设为1就行了。
4、use
[default@unknown] use keyspace1; Authenticated to keyspace: keyspace1
5、create column family
column family分为Standard和Super两种类型,默认情况下是Standard。不知道的可以先了解一下cassandra的数据存储结构。
创建standard类型:
[default@keyspace1] create column family standardFamily with comparator=UTF8Type ; b7f2a26b-1e36-31e0-a7c2-7d32daf041fd
某一个Key内的所有Column都是按照它的Name来排序的,comparator是设置排序类型。(关于排序前面有讲到,请自行查阅)
创建super类型:
[default@keyspace1] create column family superFamily with comparator = UTF8Type and subcomparator = UTF8Type and column_type = 'Super'; 2bf4b959-6199-321b-8e56-c8cb2f653ae1
在super类型中,comparator按照所有superColumn的key来排序,subcomparator按照superColumn中所有column的name排序。
6、set
插入standard类型的数据:
[default@keyspace1] set standardFamily['key1']['cloumnName1'] = 'columnValue1'; cannot parse 'columnValue1' as hex bytes
我们可以看到不能解析,这时候需要调用ascii()函数,utf8()函数等函数来设置数据类型,cassandra中的数据类型有 bytes, integer, long, int, lexicaluuid, timeuuid, utf8, ascii, double, countercolumn。
[default@keyspace1] set standardFamily['key1']['cloumnName1'] = ascii('columnValue1'); org.apache.cassandra.serializers.MarshalException: cannot parse 'key1' as hex bytes
可以看到key也不能解析,事实上key,column_name,column_value都是需要函数解析的
[default@keyspace1] set standardFamily[utf8('key1')][utf8('columnName1')] = utf8 ('columnValue1'); Value inserted. Elapsed time: 52 msec(s).
成功插入,但是这样显然很麻烦,我们可以用assume来设置数据类型。
7、assume
格式:assume column family validator/comparator/keys/sub_comparator as type;
[default@keyspace1] assume standardFamily keys as utf8; Assumption for column family 'standardFamily' added successfully.
设置key的数据类型为utf8。
[default@keyspace1] assume standardFamily validator as utf8; Assumption for column family 'standardFamily' added successfully.
设置value的数据类型为utf8。
至于comparator/sub_comparator就是指定排序的类型了。(前面有讲到排序类型)
[default@keyspace1] set standardFamily['key2']['columnName2'] = 'columnValue2'; Value inserted. Elapsed time: 1.85 msec(s).
这样如果也对name设置了排序类型的话就可以直接插入了(提示:如果column_name没有设置排序类型,column_name是仍旧需要函数解析的)
并且assume设置的验证类型在关闭服务器后就会失效,所以我们永久的方法就是直接修改column family的设置,看下面的update。
8、update column family
[default@keyspace1] update column family standardFamily with key_validation_clas s = UTF8Type and default_validation_class = UTF8Type; fe850dbb-c170-3639-846d-2ce3ef975462
这样设置就永久保存了。
9、describe
我们来看下standradFamily的配置情况。
[default@keyspace1] describe standardFamily; WARNING: CQL3 tables are intentionally omitted from 'describe' output. See https://issues.apache.org/jira/browse/CASSANDRA-4377 for details. ColumnFamily: standardFamily Key Validation Class: org.apache.cassandra.db.marshal.UTF8Type Default column value validator: org.apache.cassandra.db.marshal.UTF8Type Cells sorted by: org.apache.cassandra.db.marshal.UTF8Type GC grace seconds: 864000 Compaction min/max thresholds: 4/32 Read repair chance: 0.0 DC Local Read repair chance: 0.1 Caching: KEYS_ONLY Default time to live: 0 Bloom Filter FP chance: default Index interval: default Speculative Retry: NONE Built indexes: [] Compaction Strategy: org.apache.cassandra.db.compaction.SizeTieredCompacti onStrategy Compression Options: sstable_compression: org.apache.cassandra.io.compress.LZ4Compressor
可以看到我们的设置都生效了。
10、get
获取key对应的value
[default@keyspace1] get standardFamily['key1']; => (name=columnName1, value=columnValue1, timestamp=1448367633646000) Returned 1 results. Elapsed time: 193 msec(s).
获取key下column_name对应的value
[default@keyspace1] get standardFamily['key1']['columnName1']; => (name=columnName1, value=columnValue1, timestamp=1448367633646000) Elapsed time: 193 msec(s).
11、list
获取某个column family中的所有数据,也可以用limit来限制查询的数量,默认100
[default@keyspace1] list standardFamily; Using default limit of 100 Using default cell limit of 100 ------------------- RowKey: 3 => (name=3, value=3, timestamp=1448369924110000) ------------------- RowKey: key1 => (name=columnName1, value=columnValue1, timestamp=1448367633646000) ------------------- RowKey: 1 => (name=1, value=1, timestamp=1448368544230000) ------------------- RowKey: key2 => (name=2, value=columnValue2, timestamp=1448369767734000) => (name=columnName2, value=columnValue2, timestamp=1448369748230000) 4 Rows Returned. Elapsed time: 270 msec(s).