现在的位置: 首页 > 综合 > 正文

第一部分 Cassandra 1.0.X常用命令指南

2013年06月29日 ⁄ 综合 ⁄ 共 12751字 ⁄ 字号 评论关闭
文章目录

      接触了一段时间的Cassandra之后发现网上一些文档显得有点旧了,绝大部分是0.6,0.7版本的资料,可是Cassandra发展很快,已经发展到了1.0.x版本了,于是决定做一些主要文档的翻译,以便一些初学者可以得到一手的资料,翻译文档也是学习的过程,翻译工作也是第一次干,肯定还是有很多的不足,希望这些资料的翻译可以帮助到一些想学习Cassandra的人。

我将在接下来的文档中体现:安装和配置、群集、数据模型、数据建模、案例分析、安全和权限等内容,而第一部分的内容是客户端一些常用指令的说明。

Apache Cassandra 1.0Documentation

Getting Started Using the Cassandra CLI

开始使用卡桑德拉客户端(卡桑德拉客户端常用命令)

 

原文

The Cassandra CLI client utility can beused to do basic data definition (DDL) and data manipulation (DML) within aCassandra cluster. It is located in /usr/bin/cassandra-cliin packaged installations or$CASSANDRA_HOME/bin/cassandra-cli
in binary installations.

 

译文

卡桑德拉客户端工具(CassandraCLI)可用于对卡桑德拉群集进行基本的数据定义和数据操作,在安装卡桑德拉之后的bin目录($CASSANDRA_HOME/bin/)下可以找到客户端工具,名称是:Cassandra-cli。

 

原文

