After installing Cassandra, we begin to experience the query of this database, the traditional relational database uses SQL to query, and Cassandra uses the CQL.
CQL Grammar is still a lot of, here do not elaborate, also not this necessary, specific documents countless, here just the most commonly used query function listed.
Start by opening the command line (or PowerShell) to the Bin folder in the Cassandra installation directory, executing cqlsh.bat (Cqlsh is ok under PowerShell). Here I am in PowerShell.
Enter CQL client, PowerShell directly with Cqlsh login, cmd and command line need to use Cqlsh.bat enter
PS d:\apache-cassandra-2.1.7\bin> . \cqlsh127.0.0.1:90425.0.1 | Cassandra 2.1.7 | CQL Spec 3.2.0 | for help . Warning:pyreadline dependency missing. Install to enable tab COMPLETION.CQLSH>
First introduce keyspace, Chinese literal translation is a key value space, is actually a concept of a namespace, which is similar to the database in the relational database. Then we'll check what Keyspace:
Show Keyspace
Cqlsh> describe keyspaces; mykeyspace simplex system_traces system
The results show a query to 4 keyspace, the Scarlet Letter indicates the part. Then we manually create a keyspace ourselves:
Create Keyspace
Mycas With REPLICATION = {' class ': ' Simplestrategy ', ' replication_factor ': 1}; Cqlsh> describe keyspaces; mycas mykeyspace simplex system_traces system
You can see that you have successfully created a keyspace named Mycas. We use the keyspace that we just created:
Select Keyspace
cqlsh> use mycas; Cqlsh:mycas>
We create a table user under the current keyspace and display all the tables under the current keyspace:
Create a table
cqlsh:mycas> CREATE TABLE user ( ID int, user_name varchar, PRIMARY KEY (id)); Cqlsh:mycas> describe tables; User
As the results show, only the user table we just created is currently under Keyspace. Insert a piece of data into the table:
Adding data to a table
cqlsh> use mycas; Cqlsh:mycasINSERT into Users (Id,user_name) VALUES (1, ' Zhangsan '); Cqlsh:mycasSELECT * from users;| user_name----+----------- 1 | Zhangsan
Then we make a simple conditional query:
Querying data from a table
SELECT * from users where id=1 ;| user_name----+----------- 1 | Zhangsan(1 rows) Cqlsh:mycas SELECT * from users where user_name= ' Zhangsan '; Invalidrequest:code=2200 [Invalid query] message=""
You can see that the query primary key is OK, but the query does not have an indexed user_name but cannot query.
We create an index and try again:
Create an index
cqlsh:mycas> CREATE index on users (user_name); Cqlsh:mycasSELECT * from users where user_name=' Zhangsan ';| user_name----+----------- 1 | Zhangsan
Try the update:
//Update data in a table
Cqlsh:mycas>Update users set user_name= ' Lisi '; Syntaxexception: <errormessage Code=2000 [Syntax ErrorinchCQL query] Message="Line 1:33 mismatched input '; ' Expecting K_where">Cqlsh:mycas>Update users set user_name= ' Lisi ' where id=1; Cqlsh:mycas>SELECT *From users; ID|user_name----+-----------1|Lisi
You can see that updates can only be made by criteria.
Try deleting:
Delete data from a table
inch CQL query] message="line1:17 mismatched input '; expecting k_where">Cqlsh:mycas > Delete from users where id=1; Cqlsh:mycasSELECT * from users;| user_name----+-----------(0 rows)
You can see that the deletion can only be deleted by condition.
More specific commands can be found in Cassandra's official CQL documentation, very comprehensive.
NoSQL Tour--cassandra's CQL profile (ii)