Some documents on the Internet appear a little old after a period of exposure to Cassandra, most of which are 0.6 and 0.7, But cassandra has developed rapidly and has developed to version 1.0.x, therefore, I decided to translate some major documents so that some beginners can obtain first-hand documents. Translation documents are also a learning process. The translation work is also the first time, and there must be many shortcomings, I hope that the translation of these documents will help some people who want to learn Cassandra.
I will demonstrate in the following documents: installation and configuration, cluster, data model, data modeling, case analysis, security and permissions, etc, the first part describes some common commands on the client.
Apache Cassandra 1.0 documentation
Getting started using the Cassandra CLI
Start to use the Sandra client (Common commands on the Sandra client)
Original
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.
Translation
Cassandracli can be used to define data and perform data operations on the cluster. You can find the client tool under the bin directory ($ cassandra_home/bin/) after installing the cluster, the name is Cassandra-cli.
Original
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 inCassandra. yaml
File (which isTestcluster'By default). For example, if you have a single-node cluster on localhost:
$ cassandra-cli -host localhost -port 9160
Translation
Start the client tool and connect to a specific Cassandra instance. The-host and-port parameters of the instance must be provided during connection. If the provided parameters are correct, the client tool will connect you to Cassandra. for example, if you run a single-node cluster on localhost, the client uses the following command to connect to localhost:
$ Cassandra-cli-host localhost-port 9160
Original
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
Translation
Or connect to a node in a multi-node cluster through the IP address and port of the node:
$ Cassandra-cli-host 110.123.4.5-port9160
Original
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;
Translation
You can view the help and instructions of each command in the command line.
[Default @ unknown] help;
To view the help information of a specific command, enter help in the command line. For example:
[Default @ unknown] Help set;
In this way, you can view the help information of the SET command.
Original
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 |
.
Translation
Remarks |
Each Command must end with a ";" to be sent to the server for execution. If you do not enter ";" and press enter directly, the command line interface will display "..." Prompt, meaning the client waits for more input content. |
Original
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.
Translation
Create a keyspace object
You can use the client tool to create a keydspace object. In this example, we will create a keyspace named demo. It carries a replication factor 1 and uses the simplestrategy copy placement policy.
Note the string value of placement_strategy in single quotes:
[Default @ unknown] Create keyspace demo
With placement_strategy = 'org. Apache. Cassandra. locator. simplestrategy'
And strategy_options = [{replication_factor: 1}];
At this time, you can see the actual situation after the command line is created.
Original
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 consideredStaticColumn 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, invalid 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, createDynamicColumn 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;
Translation
Create a columnfamily
First, you need to use the use command to connect to the keyspace of the columnfamily you want to create.
[Default @ unknown] Use demo;
In this example, we create a columnfamily named users in the keyspace named demo. In this column family, we define several columns: full_name, email, state, gender, and birth_year. This is considered a static column family-a pre-defined column family. Pay attention to the settings of key_validation_class and validation_class, these settings enable the encoding of these columns, keys, and column values by default. In this case, we also define the sorting method.
[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 family named blog_entry. Note that the column name and other parameters are not pre-defined here, and these parameters are provided in subsequent client operations.
[Default @ demo] Create column familyblog_entry
With comparator = timeuuidtype
And key_validation_class = utf8type
And default_validation_class = utf8type;
Original
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;
Translation
Create a column family for a calculator
The counter column family mainly contains counter columns. This column is a 64-Bit Signed value, which can be used by the client and provided to the client in ascending or descending mode. The counter column tracks recent values or calculates and uses its values, and constantly updates itself. You must create a column family dedicated to counting the client.
At the same time, we create a column family that uses this counter, and set the default_validation_class of the countercolumntype of this column family to point to this counter by default. For example:
[Default @ demo] Create column familypage_view_counts
With default_validation_class = countercolumntype
And key_validation_class = utf8type and comparator = utf8type;
Insert a row of data and update the counter column at the same time (with the counter's initial value set to 0)
[Default @ demo] incr page_view_counts ['www .datastax.com '] [home] by 0;
Calculator increment:
[Default @ demo] incr page_view_counts ['www .datastax.com '] [home] by 1;
Original
Inserting rows and columns
The following examples into strate using theset command to insert columns for a participating 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'] = 'lootjones ';
[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'] = '20140901 ';
In this example, the row key is yomama andwe are just setting some of the columns for this user.
[Default @ demo] Set users ['yamama'] ['full _ name'] = 'cathysmith ';
[Default @ demo] Set users ['yamama'] ['state'] = 'CA ';
[Default @ demo] Set users ['yamama'] ['gender'] = 'F ';
[Default @ demo] Set users ['yamama'] ['birth _ year'] = '123 ';
In this example, we are creating an entryin the blog_entry column family for row key yomama:
[Default @ demo] Set blog_entry ['ymama '] [timeuuid ()] =' I love my new shoes! ';
Translation
Insert a row or column of data
The following example shows how to use the set command to insert column values for the user columnfamily. In this example, we use the key bobbyjo and insert many columns into the bobbyjo. They belong to the users column family. Remember that bobbyjo is only a row of identifiers. Note that only one column can be inserted at a time.
[Default @ demo] Set users ['bobbyjo '] ['full _ name'] = 'lootjones ';
[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'] = '20140901 ';
In the following example, we use yomama as the row key and insert several columns and values in the users column family.
[Default @ demo] Set users ['yamama'] ['full _ name'] = 'cathysmith ';
[Default @ demo] Set users ['yamama'] ['state'] = 'CA ';
[Default @ demo] Set users ['yamama'] ['gender'] = 'F ';
[Default @ demo] Set users ['yamama'] ['birth _ year'] = '123 ';
In the following example, we create a column whose key is yomama for the blog_entry column family:
[Default @ demo] Set blog_entry ['ymama '] [timeuuid ()] =' I love my new shoes! ';
Remarks |
The Cassandra client uses one as the read/write Consistency Verification level. The client does not support other Consistency Verification levels. (Translator's note: Consistency Verification will be mentioned later in the document) |
Original
Reading rows and columns
Use the GET command within Cassandra CLI toretrieve a special 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 validate CT 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 participant row keyand column in utf8 format:
[Default @ demo] Get users [utf8 ('bob')] [utf8 ('full _ name')];
You can also use the assume command tospecify the encoding in which column family data shoshould 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;
Translation
Read data from rows and columns
In the Cassandra client, we use the GET command to read data from the specified columnfamily.
Use the LIST command to read related column values from the specified columnfamily (100 rows of data are returned by default ).
The following example shows how to return 100 rows of data from the users columnfamily:
[Default @ demo] List users;
Cassandra uses hexadecimal bytes to store data by default. If you do not specify the row key validation type, the column comparator and validator use the default value defined by the column family. The Cassandra client inserts data for the row by default, both the row key, column name, and column value use the same hexadecimal format (the same data is also returned in hexadecimal format ).
To input and return readable data formats, we must specify the encoding format. Cassandra provides the following encoding formats:
- ASCII
- Bytes
- INTEGER (a generic variable-length Integer type)
- Lexicaluuid
- Long
- Utf8
For example, the following example returns data in UTF-8 format:
[Default @ demo] Get users [utf8 ('bob')] [utf8 ('full _ name')];
You can also use the assume command to use the data format during the entire session of the client. For example, the following example uses ascii-encoded as the data return value.
[Default @ demo] assume users keys as ASCII;
[Default @ demo] assume users comparator asascii;
[Default @ demo] assume users validator asascii;
Original
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.
Translation
Set an expired Column
When you set a column in Cassandra, you can specify its expiration time or its lifecycle attribute time-to-live.
For example, if the expiration time of a user tracking discount securities is 10 days, we can define that the expiration time of a coupon column is 10 days. For example:
[Default @ demo] Set users ['bobbyjo ']
[Utf8 ('coupon _ Code')] = utf8 ('save20') withttl = 864000;
After ten or 864000 seconds, the column will be unreadable. However, its value is not deleted from the disk and Cassandra is compressed and shrunk.
Original
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;
Translation
Column Index
The Cassandra client can create a second index (index of the column value). When creating this column family, you can specify its second index or use Update column family to modify its index later.
For example, add a second index to the birth_year column in 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 the second index is added in birth_year, its value can be directly queried as a condition.
[Default @ demo] Get users where birth_date = 1969;
Original
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 ['yamama'] ['coupon _ Code'];
[Default @ demo] Get users ['yamama'];
Or to delete an entire row:
[Default @ demo] del users ['yamama'];
Translation
Delete rows and columns
Cassandra provides the del command to delete rows or columns (or subcolumns)
For example, in the uses columnfamily, delete the coupon_code column from the 'ymama 'row:
[Default @ demo] del users ['yamama'] ['coupon _ Code'];
Run the following command to verify deletion:
[Default @ demo] Get users ['yamama'];
Or directly Delete the entire row.
[Default @ demo] del users ['yamama'];
Original
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;
Translation
Delete columnfamily and keyspaces
Cassandra allows you to delete columns and keyspaces in a way similar to a relational database. The following example shows how to delete users columns and keyspaces named demo using the drop command.
[Default @ demo] Drop column family users;
[Default @ demo] Drop keyspace demo;