Cassandra Common commands

Source: Internet
Author: User
Tags cassandra datastax

1. 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:

[Default @ unknown] connect localhost/9160;

Or connect to a node in a multi-node cluster through the IP address and port of the node:

[Default @ unknown] connect-host 110.123.4.5-port 9160;

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.

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.

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.

 

View keyspace information and column family Definitions

[Default @ unknown] descibe demo;

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 family blog_entry with comparator = timeuidtype and key_validation_class = utf8type and default_validation_class = utf8type;

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 family ypage_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;

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)

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 comprator asascii;

[Default @ demo] sssume users validatior asascii;

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') with TTL = 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.

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;

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'];

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;

 

View information about nodes on the ring. Switch the current directory to the bin/directory under the terminal.

Nodetool-H 192.168.32 Ring

View the node information and switch the terminal to the bin

Nodeltool-H 192.168.32 info

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.