Windows环境下安装Cassandra1.0.2
為什么80%的碼農都做不了架構師?>>> ??
以Cassandra 1.0.2為例。
1、 安裝文件準備
下載Cassandra1.0.2、Jdk 1.7。(全部推薦最新版本)
Cassandra下載地址:http://cassandra.apache.org/download/
Jre下載地址:http://www.java.com/zh_CN/download/
2、安裝JDK、Cassandra
(一)、安裝Jdk的過程很簡單,這里不做說明。
(二) 、解壓縮后獲得目錄apache-cassandra-1.0.2
(三)、配置JDK、Cassandra系統環境變量
1、配置JDK系統環境變量
2、配置Cassandra系統環境變量
3、將cassandra系統環境變量添加到JDK環境變量中
3、配置Cassandra
(1)修改其它地址
打開文件:cassandra.yaml,修改:
data_file_directories: - D:\apache-cassandra-1.0.2\data
commitlog_directory: D:\apache-cassandra-1.0.2\commitlog
saved_caches_directory: D:\apache-cassandra-1.0.2\saved_caches
修改后的樣子如下:
data_file_directories:
??? - F:\javadevelop\service\casssandra\apache-cassandra-1.0.2\data
commitlog_directory: F:\javadevelop\service\casssandra\apache-cassandra-1.0.2\commitlog
saved_caches_directory: F:\javadevelop\service\casssandra\apache-cassandra-1.0.2\saved_caches
4、啟動
(1)在命令提示符工具中啟動Cassandra:
(2)用nodetool工具確認Cassandra已經啟動:
5、測試
(1)運行cassadnra-cli:
(2)創建keyspace、column family,插入數據:
(3)獲取數據:
可以正常執行,說明安裝成功。
6、Java客戶端代碼
(1)、創建java項目的過程很簡單,這里不做說明。
(2)、將F:\javadevelop\service\casssandra\apache-cassandra-1.0.2\lib目錄下的jar全部拷貝到WEB-INF/lib目錄下;
(3)、創建客戶端java類,code如下:
package com.tzdr.api.controller;
import org.apache.cassandra.thrift.*;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.List;
public class CassandraExample {
?? ?public static void main(String[] args)
??????????? throws TException, InvalidRequestException, UnavailableException, UnsupportedEncodingException, NotFoundException, TimedOutException {
??????? TTransport tr = new TFramedTransport(new TSocket("localhost", 9160));
??????? TProtocol proto = new TBinaryProtocol(tr);
??????? Cassandra.Client client = new Cassandra.Client(proto);
??????? tr.open();
?
??????? String key_user_id = "jsmith";???? //row id
?
??????? client.set_keyspace("tzdrdevelop");???? //指定keyspace
??????? ColumnParent parent = new ColumnParent("User");
??????? ColumnPath path = new ColumnPath("User");
?
??????? // 讀取單列內容
??????? path.setColumn(toByteBuffer("first"));
??????? ColumnOrSuperColumn col = (client.get(toByteBuffer(key_user_id), path, ConsistencyLevel.ONE));
??????? System.out.println(toString(col.column.name) + " -> " + toString(col.column.value));
?
??????? // 讀取全部列內容
??????? SlicePredicate predicate = new SlicePredicate();
??????? SliceRange sliceRange = new SliceRange(toByteBuffer(""), toByteBuffer(""), false, 10);
??????? predicate.setSlice_range(sliceRange);
?
??????? List<ColumnOrSuperColumn> results = client.get_slice(toByteBuffer(key_user_id), parent, predicate, ConsistencyLevel.ONE);
??????? for (ColumnOrSuperColumn result : results) {
??????????? Column column = result.column;
??????????? System.out.println(toString(column.name) + " -> " + toString(column.value));
??????? }
??????? // 向數據庫中插入數據
??????? long currentTimeMillis = System.currentTimeMillis();
??????? long nanoTime = System.nanoTime();
??????? long currentTimeMicros = currentTimeMillis * 1000 + nanoTime / 1000 - (nanoTime > 1000000 ? (nanoTime / 1000000) * 1000 : 0);
?
??? //注意取microseconds,而不是milliseconds,因為其它客戶機都是microseconds。
??? //如果使用System.currentTimeMillis()導致你的時間永遠小于其它客戶機的時間,使其操作會被忽略。
??????? System.out.println(currentTimeMicros);
?
??????? String new_key_user_id = "jsmith";???? //row id
?
??????? Column nameColumn = new Column(toByteBuffer("name"));
??????? nameColumn.setValue(toByteBuffer("Chris Goffinet"));
??????? nameColumn.setTimestamp(currentTimeMicros);
??????? client.insert(toByteBuffer(new_key_user_id), parent, nameColumn, ConsistencyLevel.ONE);
?
??????? Column pwdColumn = new Column(toByteBuffer("password"));
??????? pwdColumn.setValue(toByteBuffer("12345678"));
??????? pwdColumn.setTimestamp(currentTimeMicros);
??????? client.insert(toByteBuffer(new_key_user_id), parent, pwdColumn, ConsistencyLevel.ONE);
?
??????? tr.flush();
??????? tr.close();
??? }
?
??? public static ByteBuffer toByteBuffer(String value)
??????????? throws UnsupportedEncodingException {
??????? return ByteBuffer.wrap(value.getBytes("UTF-8"));
??? }
?
??? public static String toString(ByteBuffer buffer)
??????????? throws UnsupportedEncodingException {
??????? byte[] bytes = new byte[buffer.remaining()];
??????? buffer.get(bytes);
??????? return new String(bytes, "UTF-8");
??? }
}
轉載于:https://my.oschina.net/wpq/blog/674501
總結
以上是生活随笔為你收集整理的Windows环境下安装Cassandra1.0.2的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: lua中元素的下标是从1开始的
- 下一篇: 斜面上的根骨骼运动以及刚体测试