1.安装casssendra
1> 下载Java jdk:
下载地址:
我这里用的是:jdk-7u17-linux-i586.tar.gz
PS:这个是需要登录才能下载的,所以建议注册后,登录进去先下载到本地,然后再从本地计算机传到VM上去。
2> 下载完成后,解压jdk:
tar -zxvf jdk-7u17-linux-i586.tar.gz
3> 解压完成后,配置环境变量:
vim /etc/profile
在打开的文件的末尾,增加如下代码:
JAVA_HOME=/usr/local/jdk1.7.0_17 PATH=$JAVA_HOME/bin:$PATH CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export JAVA_HOME export PATH export CLASSPATH
PS:只需将JAVA_HOME的路径改一下即可,改成刚才解压后jdk的目录目录即可,我的是把jdk压缩包放在/usr/local目录下了,然后直接
在这里解压的,所以解压后的包也就直接在该目录下了【jdk1.7.0_17】,所以我这里JAVA_HOME的路径为 "/usl/local/jdk1.7.0_17"
4> 上面的步骤执行完成后,执行下如下命令
source /etc/profile
或者重启下linux ,配置才可生效
5> 执行完上面的步骤,下一步就是看一下java jdk是否安装成功,执行下如下命令
java -version
说明jdk安装成功,这里的版本是1.7
支持jdk的安装到此结束,下一步是cassandra的安装
==================================================================
1> 下载Cassandra:
地址:
我这里用的版本是:apache-cassandra-1.2.4-bin.tar.gz
2> 下载完成后,直接解压:
tar -zxvf apache-cassandra-1.2.4-bin.tar.gz
解压完成后,得到如下目录:apache-cassandra-1.2.4
进入该目录下的bin目录,直接启动Cassandra即可:
cd apache-cassandra-1.2.4/bin
./cassandra -f 【启动cassandra】
启动后,会输出一大堆的代码,如下,表明Cassandra启动成功
2. 基本命令
2.1 创建keyspace和列族
输入cassendra-cli命令进入客户端
[nxuser@PSjamsBond-0-0-0 bin]$ ./cassandra-cliConnected to: "Test Cluster" on 127.0.0.1/9160Welcome to Cassandra CLI version 2.1.0The CLI is deprecated and will be removed in Cassandra 3.0. Consider migrating to cqlsh.CQL is fully backwards compatible with Thrift data; see http://www.datastax.com/dev/blog/thrift-to-cql3Type 'help;' or '?' for help.Type 'quit;' or 'exit;' to quit.[default@unknown]
可用命令如下:
[default@unknown] help;Getting around:? Display this help.help; Display this help.help; Display command-specific help.exit; Exit this utility.quit; Exit this utility.Commands:assume Apply client side validation.connect Connect to a Cassandra node.consistencylevel Sets consisteny level for the client to use.count Count columns or super columns.create column family Add a column family to an existing keyspace.create keyspace Add a keyspace to the cluster.del Delete a column, super column or row.decr Decrements a counter column.describe cluster Describe the cluster configuration.describe Describe a keyspace and its column families or column family in current keyspace.drop column family Remove a column family and its data.drop keyspace Remove a keyspace and its data.drop index Remove an existing index from specific column.get Get rows and columns.incr Increments a counter column.list List rows in a column family.set Set columns.show api version Show the server API version.show cluster name Show the cluster name.show keyspaces Show all keyspaces and their column families.show schema Show a cli script to create keyspaces and column families.truncate Drop the data in a column family.update column family Update the settings for a column family.update keyspace Update the settings for a keyspace.use Switch to a keyspace.
[default@unknown] connect localhost/9160;Connected to: "Test Cluster" on localhost/9160
默认端口为9160,同样可以再配置文件修改。
创建自己的keysapce:
[default@unknown] create keyspace Mykeyspace;99d2e053-5150-33da-87c0-fdbdfbad50e9
进入到刚刚创建的keyspace
[default@unknown] use Mykeyspace;Authenticated to keyspace: Mykeyspace[default@Mykeyspace]
创建列族:
[default@Mykeyspace] create column family User;d939bfeb-1dc9-3359-b630-42c05d5ecd1d
利用describe命令查看
[default@Mykeyspace] describe ;WARNING: CQL3 tables are intentionally omitted from 'describe' output.See https://issues.apache.org/jira/browse/CASSANDRA-4377 for details.Keyspace: Mykeyspace: Replication Strategy: org.apache.cassandra.locator.NetworkTopologyStrategy Durable Writes: true Options: [datacenter1:1] Column Families: ColumnFamily: User Key Validation Class: org.apache.cassandra.db.marshal.BytesType Default column value validator: org.apache.cassandra.db.marshal.BytesType Cells sorted by: org.apache.cassandra.db.marshal.BytesType GC grace seconds: 864000 Compaction min/max thresholds: 4/32 Read repair chance: 0.0 DC Local Read repair chance: 0.1 Caching: KEYS_ONLY Default time to live: 0 Bloom Filter FP chance: default Index interval: default Speculative Retry: NONE Built indexes: [] Compaction Strategy: org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy Compression Options: sstable_compression: org.apache.cassandra.io.compress.LZ4Compressor
2.2 插入数据
尝试下面的插入方法,不行,失败。
[default@Mykeyspace] set User['name']='kerry';
No cell name specified, (type 'help;' or '?' for help on syntax).
修改如下,仍然错误:
[default@Mykeyspace] set User['kerry'] ['telephone']='888888';
org.apache.cassandra.serializers.MarshalException: cannot parse 'telephone' as hex bytes
查到解决方法, 编码问题,修改如下:
[default@Mykeyspace] create column family users with comparator=UTF8Type and default_validation_class=UTF8Type and key_validation_class=UTF8Type;288937e8-1d1b-35a2-b804-ef9adbf6e514[default@Mykeyspace] SET users['bobbyjo']['full_name']='RobertJones';Value inserted.Elapsed time: 30 msec(s)
在这个例子中我们采用key为bobbyjo,在bobbyjo上插入了很多列,它们属于users这个列族,记住bobbyjo只是一行的标识。注意我们每次只能插入一列。
在接下来的例子中我们采用yomama作为行的key,在users这个列族中插入若干列和值。
[default@Mykeyspace] SET users['yomama']['full_name']='CathySmith';Value inserted.Elapsed time: 1.74 msec(s).
查看如下:
[default@Mykeyspace] count users['bobbyjo'];1 cells
[default@Mykeyspace] get users['bobbyjo'];=> (name=full_name, value=RobertJones, timestamp=1413356337257000)Returned 1 results.Elapsed time: 22 msec(s).
删除一列如下命令:
[default@Mykeyspace] set users[aaa][111]='first';Value inserted.Elapsed time: 1.95 msec(s).[default@Mykeyspace] set users[bbb][222]='second';Value inserted.Elapsed time: 2.25 msec(s).[default@Mykeyspace] get users[aaa];=> (name=111, value=first, timestamp=1413356864597000)=> (name=333, value=third, timestamp=1413356964165000)Returned 2 results.Elapsed time: 2.45 msec(s).[default@Mykeyspace] del users[aaa][111];cell removed.Elapsed time: 1.7 msec(s).[default@Mykeyspace] get users[aaa];=> (name=333, value=third, timestamp=1413356964165000)Returned 1 results.Elapsed time: 2.1 msec(s).