一、启动cassandra客户端
首先启动cassandra,然后运行bin\cassandra-cli.bat启动客户端,默认hostname为localhost,port为9160
F:\apache-cassandra-2.1.11-bin\bin>cassandra-cli Starting Cassandra Client org.apache.thrift.transport.TTransportException: java.net.ConnectException: Conn ection refused: connect at org.apache.thrift.transport.TSocket.open(TSocket.java:187) at org.apache.thrift.transport.TFramedTransport.open(TFramedTransport.ja va:81) at org.apache.cassandra.thrift.TFramedTransportFactory.openTransport(TFr amedTransportFactory.java:41) at org.apache.cassandra.cli.CliMain.connect(CliMain.java:66) at org.apache.cassandra.cli.CliMain.main(CliMain.java:239) Caused by: java.net.ConnectException: Connection refused: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) at java.net.AbstractPlainSocketImpl.connect(Unknown Source) at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.SocksSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at org.apache.thrift.transport.TSocket.open(TSocket.java:182) ... 4 more Exception connecting to 127.0.0.1/9160. Reason: Connection refused: connect. Welcome to Cassandra CLI version 2.1.11 The CLI is deprecated and will be removed in Cassandra 2.2. Consider migrating to cqlsh. CQL is fully backwards compatible with Thrift data; see http://www.datastax.com/ dev/blog/thrift-to-cql3 Type 'help;' or '?' for help. Type 'quit;' or 'exit;' to quit.
可以看到Connection refused,如果遇到这个问题首先要检查配置情况(具体看我之前写的总结二),然后看cassandra是否启动,在window7下显示启动成功后可能还在后台初始化数据所以等待一小会后再运行cassandra-cli.bat启动客户端,在window8下启动cassandra后不要关掉窗口,window7下启动成功后关闭窗口cassandra仍然是在后台运行的,但在window8下关闭窗口就相当于关闭了cassandra,所以在window8下需要重新开个dos窗口来运行cassandra-cli.bat启动客户端。
如果配置都没问题,仍然拒绝连接,那就是cassandra启动失败,这时候我们可以到与$CASSANDRA_HOME同级目录的cassandra文件夹中查看登录日志来查找失败的原因。
也可以连接自己定义的hostname和port,如:cassandra-cli -h localhost -p 9160或者cassandra-cli -hostname localhost -port 9160
在上面我们可以看到这么一句话
The CLI is deprecated and will be removed in Cassandra 2.2. Consider migrating to cqlsh.
所以更新版本的想要运行cassandra客户端就到cqlsh中吧。
二、cassandra cli简介
首先我么来看下cli中有哪些命令
[default@unknown] ? Getting around: ? Display this help. help; Display this help. help <command>; Display command-specific help. exit; Exit this utility. quit; Exit this utility. Commands: assume Apply client side validation. connect Connect to a Cassandra node. consistencylevel Sets consisteny level for the client to use. count Count columns or super columns. create column family Add a column family to an existing keyspace. create keyspace Add a keyspace to the cluster. del Delete a column, super column or row. decr Decrements a counter column. describe cluster Describe the cluster configuration. describe Describe a keyspace and its column families or column fa mily in current keyspace. drop column family Remove a column family and its data. drop keyspace Remove a keyspace and its data. drop index Remove an existing index from specific column. get Get rows and columns. incr Increments a counter column. list List rows in a column family. set Set columns. show api version Show the server API version. show cluster name Show the cluster name. show keyspaces Show all keyspaces and their column families. show schema Show a cli script to create keyspaces and column familie s. truncate Drop the data in a column family. update column family Update the settings for a column family. update keyspace Update the settings for a keyspace. use Switch to a keyspace.
我们挑主要的几个讲一下:
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).
[default@keyspace1] list standardFamily limit 1; Using default cell limit of 100 ------------------- RowKey: 3 => (name=3, value=3, timestamp=1448369924110000) 1 Row Returned. Elapsed time: 24 msec(s).
可以看出key是按照cassandra自己的机制排序的,但column或者superColumn是可以自己定义类型排序的。
PS:剩下的就不一一细述了