To start the CLI and connect to aparticular Cassandra instance, launch the script together with -hostand -port options. It will connect to the cluster namespecified in thecassandra.yaml
file (which is TestCluster` by default). For example, if you have a single-node cluster on localhost:

$ cassandra-cli -host localhost -port 9160

 

译文

启动客户端工具并连接到某一个具体的Cassandra实例,连接时需要提供实例的–host 和-port参数,如果提供的参数正确那么客户端工具将会帮你连接到在cassandra.yaml定义的群集服务器实例上,比如,你在localhost运行着单节点的群集,那么客户端采用如下命令连接到localhost:

$cassandra-cli –host localhost –port 9160

 

原文

Or to connect to a node in a multi-nodecluster, give the IP address of the node:

$ cassandra-cli -host110.123.4.5 -port 9160

 

译文

或是通过节点的ip地址和端口连接到一个多节点群集中的一个节点:

$ cassandra-cli -host 110.123.4.5 -port9160

 

原文

To see help on the various commandsavailable:

[default@unknown] help;

For detailed help on a specific command,use help <command>;. For example:

[default@unknown] helpSET;

 

译文

查看各个指令的帮助和说明可以在命令行打入

[default@unknown] help;

如果想查看具体某个指令的帮助信息可以在命令行打入 help <命令的名称>,例如:

[default@unknown] help SET;

这样既可以查看SET指令的help信息了。

 

原文

Note

A command is not sent to the server unless it is terminated by a semicolon (;). Hitting the return key without a semicolon at the end of the line echos an ellipsis ( . . . ), which indicates that the CLI expects more input

.

译文

备注

每一个指令的结尾都必须带上一个“;”才会被发送到服务器上执行,如果没有输入“;”直接回车,那么命令行界面会出现“……”提示,意思客户端等待更多的输入内容。

 

原文

Creating a Keyspace

You can use the Cassandra CLI commandsdescribed in this section to create a keyspace. In this example, we create akeyspace called demo, with a replication factor of 1 and using the SimpleStrategyreplica placement strategy.

Note the single quotes around the stringvalue of placement_strategy:

[default@unknown] CREATE KEYSPACE demo

with placement_strategy = 'org.apache.cassandra.locator.SimpleStrategy'

and strategy_options = [{replication_factor:1}];

You can verify the creation of a keyspacewith the SHOW KEYSPACES command. The new keyspace is listed along with the systemkeyspace and any other existing keyspaces.

 

译文

创建一个 Keyspace对象

你可以使用客户端工具创建Keydspace对象,在本例子中,我们将创建一个命名为demo的keyspace。 它带有一个复制因子1和使用simplestrategy副本放置策略。

注意单引号内placement_strategy的字符串值:

[default@unknown] CREATE KEYSPACE demo

with placement_strategy = 'org.apache.cassandra.locator.SimpleStrategy'

and strategy_options = [{replication_factor:1}];

这时候你可以看到在命令行创建之后的现实情况。

 

 

原文

Creating a Column Family

First, connect to the keyspace where youwant to define the column family with the USE command.

[default@unknown] USE demo;

In this example, we create a users columnfamily in the demo keyspace. In this column family we are defining a fewcolumns; full_name, email, state, gender, and birth_year. This is considered astaticcolumn family - we are defining the column names
up front and most rows areexpected to have more-or-less the same columns.

Notice the settings of comparator, key_validation_classand validation_class. These are setting the default encoding used for columnnames, row key values and column values. In the case of column names, thecomparator also determines the sort order.

[default@unknown] USE demo;

 

[default@demo] CREATE COLUMN FAMILY users

WITH comparator = UTF8Type

AND key_validation_class=UTF8Type

AND column_metadata = [

{column_name: full_name, validation_class:UTF8Type}

{column_name: email, validation_class:UTF8Type}

{column_name: state, validation_class:UTF8Type}

{column_name: gender, validation_class:UTF8Type}

{column_name: birth_year, validation_class:LongType}

];

Next, create a dynamic column familycalled blog_entry. Notice that here we do not specify column definitions as thecolumn names are expected to be supplied later by the client application.

[default@demo] CREATE COLUMN FAMILYblog_entry

WITH comparator = TimeUUIDType

AND key_validation_class=UTF8Type

AND default_validation_class = UTF8Type;

 

译文

创建一个列族

首先你要使用use指令连接到你要创建列族的keyspace。

[default@unknown] USE demo;

在本例子中我们在名为demo的keyspace创建一个名为users的列族,而且在这个列族里面我们定义了几个列:full_name, email,state, gender, 和 birth_year。这被认为是一个静态的列族-一种预先定义列的列族,注意key_validation_class和validation_class的设置,这些设置是让这些列和key以及列值默认使用的编码。在这个案例中我们也定义了排序的方法。

[default@demo] CREATE COLUMN FAMILY users

WITH comparator = UTF8Type

AND key_validation_class=UTF8Type

AND column_metadata = [

{column_name: full_name, validation_class:UTF8Type}

{column_name: email, validation_class:UTF8Type}

{column_name: state, validation_class:UTF8Type}

{column_name: gender, validation_class:UTF8Type}

{column_name: birth_year, validation_class:LongType}

];

接下来我们创建一个名为blog_entry的动态列族,注意到这里我们并没有预先定义列名和其他参数,而在后面的客户端操作中提供这些参数。

[default@demo] CREATE COLUMN FAMILYblog_entry

WITH comparator = TimeUUIDType

AND key_validation_class=UTF8Type

AND default_validation_class = UTF8Type;

 

原文

Creating a Counter Column Family

A counter column family contains countercolumns. A counter column is a specific kind of column whose user-visible valueis a 64-bit signed integer that can be incremented (or decremented) by a clientapplication. The counter column tracks the most recent value
(or count) of allupdates made to it. A counter column cannot be mixed in with regular columns ofa column family, you must create a column family specifically to hold counters.

To create a column family that holdscounter columns, set the default_validation_class of the column family to CounterColumnType.For example:

[default@demo] CREATE COLUMN FAMILYpage_view_counts

WITH default_validation_class=CounterColumnType

AND key_validation_class=UTF8Type AND comparator=UTF8Type;

To insert a row and counter column into thecolumn family (with the initial counter value set to 0):

[default@demo] INCR page_view_counts['www.datastax.com'][home]BY 0;

To increment the counter:

[default@demo] INCR page_view_counts['www.datastax.com'][home]BY 1;

 

译文

创建一个计算器的列族

计数器列族主要包含计数器列,这个列是一个64位的有符号数的值,可供客户端使用,采用递增或是递减的方式提供给客户端使用。计数器列跟踪最近的值或是计算使用它的值,并不断更新自己,你必须创建一个列族专门给客户端做计数用。

我们同时创建一个使用该计数器的列族,并使这个列族的CounterColumnType的default_validation_class默认指向该计数器。比如:

[default@demo] CREATE COLUMN FAMILYpage_view_counts

WITH default_validation_class=CounterColumnType

AND key_validation_class=UTF8Type AND comparator=UTF8Type;

插入一行数据时和计数器列同时更新(与计数器的初始值设置为0)

[default@demo] INCR page_view_counts['www.datastax.com'][home]BY 0;

计算器增量:

[default@demo] INCR page_view_counts['www.datastax.com'][home]BY 1;

 

原文

Inserting Rows and Columns

The following examples illustrate using theSET command to insert columns for a particular row key into the users columnfamily. In this example, the row key is bobbyjo and we are setting each of thecolumns for this user. Notice that you can only set one column
at a time in a SETcommand.

[default@demo] SET users['bobbyjo']['full_name']='RobertJones';

 

[default@demo] SET users['bobbyjo']['email']='bobjones@gmail.com';

 

[default@demo] SET users['bobbyjo']['state']='TX';

 

[default@demo] SET users['bobbyjo']['gender']='M';

 

[default@demo] SET users['bobbyjo']['birth_year']='1975';

In this example, the row key is yomama andwe are just setting some of the columns for this user.

[default@demo] SET users['yomama']['full_name']='CathySmith';

 

[default@demo] SET users['yomama']['state']='CA';

 

[default@demo] SET users['yomama']['gender']='F';

 

[default@demo] SET users['yomama']['birth_year']='1969';

In this example, we are creating an entryin the blog_entry column family for row key yomama:

[default@demo] SET blog_entry['yomama'][timeuuid()]= 'I love my new shoes!';

 

译文

插入一行数据或是一列数据

下面的例子说明了使用set指令为user列族插入列值。在这个例子中我们采用key为bobbyjo,在bobbyjo上插入了很多列,它们属于users这个列族,记住bobbyjo只是一行的标识。注意我们每次只能插入一列。

[default@demo] SET users['bobbyjo']['full_name']='RobertJones';

 

[default@demo] SET users['bobbyjo']['email']='bobjones@gmail.com';

 

[default@demo] SET users['bobbyjo']['state']='TX';

 

[default@demo] SET users['bobbyjo']['gender']='M';

 

[default@demo] SET users['bobbyjo']['birth_year']='1975';

 

在接下来的例子中我们采用yomama作为行的key,在users这个列族中插入若干列和值。

[default@demo] SET users['yomama']['full_name']='CathySmith';

 

[default@demo] SET users['yomama']['state']='CA';

 

[default@demo] SET users['yomama']['gender']='F';

 

[default@demo] SET users['yomama']['birth_year']='1969';

 

在下面的例子中,我们为创blog_entry列族建一个key为yomama的列:

[default@demo] SET blog_entry['yomama'][timeuuid()]= 'I love my new shoes!';

 

备注

Cassandra客户端采用ONE作为读写的一致性验证级别,客户端不支持其他的一致性验证级别。(译者注:一致性验证我们再往后的文档中会提及)

 

原文

Reading Rows and Columns

Use the GET command within Cassandra CLI toretrieve a particular row from a column family. Use the LIST command to returna batch of rows and their associated columns (default limit of rows returned is100).

For example, to return the first 100 rows(and all associated columns) from the users column family:

[default@demo] LIST users;

Cassandra stores all data internally as hexbyte arrays by default. If you do not specify a default row key validationclass, column comparator and column validation class when you define the columnfamily, Cassandra CLI will expect input data for row keys,
column names, andcolumn values to be in hex format (and data will be returned in hex format).

To pass and return data in human-readableformat, you can pass a value through an encoding function. Available encodingsare:

  • ascii
  • bytes
  • integer (a generic variable-length integer type)
  • lexicalUUID
  • long
  • utf8

For example to return a particular row keyand column in UTF8 format:

[default@demo] GET users[utf8('bobby')][utf8('full_name')];

You can also use the ASSUME command tospecify the encoding in which column family data should be returned for theentire client session. For example, to return row keys, column names, andcolumn values in ASCII-encoded format:

[default@demo] ASSUME users KEYS AS ascii;

[default@demo] ASSUME users COMPARATOR ASascii;

[default@demo] ASSUME users VALIDATOR ASascii;

 

 

译文

读取行和列的数据

在Cassandra客户端中我们使用GET指令从指定的列族中读取数据。

采用List指令冲指定的列族中读取相关的列值出来(默认是返回100行的数据)。

下面的例子就是从users列族中返回100行的数据:

[default@demo] LIST users;

Cassandra内部默认采用十六进制的字节存放数据。假如你没有指定行key的校验类型,那么列的比较器和校验器都是采用列族定义的默认值,Cassandra客户端将按照默认的的方式为行插入数据,不管是行key还是列名,还是列值都是一样的采用十六进制的方式(同样数据也是以十六进制的方式返回)。

想输入和返回我们可读的数据格式,我们就必须指定编码格式,Cassandra可以提供的编码格式有:

  • ascii
  • bytes
  • integer (a generic variable-length integer type)
  • lexicalUUID
  • long
  • utf8

比如以下例子采用UTF-8的格式返回数据:

[default@demo] GET users[utf8('bobby')][utf8('full_name')];

你也可以通过ASSUME指令在客户端的整个会话过程中采用那一种数据格式,比如下面的例子中采用ASCII-encoded作为数据返回值。

[default@demo] ASSUME users KEYS AS ascii;

[default@demo] ASSUME users COMPARATOR ASascii;

[default@demo] ASSUME users VALIDATOR ASascii;

 

原文

Setting an Expiring Column

When you set a column in Cassandra, you canoptionally set an expiration time, ortime-to-live (TTL) attribute forit.

For example, suppose we are tracking couponcodes for our users that expire after 10 days. We can define a coupon_codecolumn and set an expiration date on that column. For example:

[default@demo] SET users['bobbyjo']

[utf8('coupon_code')] = utf8('SAVE20') WITHttl=864000;

After ten days, or 864,000 seconds haveelapsed since the setting of this column, its value will be marked as deletedand no longer be returned by read operations. Note, however, that the value isnot actually deleted from disk until normal Cassandra compaction
processes arecompleted.

 

译文

设置一个过期的列

当你在Cassandra设置一个列时,你可以指定它的过期时间,或是他的生命周期属性time-to-live。

比如:我们的用户跟踪优惠证券的过期时限是10天,我们就能定义一个优惠卷的列过期时间指定为10天,如:

[default@demo] SET users['bobbyjo']

[utf8('coupon_code')] = utf8('SAVE20') WITHttl=864000;

那么过了十天或是864000秒,该列将不可读,然而它的值并没有从磁盘中删除知道Cassandra完成压缩和收缩。

 

原文

Indexing a Column

The CLI can be used to create secondaryindexes (indexes on column values). You can add a secondary index when youcreate a column family or add it later using the UPDATE COLUMN FAMILY command.

For example, to add a secondary index tothe birth_year column of the users column family:

[default@demo] UPDATE COLUMN FAMILY users

WITH comparator = UTF8Type

AND column_metadata = [{column_name:birth_year, validation_class: LongType, index_type: KEYS}];

Because of the secondary index created forthe column birth_year, its values can be queried directly for users born in agiven year as follows:

[default@demo] GET users WHERE birth_date =1969;

 

译文

列的索引

Cassandra客户端可以创建第二索引(列值的索引),当创建该列族是你可以指定它的第二索引或是在后期使用UPDATE COLUMN FAMILY修改它的索引。

比如在users列族中的birth_year列添加第二索引:

[default@demo] UPDATE COLUMN FAMILY users

WITH comparator = UTF8Type

AND column_metadata = [{column_name:birth_year, validation_class: LongType, index_type: KEYS}];

因为在birth_year添加了第二索引,所以它的值可以作为条件直接查询。

[default@demo] GET users WHERE birth_date =1969;

 

原文

Deleting Rows and Columns

The Cassandra CLI provides the DEL commandto delete a row or column (or subcolumn).

For example, to delete the coupon_codecolumn for the yomama row key in the users column family:

[default@demo] DEL users ['yomama']['coupon_code'];

 

[default@demo] GET users ['yomama'];

Or to delete an entire row:

[default@demo] DEL users ['yomama'];

 

 

译文

删除行和列

Cassandra提供DEL指令删除行或是列(或是子列)

比如在uses列族中的'yomama'行删除coupon_code列:

[default@demo] DEL users ['yomama']['coupon_code'];

 

通过以下指令验证删除的情况

[default@demo] GET users ['yomama'];

 

或是直接删除整行

[default@demo] DEL users ['yomama'];

 

原文

Dropping Column Families and Keyspaces

With Cassandra CLI commands you can dropcolumn families and keyspaces in much the same way that tables and databasesare dropped in a relational database. This example shows the commands to dropour example users column family and then drop the demo keyspace
altogether:

[default@demo] DROP COLUMN FAMILY users;

 

[default@demo] DROP KEYSPACE demo;

 

译文

删除列族和keyspaces

Cassandra提供了类似关系数据库一样删除表和数据库的方式删除列族和keyspaces,以下的例子显示如何通过Drop指令删除users列族和名为demo的keyspace。

[default@demo] DROP COLUMN FAMILY users;

 

[default@demo] DROP KEYSPACE demo;

抱歉!评论已关闭.