本文主要關(guān)注兩個(gè)部分,
1. 怎么寫一個(gè)最簡(jiǎn)單cassandra的sample
2. 通過(guò)代碼,了解cassandra的數(shù)據(jù)模型及隱藏在后面的交互邏輯
步驟一:
首先我們創(chuàng)建一個(gè)工程,然后將cassandra/lib目錄下的包,導(dǎo)入到我們的工程中。
步驟二:
創(chuàng)建一個(gè)類,內(nèi)容如下:
- import org.apache.cassandra.thrift.Cassandra;
- import org.apache.cassandra.thrift.Column;
- import org.apache.cassandra.thrift.ColumnPath;
- import org.apache.cassandra.thrift.ConsistencyLevel;
- import org.apache.cassandra.thrift.InvalidRequestException;
- import org.apache.cassandra.thrift.NotFoundException;
- import org.apache.cassandra.thrift.TimedOutException;
- import org.apache.cassandra.thrift.UnavailableException;
- import org.apache.thrift.TException;
- import org.apache.thrift.protocol.TBinaryProtocol;
- import org.apache.thrift.transport.TSocket;
- import org.apache.thrift.transport.TTransport;
- import org.apache.thrift.transport.TTransportException;
-
-
- public class SampleOne {
- static Cassandra.Client cassandraClient;
- static TTransport socket;
-
-
- private static void init() throws TTransportException {
- String server = "192.168.1.129";
-
- int port = 9160;
-
-
- socket = new TSocket(server, port);
- System.out.println(" connected to " + server + ":" + port + ".");
-
-
-
- TBinaryProtocol binaryProtocol = new TBinaryProtocol(socket, false, false);
- cassandraClient = new Cassandra.Client(binaryProtocol);
-
-
-
- socket.open();
- }
-
-
- public static void main(String[] args) throws TException, TimedOutException, InvalidRequestException, UnavailableException, NotFoundException {
-
- init();
-
-
-
- String keyspace= "Keyspace1";
- String row = "employee";
-
-
- String tableName = "Standard2";
-
-
- insertOrUpdate(keyspace,tableName,row,"name","happy birthday!",System.currentTimeMillis());
-
-
-
- Column column = getByColumn(keyspace,tableName,row,"name", System.currentTimeMillis());
- System.out.println("read row " + row);
- System.out.println("column name " + ":" + new String(column.name));
- System.out.println("column value" + ":" + new String(column.value));
- System.out.println("column timestamp" + ":" + (column.timestamp));
-
- close();
- }
-
-
-
-
- public static void insertOrUpdate(String tableSpace,String tableName, String rowParam,String ColumnName,String ColumnValue,long timeStamp)
- throws TException, TimedOutException, InvalidRequestException, UnavailableException, NotFoundException{
-
- String keyspace= tableSpace;
-
- String row = rowParam;
-
-
- ColumnPath col = new ColumnPath(tableName);
- col.setColumn(ColumnName.getBytes());
-
-
-
-
- cassandraClient.insert(keyspace, row, col,"i don't know".getBytes(), System.currentTimeMillis(), ConsistencyLevel.ONE);
- }
-
-
-
-
- public static void delete(String tableSpace,String tableName, String rowParam,String ColumnName,long timeStamp)
- throws TException, TimedOutException, InvalidRequestException, UnavailableException, NotFoundException{
-
- String keyspace= tableSpace;
-
- String row = rowParam;
-
-
- ColumnPath col = new ColumnPath(tableName);
- col.setColumn(ColumnName.getBytes());
-
-
-
-
- cassandraClient.remove(keyspace, row, col, System.currentTimeMillis(), ConsistencyLevel.ONE);
- }
-
-
- * 獲取數(shù)據(jù)
- */
- public static Column getByColumn(String tableSpace,String tableName, String rowParam,String ColumnName,long timeStamp)
- throws TException, TimedOutException, InvalidRequestException, UnavailableException, NotFoundException{
-
- String keyspace= tableSpace;
-
- String row = rowParam;
-
-
- ColumnPath col = new ColumnPath(tableName);
- col.setColumn(ColumnName.getBytes());
-
-
-
-
- Column column = cassandraClient.get(keyspace, row, col, ConsistencyLevel.ONE).column;
- return column;
- }
-
-
-
-
-
- public static void close() {
- socket.close();
- }
- }
import org.apache.cassandra.thrift.Cassandra;
import org.apache.cassandra.thrift.Column;
import org.apache.cassandra.thrift.ColumnPath;
import org.apache.cassandra.thrift.ConsistencyLevel;
import org.apache.cassandra.thrift.InvalidRequestException;
import org.apache.cassandra.thrift.NotFoundException;
import org.apache.cassandra.thrift.TimedOutException;
import org.apache.cassandra.thrift.UnavailableException;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
public class SampleOne {
static Cassandra.Client cassandraClient;
static TTransport socket;
private static void init() throws TTransportException {
String server = "192.168.1.129";
// String server = "localhost";
int port = 9160;
/* 首先指定cassandra server的地址 */
socket = new TSocket(server, port);
System.out.println(" connected to " + server + ":" + port + ".");
/* 指定通信協(xié)議為二進(jìn)制流協(xié)議 */
TBinaryProtocol binaryProtocol = new TBinaryProtocol(socket, false, false);
cassandraClient = new Cassandra.Client(binaryProtocol);
/* 建立通信連接 */
socket.open();
}
public static void main(String[] args) throws TException, TimedOutException, InvalidRequestException, UnavailableException, NotFoundException {
/* 初始化連接 */
init();
/* 選擇需要操作的Keyspaces, 可以理解成數(shù)據(jù)庫(kù)的表 */
String keyspace= "Keyspace1";
String row = "employee";
/* 創(chuàng)建一個(gè)Table Name */
String tableName = "Standard2";
/* 插入一條記錄 */
insertOrUpdate(keyspace,tableName,row,"name","happy birthday!",System.currentTimeMillis());
/* 刪除一條記錄 */
//delete(keyspace,tableName,row,"name",System.currentTimeMillis());
/* 獲取一條記錄 (由于插入和刪除是同一條記錄,有可能會(huì)檢索不到哦!請(qǐng)大家主意!*/
Column column = getByColumn(keyspace,tableName,row,"name", System.currentTimeMillis());
System.out.println("read row " + row);
System.out.println("column name " + ":" + new String(column.name));
System.out.println("column value" + ":" + new String(column.value));
System.out.println("column timestamp" + ":" + (column.timestamp));
close();
}
/**
* 插入記錄
*/
public static void insertOrUpdate(String tableSpace,String tableName, String rowParam,String ColumnName,String ColumnValue,long timeStamp)
throws TException, TimedOutException, InvalidRequestException, UnavailableException, NotFoundException{
/* 選擇需要操作的Keyspaces, 存放數(shù)據(jù)表所在的空間位置 */
String keyspace= tableSpace;
/* 數(shù)據(jù)所在的行標(biāo) */
String row = rowParam;
/* 創(chuàng)建一個(gè)column path */
ColumnPath col = new ColumnPath(tableName);
col.setColumn(ColumnName.getBytes());
/* 執(zhí)行插入操作,指定keysapce, row, col, 和數(shù)據(jù)內(nèi)容, 后面兩個(gè)參數(shù)一個(gè)是timestamp, 另外一個(gè)是consistency_level
* timestamp是用來(lái)做數(shù)據(jù)一致性保證的, 而consistency_level是用來(lái)控制數(shù)據(jù)分布的策略,前者的理論依據(jù)是bigtable, 后者的理論依據(jù)是dynamo
*/
cassandraClient.insert(keyspace, row, col,"i don't know".getBytes(), System.currentTimeMillis(), ConsistencyLevel.ONE);
}
/**
* 刪除記錄
*/
public static void delete(String tableSpace,String tableName, String rowParam,String ColumnName,long timeStamp)
throws TException, TimedOutException, InvalidRequestException, UnavailableException, NotFoundException{
/* 選擇需要操作的Keyspaces, 存放數(shù)據(jù)表所在的空間位置 */
String keyspace= tableSpace;
/* 數(shù)據(jù)所在的行標(biāo) */
String row = rowParam;
/* 創(chuàng)建一個(gè)column path */
ColumnPath col = new ColumnPath(tableName);
col.setColumn(ColumnName.getBytes());
/* 執(zhí)行刪除操作,指定keysapce, row, col, 后面兩個(gè)參數(shù)一個(gè)是timestamp, 另外一個(gè)是consistency_level
* timestamp是用來(lái)做數(shù)據(jù)一致性保證的, 而consistency_level是用來(lái)控制數(shù)據(jù)分布的策略,前者的理論依據(jù)是bigtable, 后者的理論依據(jù)是dynamo
*/
cassandraClient.remove(keyspace, row, col, System.currentTimeMillis(), ConsistencyLevel.ONE);
}
/**
* 獲取數(shù)據(jù)
*/
public static Column getByColumn(String tableSpace,String tableName, String rowParam,String ColumnName,long timeStamp)
throws TException, TimedOutException, InvalidRequestException, UnavailableException, NotFoundException{
/* 選擇需要操作的Keyspaces, 存放數(shù)據(jù)表所在的空間位置 */
String keyspace= tableSpace;
/* 數(shù)據(jù)所在的行標(biāo) */
String row = rowParam;
/* 創(chuàng)建一個(gè)column path */
ColumnPath col = new ColumnPath(tableName);
col.setColumn(ColumnName.getBytes());
/* 執(zhí)行查詢操作,指定keysapce, row, col, timestamp
* timestamp是用來(lái)做數(shù)據(jù)一致性保證的, 而consistency_level是用來(lái)控制數(shù)據(jù)分布的策略,前者的理論依據(jù)是bigtable, 后者的理論依據(jù)是dynamo
*/
Column column = cassandraClient.get(keyspace, row, col, ConsistencyLevel.ONE).column;
return column;
}
/**
* 關(guān)閉當(dāng)前的遠(yuǎn)程訪問(wèn)連接
*/
public static void close() {
socket.close();
}
}
為了比較好的理解這些名詞解釋,我們先看看cassandra的數(shù)據(jù)模型:
Cassandra 的數(shù)據(jù)模型的基本概念:
keyspace:
用于存放 ColumnFamily 的容器,相當(dāng)于關(guān)系數(shù)據(jù)庫(kù)中的 Schema 或 database,
ColumnFamily :
用于存放 Column 的容器,類似關(guān)系數(shù)據(jù)庫(kù)中的 table 的概念。
SuperColumn :
它是一個(gè)特列殊的 Column, 它的 Value 值可以包函多個(gè) Column
- {
- name: "李明杰",
-
- value: {
- street: {name: "street", value: "1234 x street", timestamp: 123456789},
- city: {name: "city", value: "san francisco", timestamp: 123456789},
- zip: {name: "zip", value: "94107", timestamp: 123456789},
- }
- }
{ // 這是一個(gè)SuperColumn
name: "李明杰",
// 包含一系列的Columns
value: {
street: {name: "street", value: "1234 x street", timestamp: 123456789},
city: {name: "city", value: "san francisco", timestamp: 123456789},
zip: {name: "zip", value: "94107", timestamp: 123456789},
}
}
Columns:
Cassandra 的最基本單位。由 name , value , timestamp 組成
- {
- name: "李明杰",
- value: "mydream.limj@gmali.com",
- timestamp: 123456789
- }
{ // 這是一個(gè)column
name: "李明杰",
value: "mydream.limj@gmali.com",
timestamp: 123456789
}
cassandra的數(shù)據(jù)模型主要就是由上述幾種模型構(gòu)建而成的,很簡(jiǎn)單吧,的確是這樣,最大的好處就是讀寫數(shù)據(jù)的API非常簡(jiǎn)單.