启动cassandra-cli服务之后,可以进行CQL的使用。
1. 创建keyspace 可以理解成关系数据库的database
[default@testkeyspace] create keyspace test_keyspace;6f22a515-d0d9-3587-b467-cf9639b8b82c
2. 创建列column family,可以理解为table
下面创建了一个列族User,并以UTF8的方式进行排序
[default@testkeyspace] use test_keyspace;Authenticated to keyspace: test_keyspace[default@test_keyspace] create column family User with comparator = UTF8Type;59259f5d-7545-3f04-9050-840de78a627b
3. 插入记录
下面插入两条记录,分别是地址和电话号码
[default@test_keyspace] set User[utf8('zhangsan')][utf8('address')]=utf8('beijing');Value inserted.Elapsed time: 4.44 msec(s).[default@test_keyspace] set User[utf8('zhangsan')][utf8('phone')]=utf8('1520139');Value inserted.Elapsed time: 3.64 msec(s).
4. 查询记录
[default@test_keyspace] get User[utf8('zhangsan')];=> (name=address, value=beijing, timestamp=1444190236635000)=> (name=phone, value=1520139, timestamp=1444190257649000)Returned 2 results.Elapsed time: 6.24 msec(s).
5. 创建超级列
[default@test_keyspace] create column family supertest with comparator = UTF8Type and column_type = Super;04798230-c918-301d-9c78-12cb45ee37d5
6.向超级列插入数据
下面向刚刚创建的supertest超级列插入数据
[default@test_keyspace] set supertest[utf8('zhangsan')][utf8('address')][utf8('family')]=utf8('beijing');Value inserted.Elapsed time: 4.3 msec(s).[default@test_keyspace] set supertest[utf8('zhangsan')][utf8('address')][utf8('company')]=utf8('shanghai');Value inserted.Elapsed time: 4.07 msec(s).
7. 查询超级列的数据
下面supertest超级列刚刚插入的数据
[default@test_keyspace] get supertest[utf8('zhangsan')];=> (super_column=address, (name=636f6d70616e79, value=shanghai, timestamp=1444190932884000) (name=66616d696c79, value=beijing, timestamp=1444190909162000))Returned 1 results.Elapsed time: 8.36 msec(s).
8. 删除列族:
下面删除刚刚创建的超级列supertest
[default@test_keyspace] drop column family supertest;59259f5d-7545-3f04-9050-840de78a627b