Cassandra Command _hadoop

Source: Internet
Author: User
Tags cassandra datastax

First use CASSANDRA-CLI to enter the command line: $ bin/cassandra-cli-host 192.168.0.101
1. Create Keyspace

CREATE keyspace usertable with placement_strategy = ' org.apache.cassandra.locator.SimpleStrategy ' and strategy_options = {Replication_factor:2};

2. Create a column cluster

Create column family data with Comparator=utf8type and Default_validation_class=utf8type and key_validation_class= Utf8type;

3. Create a cluster of calculators

The Counter column family consists primarily of counter columns, which are 64-bit signed values that can be used by clients and are provided to clients using incrementally or in descending mode. The Counter column tracks the nearest value or calculates its value, and constantly updates itself, you must create a column family dedicated to the client to do the counting.

We also create a column family that uses the counter, and make the Countercolumntype default_validation_class of this column family point to the counter by default. Like what:

[Default@demo] CREATE COLUMN familypage_view_counts

With Default_validation_class=countercolumntype

and Key_validation_class=utf8type and Comparator=utf8type;

When inserting a row of data and the counter column is updated simultaneously (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;

4. Insert a row or column of data

Example illustrates the use of the SET directive to insert column values for the user column family. In this example we use key for Bobbyjo, and many columns are inserted on the Bobbyjo, they belong to the family of users, and remember that Bobbyjo is just a line of identification. Notice that we can only insert one column at a time.

[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 the next example we use Yomama as the key for the row, inserting several columns and values into the Users column family.

[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 the following example, we build a column with key Yomama for the Blog_entry group:

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

5. Reading rows and columns of data

In the Cassandra client, we use the get instruction to read data from the specified column family.

Use the list instruction to read the associated column values in the specified column family (default is to return 100 rows of data).

The following examples are data that returns 100 rows from the Users column family:

[Default@demo] LIST users;

Cassandra internal default uses hexadecimal bytes to store data. If you do not specify a checksum type for the line key, then the column comparer and validator take the default value defined by the column family, and the Cassandra client inserts the data for the row in the default way, whether it is a row key or a column name. Or column values are the same in hexadecimal (the same data is returned in 16-way).

To enter and return our readable data format, we must specify the encoding format that Cassandra can provide: ASCII bytes integer (a generic variable-length integer type) Lexicaluuid Long UTF8

For example, the following example uses the UTF-8 format to return data:

[Default@demo] Get Users[utf8 (' Bobby ')][utf8 (' Full_name ')];

You can also use the data format for the entire session of the client through the assume instruction, such as using ascii-encoded as the data return value in the following example.

[Default@demo] Assume users KEYS as ASCII;

[Default@demo] Assume users COMPARATOR asascii;

[Default@demo] Assume users VALIDATOR asascii;

6. Set an expired column

When you set up a column in Cassandra, you can specify its expiration time, or his life cycle attribute time-to-live.

For example: Our users track the expiration time of the preferential securities is 10 days, we can define a discount volume of the column expiration date specified as 10 days, such as:

[Default@demo] SET users[' Bobbyjo ']

[UTF8 (' coupon_code ')] = UTF8 (' SAVE20 ') withttl=864000;

So after 10 days or 864,000 seconds, the column will be unreadable, but its value is not removed from disk know Cassandra complete compression and contraction.

7. Index of columns

The Cassandra client can create a second index (the index of the column value), and when you create the column family you can specify its second index or modify its index later by using Update column family.

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 a second index is added to the birth_year, its value can be queried directly as a condition.

[Default@demo] Get users WHERE birth_date = 1969;

8. Delete Rows and columns

Cassandra provides del instruction to delete rows or columns (or sub columns)

For example, the ' Yomama ' row in the uses column family deletes the Coupon_code column:

[Default@demo] DEL users [' Yomama '] [' coupon_code '];

Verify the deletion through the following directives

[Default@demo] Get users [' Yomama '];

or delete the entire line directly.

[Default@demo] DEL users [' Yomama '];

9. Delete columns and keyspace

Cassandra provides a way to delete tables and databases like a relational database deletes the column family and Keyspaces, the following example shows how to delete the Users column family and the keyspace named demo through the drop instruction.

[Default@demo] DROP COLUMN FAMILY users;

[Default@demo] DROP Keyspace Demo;

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.