java操作cassandra(安装)

从apache官网下载最新的cassandra,目前最新的版本是2.1.2

解压,放到一个固定目录下。

 

修改conf/cassandra.yaml

替换其中的var/lib/cassandra/ 为安装目录

 

修改conf/logback.xml

替换其中的${cassandra.logdir} 为安装目录

 

配置环境变量

添加环境变量CASSANDRA_HOME,值为安装目录,然后添加path环境变量为%CASSANDRA_HOME%\bin

 

启动

进入命令行工具,输入cassandra然后回车,显示如下说明启动成功。

 INFO  02:49:53 Enqueuing flush of local: 49395 (0%) on-heap, 0 (0%) off-heap  
INFO  02:49:53 Writing Memtable-local@26225981(8561 serialized bytes, 259 ops, 0%/0% of on/off-heap limit)  
INFO  02:49:53 Completed flushing D:\Program Files\apache-cassandra-2.1.2\data\data\system\local-7ad54392bcdd35a684174e047860b377\syste  
sition(segmentId=1419389392265, position=98921)  
INFO  02:49:53 Node localhost/127.0.0.1 state jump to normal  
INFO  02:49:53 Compacted 4 sstables to [D:\Program Files\apache-cassandra-2.1.2\data\data\system\local-7ad54392bcdd35a684174e047860b377  
n 218ms = 0.024979MB/s.  4 total partitions merged to 1.  Partition merge counts were {4:1, }  
INFO  02:49:53 Netty using Java NIO event loop  
INFO  02:49:53 Using Netty Version: [netty-buffer=netty-buffer-4.0.23.Final.208198c, netty-codec=netty-codec-4.0.23.Final.208198c, nett  
ocks=netty-codec-socks-4.0.23.Final.208198c, netty-common=netty-common-4.0.23.Final.208198c, netty-handler=netty-handler-4.0.23.Final.2  
ty-transport-rxtx=netty-transport-rxtx-4.0.23.Final.208198c, netty-transport-sctp=netty-transport-sctp-4.0.23.Final.208198c, netty-tran  
INFO  02:49:53 Starting listening for CQL clients on localhost/127.0.0.1:9042...  
INFO  02:49:53 Binding thrift service to localhost/127.0.0.1:9160  
INFO  02:49:53 Listening for thrift clients...


启动客户端工具

重新启动一个命令行,输入cassandra-cli,然后回车,显示如下:

<EMBED height=14 type=application/x-shockwave-flash width=29 src=http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf quality="best">
Microsoft Windows [版本 6.1.7601]  
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。  
  
C:\Users\Administrator>cassandra-cli  
Starting Cassandra Client  
Connected to: "Test Cluster" on 127.0.0.1/9160  
Welcome to Cassandra CLI version 2.1.2  
  
The 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-cql3  
  
Type 'help;' or '?' for help.  
Type 'quit;' or 'exit;' to quit.  
  
[default@unknown]

启动过程中遇到的错误

<EMBED height=14 type=application/x-shockwave-flash width=29 src=http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf quality="best">
1:  
Windows PowerShell 内部错误。加载托管的 Windows PowerShell 失败,返回错误 80070002。  
WARNING! Powershell script execution unavailable.  
   Please use 'powershell Set-ExecutionPolicy Unrestricted'  
   on this user-account to run cassandra with fully featured  
   functionality on this platform.  
Starting with legacy startup options  
Starting Cassandra Server  
Error occurred during initialization of VM  
Could not reserve enough space for 2097152KB object heap  
  
解决办法:  
安装补丁Windows Management Framework 4.0 (KB2819745)可解决.  
地址:http://www.microsoft.com/en-us/download/details.aspx?id=40855  
  
  
2:  
Could not reserve enough space for 2097152KB object heap  
  
解决办法:  
修改 cassandra.bat 中的JVM参数  
set JAVA_OPTS=-ea^  
 -javaagent:"%CASSANDRA_HOME%\lib\jamm-0.2.8.jar"^  
 -Xms512m^  
 -Xmx512m^

 

启动

进入bin目录,运行cassandra.bat即可,如下:(省略了一些启动信息)。
(segmentId=1363752903001, position=50834)
 INFO 12:15:04,031 Node localhost/127.0.0.1 state jump to normal
 INFO 12:15:04,031 Startup completed! Now serving reads.
 INFO 12:15:04,062 Not starting native transport as requested. Use JMX (StorageService->startNativeTransport()) to start it
 INFO 12:15:04,062 Binding thrift service to localhost/127.0.0.1:9160
 INFO 12:15:04,093 Using TFramedTransport with a max frame size of 15728640 bytes.
 INFO 12:15:04,109 Using synchronous/threadpool thrift server on localhost : 9160
 INFO 12:15:04,109 Listening for thrift clients...

启动客户端工具

进入bin目录,运行cassandra-cli.bat即可,如下:
Starting Cassandra Client
Connected to: "Test Cluster" on 127.0.0.1/9160
Welcome to Cassandra CLI version 1.2.3

Type 'help;' or '?' for help.
Type 'quit;' or 'exit;' to quit.

[default@unknown]

创建keyspace

keyspace就相当于数据库。创建命令如下:
[default@unknown] create keyspace mykeyspace;  
986241c1-59d8-3579-972d-d4881ea931b7  
[default@unknown]  

使用创建的keyspace

[default@unknown] use mykeyspace;
Authenticated to keyspace: mykeyspace
[default@mykeyspace]

创建列族column family

 column family 就相当于关系数据库中的表。
[default@mykeyspace] create column family User;
3c077f98-7039-372e-8447-c66c2fb38432
[default@mykeyspace]

插入数据

[default@mykeyspace] set User[ascii('zhangsan')][ascii('name')]=ascii('zhangsan');
Value inserted.
Elapsed time: 48 msec(s).
[default@mykeyspace] set User[ascii('zhangsan')][ascii('age')]=ascii('18');
Value inserted.
Elapsed time: 6.97 msec(s).
[default@mykeyspace]

统计数据

[default@mykeyspace] count User[ascii('zhangsan')];
2 columns
[default@mykeyspace]

查询数据

default@mykeyspace] get User[ascii('zhangsan')];
> (column=616765, value=18, timestamp=1363753306031000)
> (column=6e616d65, value=zhangsan, timestamp=1363753293734000)
eturned 2 results.
lapsed time: 21 msec(s).
default@mykeyspace]

删除数据

[default@mykeyspace] del User[ascii('zhangsan')];
row removed.
Elapsed time: 12 msec(s).
[default@mykeyspace] get User[ascii('zhangsan')];
Returned 0 results.
Elapsed time: 1.68 msec(s).


 

 

你可能感兴趣的:(java操作cassandra(安装))