cassandra-cli的基本操作——cassandra总结(三)

一、启动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启动客户端。

  也可以连接自己定义的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).                                                               
[[email protected]] 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:剩下的就不一一细述了

时间: 2024-10-27 07:30:27

cassandra-cli的基本操作——cassandra总结(三)的相关文章

cassandra client in Java——cassandra总结(五)

cassandra client是基于Apache Thrift这个RPC框架来进行客户端和服务器的通信. 首先到$CASSANDRA_HOME\lib目录下导入apache-cassandra-thrift-2.1.11.jar,libthrift-0.9.2.jar这两个包,然后再导入单元测试JUnit包. import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; import java.util.Arr

Cassandra基本介绍(2) - Cassandra概述

上一节我们介绍RDBMS遇到的问题,这一节我们将介绍Cassandra以及Cassandra是否可以解决此问题. 通过此章节,我们将学习到: 什么是Cassandra Cassandra数据的Hash分布 Cassandra在CAP中的权衡 Cassandra复制 Cassandra可调一致性 Cassandra多数据中心 什么是Cassandra Apache Cassandra是一个开源的.分布式.无中心.弹性可扩展.高可用.容错.一致性可调.面向列的数据库,它基于Amazon Dynamo

Cassandra CLI 命令详解

Cassandra Shell命令详解 Login cqlsh 172.20.101.252 -u cassandra -p cassandra Connected to cn-engage-dev at 172.20.101.252:9042. [cqlsh 5.0.1 | Cassandra 3.11.4 | CQL spec 3.4.4 | Native protocol v4] 创建一个Keyspace CREATE KEYSPACE <identifier> WITH <pro

二叉树基本操作--创建,三种遍历,叶子节点

虽然二叉树的操作很常见,但是认真写写熟悉很重要,特别是typedef, CreateBiTree(BiTNode** T)指针的操作等等,还有就是创建方法,去实际输入值就知道其中的妙处,为-1时为空节点. #include <iostream> using namespace std; //节点的定义 typedef struct BTNode { int data; BTNode* rChild; BTNode* lChild; }BiTNode, *BiTree; //二叉树的创建,先序创

低调、奢华、有内涵的敏捷式大数据方案:Flume+Cassandra+Presto+SpagoBI

基于FacebookPresto+Cassandra的敏捷式大数据 目录 1      概述...3 1.1       Cassandra.3 1.1.1        特点...3 1.1.2        系统架构...4 1.2       Presto.4 1.2.1        特点...4 1.2.2        系统架构...5 2      环境准备...5 2.1       主机...5 2.2       用户...5 2.3       程序包...6 2.4   

Cassandra非关系型数据库

由于在工作中用到了Cassandra非关系型数据库,因此总结一下常用操作.Cassandra使用Java语言编写的,因此在安装之前,首先需要安装JDK.自己使用的版本为apache-cassandra-2.1.11-bin.tar.gz,在Ubuntu 12.04上进行安装.由于目前仅仅在单机上面实验,因此集群部分以后整理. 1. Cassandra安装和配置 (1)~/.bashrc环境变量配置 export CASSANDRA_HOME=/usr/local/cassandra/apache

Cassandra简介

在前面的一篇文章<图形数据库Neo4J简介>中,我们介绍了一种非常流行的图形数据库Neo4J的使用方法.而在本文中,我们将对另外一种类型的NoSQL数据库——Cassandra进行简单地介绍. 接触Cassandra的原因与接触Neo4J的原因相同:我们的产品需要能够记录一系列关系型数据库所无法快速处理的大量数据.Cassandra,以及后面将要介绍的MongoDB,都是我们在技术选型过程中的一个备选方案.虽然说最后我们并没有选择Cassandra,但是在整个技术选型过程中所接触到的一系列内部

Cassandra研究报告

1基本安装 1.1在基于RHEL的系统中安装Cassandra 1.1.1必要条件 ?  YUM包管理器 ?  Root或sudo权限 ?  JRE6或者JRE7 ?  JNA(Java native Access)(生产环境需要) 1.1.2步骤 ?  安装配置JRE(略) ?  添加软件包仓库到YUM的软件库 将以下内容添加进/etc/yum.repos.d/datastax.repo文件即可: [datastax] name = DataStax Repo for ApacheCassan

kairosdb+cassandra集群化安装

kairosdb (1)到/conf目录下,找到kairosdb.properties,修改datastore为cassandra (2)设置cassandra的连接方式 (3) 设置用户名密码 4. 启动:到/bin目录下,直接跑./kairosdb.sh start,最后会看到 KairosDB service started   这样一句话,就OK了 172.16.101.25:8080 kairosdb客户端 cassandra 修改cassandra配置文件 conf/cassandr