NoSql Cassandra

咱們爲何要使用NOSQL非關係數據庫?
 隨着互聯網web2.0網站的興起,非關係型的數據庫如今成了一個極其熱門的新領域,非關係數據庫產品的發展很是迅速。而傳統的關係數據庫在應付web2.0網站,特別是超大規模和高併發的SNS類型的web2.0純動態網站已經顯得力不從心,暴露了不少難以克服的問題,例如:   
     一、High performance - 對數據庫高併發讀寫的需求   web2.0網站要根據用戶個性化信息來實時生成動態頁面和提供動態信息,因此基本上沒法使用動態頁面靜態化技術,所以數據庫併發負載很是高,每每要達到每秒上萬次讀寫請求。關係數據庫應付上萬次SQL查詢還勉強頂得住,可是應付上萬次SQL寫數據請求,硬盤IO就已經沒法承受了。其實對於普通的BBS網站,每每也存在對高併發寫請求的需求。  php

 二、Huge Storage - 對海量數據的高效率存儲和訪問的需求   對於大型的SNS網站,天天用戶產生海量的用戶動態,以國外的Friendfeed爲例,一個月就達到了2.5億條用戶動態,對於關係數據庫來講,在一張2.5億條記錄的表裏面進行SQL查詢,效率是極其低下乃至不可忍受的。再例如大型web網站的用戶登陸系統,例如騰訊,盛大,動輒數以億計的賬號,關係數據庫也很難應付。  、html

 三、High Scalability && High Availability- 對數據庫的高可擴展性和高可用性的需求   在基於web的架構當中,數據庫是最難進行橫向擴展的,當一個應用系統的用戶量和訪問量與日俱增的時候,你的數據庫卻沒有辦法像web server和app server那樣簡單的經過添加更多的硬件和服務節點來擴展性能和負載能力。對於不少須要提供24小時不間斷服務的網站來講,對數據庫系統進行升級和擴展是很是痛苦的事情,每每須要停機維護和數據遷移,爲何數據庫不能經過不斷的添加服務器節點來實現擴展呢?   在上面提到的「三高」需求面前,關係數據庫遇到了難以克服的障礙,而對於web2.0網站來講,關係數據庫的不少主要特性卻每每無用武之地,例如:   一、數據庫事務一致性需求   不少web實時系統並不要求嚴格的數據庫事務,對讀一致性的要求很低,有些場合對寫一致性要求也不高。所以數據庫事務管理成了數據庫高負載下一個沉重的負擔。   二、數據庫的寫實時性和讀實時性需求   對關係數據庫來講,插入一條數據以後馬上查詢,是確定能夠讀出來這條數據的,可是對於不少web應用來講,並不要求這麼高的實時性。   三、對複雜的SQL查詢,特別是多表關聯查詢的需求   任何大數據量的web系統,都很是忌諱多個大表的關聯查詢,以及複雜的數據分析類型的複雜SQL報表查詢,特別是SNS類型的網站,從需求以及產品設計角度,就避免了這種狀況的產生。每每更多的只是單表的主鍵查詢,以及單表的簡單條件分頁查詢,SQL的功能被極大的弱化了。   所以,關係數據庫在這些愈來愈多的應用場景下顯得不那麼合適了,爲了解決這類問題的非關係數據庫應運而生。   NoSQL 是非關係型數據存儲的廣義定義。它打破了長久以來關係型數據庫與ACID理論大一統的局面。NoSQL 數據存儲不須要固定的表結構,一般也不存在鏈接操做。在大數據存取上具有關係型數據庫沒法比擬的性能優點。該術語在 2009 年初獲得了普遍認同。   當今的應用體系結構須要數據存儲在橫向伸縮性上可以知足需求。而 NoSQL 存儲就是爲了實現這個需求。Google 的BigTable與Amazon的Dynamo是很是成功的商業 NoSQL 實現。一些開源的 NoSQL 體系,如Facebook 的Cassandra, Apache 的HBase,也獲得了普遍認同。從這些NoSQL項目的名字上看不出什麼相同之處:Hadoop、Voldemort、Dynomite,還有其它不少。java

 

 

 

下面將以Cassandra爲例說明:node

 

Cassandra最簡單入門:web

這裏簡要介紹一下Cassandra入門,即安裝和簡單實用。 數據庫

下載cassandra,http://cassandra.apache.org/download/
解壓到 D:/apache-cassandra-0.5.1 apache

建立環境變量 CASSANDRA_HOME = D:/apache-cassandra-0.5.1 編程

設置conf/storage-conf.xml中的目錄路徑 ruby

<CommitLogDirectory>D:/cassandra/commitlog</CommitLogDirectory>
  <DataFileDirectories>
      <DataFileDirectory>D:/cassandra/data</DataFileDirectory>
  </DataFileDirectories>
  <CalloutLocation>D:/cassandra/callouts</CalloutLocation>
  <StagingFileDirectory>D:/cassandra/staging</StagingFileDirectory> 服務器

運行 D:/apache-cassandra-0.5.1/bin>cassandra -f

啓動後以下所示:

 

這裏服務端便啓動完畢,如今開啓另外一個cmd,在bin目錄下運行cassandra-cli,即cassandra客戶端。進入客戶端後,鍵入connect localhost/9160 連接服務器。連接後如圖:
 

如下讀寫cassandra數據

cassandra> set Keyspace1.Standard1['jsmith']['first'] = 'John'
  Value inserted.
  cassandra> set Keyspace1.Standard1['jsmith']['last'] = 'Smith'
  Value inserted.
  cassandra> set Keyspace1.Standard1['jsmith']['age'] = '42'
  Value inserted.
  cassandra> get Keyspace1.Standard1['jsmith']
    (column=age, value=42; timestamp=1249930062801)
    (column=first, value=John; timestamp=1249930053103)
    (column=last, value=Smith; timestamp=1249930058345)
  Returned 3 rows.
  cassandra>

Java代碼
     
set Keyspace1.Standard1[ 'jsmith' ][ 'first' ] =  'John'   
       /            /         /        /          /   
        /            /         /_ key   /          /_ value   
         /            /                  /_ column   
          /_ keyspace  /_ column family  

set Keyspace1.Standard1['jsmith']['first'] = 'John'
/ / / / /
/ / /_ key / /_ value
/ / /_ column
/_ keyspace /_ column family


Data stored in Cassandra is associated with a column family (Standard1),
which in turn is associated with a keyspace (Keyspace1). In the example
above, we set the value 'John' in the 'first' column for key 'jsmith'.

 

===============================================================================

Cassandra 入門詳解:

Cassandra 的數據存儲結構:

 

 

Cassandra 的數據模型是基於列族(Column Family)的四維或五維模型。它借鑑了 Amazon 的 Dynamo 和 Google's BigTable 的數據結構和功能特色,採用 Memtable 和 SSTable 的方式進行存儲。在 Cassandra 寫入數據以前,須要先記錄日誌 ( CommitLog ),而後數據開始寫入到 Column Family 對應的 Memtable 中,Memtable 是一種按照 key 排序數據的內存結構,在知足必定條件時,再把 Memtable 的數據批量的刷新到磁盤上,存儲爲 SSTable 。

 

圖 1. Cassandra 的數據模型圖:

 

 

 

Cassandra 的數據模型的基本概念:

1. Cluster : Cassandra 的節點實例,它能夠包含多個 Keyspace
2. Keyspace : 用於存放 ColumnFamily 的容器,至關於關係數據庫中的 Schema 或 database3. ColumnFamily : 用於存放 Column 的容器,相似關係數據庫中的 table 的概念 4. SuperColumn :它是一個特列殊的 Column, 它的 Value 值能夠包函多個 Column5. Columns:Cassandra 的最基本單位。由 name , value , timestamp 組成
下面是關於數據模型實例分析 :

圖 2. 數據模型實例分析

 


Cassandra 節點的安裝和配置

獲取 Cassandra

# wget http://labs.renren.com/apache-mirror/cassandra/0.6.0/apache-
cassandra-0.6.0-rc1-bin.tar.gz
# tar -zxvf apache-cassandra-0.6.0-rc1-bin.tar.gz
# mv apache-cassandra-0.6.0-rc1 cassandra
# ls Cassandra
Cassandra 的目錄說明

bin 存放與 Cassandra 操做的相關腳本
conf 存放配置文件的目錄
interface Cassandra 的 Thrift 接口定義文件,能夠用於生成各類編程語言的接口代碼
Javadoc 源代碼的 javadoc
lib Cassandra 運行時所需的 jar 包

配製 Cassandra 節點的數據存儲目錄和日誌目錄

修改配製文件 storage-conf.xml:

默認的內容

<CommitLogDirectory>/var/lib/cassandra/commitlog</CommitLogDirectory>
<DataFileDirectories>
<DataFileDirectory>/var/lib/cassandra/data</DataFileDirectory>
</DataFileDirectories>

配置後的內容

<CommitLogDirectory>/data3/db/lib/cassandra/commitlog</CommitLogDirectory>
<DataFileDirectories>
<DataFileDirectory>/data3/db/lib/cassandra/data</DataFileDirectory>
</DataFileDirectories>

修改日誌配製文件 log4j.properties:

log4j.properties 配置
# 日誌路徑
#log4j.appender.R.File=/var/log/cassandra/system.log
# 配置後的日誌路徑 :
log4j.appender.R.File=/data3/db/log/cassandra/system.log
建立文件存放數據和日誌的目錄

# mkdir – p /data3/db/lib/cassandra
# mkdir – p /data3/db/log/Cassandra

配製完成後,啓動 Cassandra

# bin/Cassandra

顯示信息

INFO 09:29:12,888 Starting up server gossip
INFO 09:29:12,992 Binding thrift service to localhost/127.0.0.1:9160

看到這兩行啓動回顯信息時,說明 Cassandra 已啓動成功。

鏈接到 Cassandra 並添加、獲取數據

Cassandra 的 bin 目錄已自帶了命令行鏈接工具 cassandra-cli,可以使用它鏈接到 Cassandra,並添加、讀取數據。

鏈接到 Cassandra,並添加、讀取數據

# bin/cassandra-cli --host localhost --port 9160
Connected to: "Test Cluster" on localhost/9160
Welcome to cassandra CLI.
Type 'help' or '?' for help. Type 'quit' or 'exit' to quit.
cassandra>
cassandra> set Keyspace1.Standard2['studentA']['age'] = '18'
Value inserted
cassandra> get Keyspace1.Standard2['studentA']
=> (column=age, value=18, timestamp=1272357045192000)
Returned 1 results

中止 Cassandra 服務

查出 Cassandra 的 pid:16328

# ps -ef | grep cassandra
# kill 16328

Cassandra 配製文件 storage-conf.xml 相關配製介紹

清單 1. storage-conf.xml 節點配製說明清單

<!-- 集羣時顯示的節點名稱 -->
<ClusterName>Test Cluster</ClusterName>
<!-- 節點啓動時,是否自動加入到集羣中,默認爲 false -->
<AutoBootstrap>false</AutoBootstrap>
<!-- 集羣的節點配製 -->
<Seeds>
<Seed>127.0.0.1</Seed>
</Seeds>
<!-- 節點之間通迅的監聽地址 -->
<ListenAddress>localhost</ListenAddress>
<!--
基於 Thrift 的 cassandra 客戶端監聽地址,
集羣時設爲:0.0.0.0 表示偵聽全部客戶端 , 默認爲:localhost
-->
<ThriftAddress>localhost</ThriftAddress>
<!-- 客戶端鏈接的端口 -->
<ThriftPort>9160</ThriftPort>
<!--
FlushDataBufferSizeInMB 將 memtables 上的數據寫入在 Disk 上,
超過設定好的限制大小時 ( 默認 32M),則將數據寫入磁盤,
FlushIndexBufferSizeInMB 超過設定的時長(默認 8 分鐘)後,
將 memtables 由的數據寫入磁盤中
-->
<FlushDataBufferSizeInMB>32</FlushDataBufferSizeInMB>
<FlushIndexBufferSizeInMB>8</FlushIndexBufferSizeInMB>
<!--
節點之間的日誌記錄同步模式。
默認:periodic, 對應配製 CommitLogSyncPeriodInMS
啓動 batch 時,則對應的配製 CommitLogSyncBatchWindowInMS
-->
<CommitLogSync>periodic</CommitLogSync>
<!-- 默認爲每 10 秒同步一第二天志記錄 -->
<CommitLogSyncPeriodInMS>10000</CommitLogSyncPeriodInMS>
<!--
<CommitLogSyncBatchWindowInMS>1</CommitLogSyncBatchWindowInMS> -->
經常使用編程語言使用 Cassandra 來存儲數據

在使用 Cassandra 時,一般狀況下都須要使用第三方插件 Thrift 來生成與 Cassandra 相關的庫文件 , 您能夠在 http://incubator.apache.org/thrift 下載此插件,並學習它的使用方法。如下是分別在 Java、PHP、Python、C#、Ruby 五種經常使用編程語言中使用 Cassandra:

Java 程序使用 Cassandra

把 libthrift-r917130.jar,apache-cassandra-0.6.0-rc1.jar 加入到 Eclipse 的編譯路徑中。

創建數據庫鏈接:使用 libthrift-r917130.jar 的 TTransport 的 open 方法創建起與 Cassandra 服務端 (IP:192.168.10.2 端口:9160) 的鏈接。

數據庫操做:使用 Cassandra.Client 建立一個客戶端實例。調用 Client 實例的 insert 方法寫入數據,經過 get 方法獲取數據。

關閉數據庫鏈接:使用 TTransport 的 close 方法斷開與 Cassandra 服務端的鏈接。


清單 2. Java 鏈接 Cassandra,寫入並讀取數據。

package com.test.cassandra;|
import java.io.UnsupportedEncodingException;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.TException;
import org.apache.cassandra.thrift.Cassandra;
import org.apache.cassandra.thrift.Column;
import org.apache.cassandra.thrift.ColumnOrSuperColumn;
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;
/*
* 使 Java 客戶端鏈接 Cassandra 並進行讀寫操做
* @author jimmy
* @date 2010-04-10
*/
public class JCassandraClient{
public static void main(String[] args) throws InvalidRequestException,
NotFoundException, UnavailableException, TimedOutException,
TException, UnsupportedEncodingException {

// 創建數據庫鏈接
TTransport tr = new TSocket("192.168.10.2", 9160);
TProtocol proto = new TBinaryProtocol(tr);
Cassandra.Client client = new Cassandra.Client(proto);
tr.open();
String keyspace = "Keyspace1";
String cf = "Standard2";
String key = "studentA";
// 插入數據
long timestamp = System.currentTimeMillis();
ColumnPath path = new ColumnPath(cf);
path.setColumn("age".getBytes("UTF-8"));
client.insert(keyspace,key,path,"18".getBytes("UTF-8"),
timestamp,ConsistencyLevel.ONE);
path.setColumn("height".getBytes("UTF-8"));
client.insert(keyspace,key,path,"172cm".getBytes("UTF-8"),
timestamp,ConsistencyLevel.ONE);
// 讀取數據
path.setColumn("height".getBytes("UTF-8"));
ColumnOrSuperColumn cc = client.get(keyspace, key, path, ConsistencyLevel.ONE);
Column c = cc.getColumn();
String v = new String(c.value, "UTF-8");
// 關閉數據庫鏈接
tr.close();
}
}

 

PHP 程序使用 Cassandra

在 PHP 代碼中使用 Cassandra,須要藉助 Thrift 來生成須要的 PHP 文件,經過使用 thrift --gen php interface/cassandra.thrift 生成所須要的 PHP 文件,生成的 PHP 文件中提供了與 Cassandra 創建鏈接、讀寫數據時所須要的函數。


清單 3. PHP 鏈接 Cassandra,寫入並讀取數據。

<?php
$GLOBALS['THRIFT_ROOT'] = '/usr/share/php/Thrift';
require_once
$GLOBALS['THRIFT_ROOT'].'/packages/cassandra/Cassandra.php';
require_once
$GLOBALS['THRIFT_ROOT'].'/packages/cassandra/cassandra_types.php';
require_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php';
require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php';
require_once
$GLOBALS['THRIFT_ROOT'].'/transport/TFramedTransport.php';
require_once
$GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php';
try {
// 創建 Cassandra 鏈接
$socket = new TSocket('192.168.10.2', 9160);
$transport = new TBufferedTransport($socket, 1024, 1024);
$protocol = new TBinaryProtocolAccelerated($transport);
$client = new CassandraClient($protocol);
$transport->open();
$keyspace = 'Keyspace1';
$keyUser = "studentA";
$columnPath = new cassandra_ColumnPath();
$columnPath->column_family = 'Standard1';
$columnPath->super_column = null;
$columnPath->column = 'age';
$consistency_level = cassandra_ConsistencyLevel::ZERO;
$timestamp = time();
$value = "18";
// 寫入數據
$client->insert($keyspace, $keyUser, $columnPath, $value,
$timestamp, $consistency_level);
$columnParent = new cassandra_ColumnParent();
$columnParent->column_family = "Standard1";
$columnParent->super_column = NULL;
$sliceRange = new cassandra_SliceRange();
$sliceRange->start = "";
$sliceRange->finish = "";
$predicate = new cassandra_SlicePredicate();
list() = $predicate->column_names;
$predicate->slice_range = $sliceRange;
$consistency_level = cassandra_ConsistencyLevel::ONE;
$keyUser = studentA;
// 查詢數據
$result = $client->get_slice($keyspace, $keyUser, $columnParent,
$predicate, $consistency_level);
// 關閉鏈接
$transport->close();
} catch (TException $tx) {
}?>

 

Python 程序使用 Cassandra

在 Python 中使用 Cassandra 須要 Thrift 來生成第三方 Python 庫,生成方式: thrift --gen py interface/cassandra.thrift, 而後在 Python 代碼中引入所需的 Python 庫,生成的 Python 庫提供了與 Cassandra 創建鏈接、讀寫數據時所須要的方法。


清單 4. Python 鏈接 Cassandra,寫入並讀取數據。

from thrift import Thrift
from thrift.transport import TTransport
from thrift.transport import TSocket
from thrift.protocol.TBinaryProtocol import
TBinaryProtocolAccelerated
from cassandra import Cassandra
from cassandra.ttypes import *
import time
import pprint
def main():
socket = TSocket.TSocket("192.168.10.2", 9160)
transport = TTransport.TBufferedTransport(socket)
protocol = TBinaryProtocol.TBinaryProtocolAccelerated(transport)
client = Cassandra.Client(protocol)
pp = pprint.PrettyPrinter(indent=2)
keyspace = "Keyspace1"
column_path = ColumnPath(column_family="Standard1", column="age")
key = "studentA"
value = "18 "
timestamp = time.time()
try:
# 打開數據庫鏈接
transport.open()
# 寫入數據
client.insert(keyspace,key,column_path,
value,timestamp,ConsistencyLevel.ZERO)
# 查詢數據
column_parent = ColumnParent(column_family="Standard1")
slice_range = SliceRange(start="", finish="")
predicate = SlicePredicate(slice_range=slice_range)
result = client.get_slice(keyspace,key,column_parent,
predicate,ConsistencyLevel.ONE)
pp.pprint(result)
except Thrift.TException, tx:
print 'Thrift: %s' % tx.message
finally:
# 關閉鏈接
transport.close()
if __name__ == '__main__':
main()
C# 使用 Cassandra

在 C# 中使用 Cassandra 須要 Thrift.exe 來生成動態連接庫,使用 ./thrift.exe --gen csharp interface/cassandra.thrift 生成所須要的 DLL 文件,生成的 DLL 提供了與 Cassandra 創建鏈接,讀寫數據等所需的類和方法,在編程環境中引入生成的 DLL,便可使用。

清單 5. C# 鏈接 Cassandra,寫入並讀取數據。

namespace CshareCassandra{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Apache.Cassandra;
using Thrift.Protocol;
using Thrift.Transport;
class CassandraClient{
static void Main(string[] args){
// 創建數據庫鏈接
TTransport transport = new TSocket("192.168.10.2", 9160);
TProtocol protocol = new TBinaryProtocol(transport);
Cassandra.Client client = new Cassandra.Client(protocol);
transport.Open();
System.Text.Encoding utf8Encoding = System.Text.Encoding.UTF8;
long timeStamp = DateTime.Now.Millisecond;
ColumnPath nameColumnPath = new ColumnPath(){
Column_family = "Standard1",
Column = utf8Encoding.GetBytes("age")};
// 寫入數據
client.insert("Keyspace1","studentA",nameColumnPath,
utf8Encoding.GetBytes("18"),timeStamp, ConsistencyLevel.ONE);
// 讀取數據
ColumnOrSuperColumn returnedColumn = client.get("Keyspace1",
"studentA", nameColumnPath, ConsistencyLevel.ONE);
Console.WriteLine("Keyspace1/Standard1: age: {0}, value: {1}",
utf8Encoding.GetString(returnedColumn.Column.Name),
utf8Encoding.GetString(returnedColumn.Column.Value));
// 關閉鏈接
transport.Close();
}
}}
Ruby 使用 Cassandra

在 Ruby 中使用 Cassandra 須要先安裝 gem,安裝命令:gem install cassandra

安裝完成後,打開 Ruby 的 irb 開始使用 Cassandra。


清單 6. Ruby 鏈接 Cassandra,寫入並讀取數據

> require 'rubygems'
> require 'cassandra'
# 創建數據庫鏈接
> cdb = Cassandra.new('Keyspace1',"192.168.10.1:9160", :retries => 3)
# 寫入數據
> cdb.insert(:Standard1, 'studentA', {'age' => '18'})
# 讀取數據
> cdb.get(:Standard1, :studentA)
# 關閉鏈接
> cdb.disconnect

搭建 Cassandra 集羣環境

Cassandra 的集羣是沒有中心節點的,各個節點的地位徹底相同,節點之間是經過 gossip 的協議來維護集羣的狀態。
如下是兩臺安裝了 Linux 系統的服務器,且初步設置了 Cassandra 環境和啓用了端口 7000,9160:
服務器名 端口 IP 地址
ServiceA 7000,9160 192.168.10.3
ServiceB 7000,9160 192.168.10.2
配製服務器 ServiceA、ServiceB 的 storage-conf.xml 文件

ServiceA 的配置

<Seeds>
<Seed>192.168.10.3</Seed>
</Seeds>
<ListenAddress>192.168.10.2</ListenAddress>
<ThriftAddress>0.0.0.0</ThriftAddress>

ServiceB 的配置

<Seeds>
<Seed>192.168.10.3</Seed>
<Seed>192.168.10.2</Seed>
</Seeds>
<ListenAddress>192.168.10.2</ListenAddress>
<ThriftAddress>0.0.0.0</ThriftAddress>

配製完成後,分別啓動 ServiceA 和 ServiceB 上的 Cassandra 服務。

查看 ServiceA 和 ServiceB 是否集羣成功,可以使用 Cassandra 自帶的客戶端命令

bin/nodetool --host 192.168.10.2 ring

集羣成功則會返回如下相似信息:

Address Status Load Range Ring
106218876142754404016344802054916108445
192.168.10.2 Up 2.55 KB 31730917190839729088079827277059909532 |<--|
192.168.10.3 Up 3.26 KB 106218876142754404016344802054916108445 |-->|

使用 Cassandra 命令行工具進行集羣測試

從 ServiceB 鏈接到 ServiceA,可以使用命令:

cassandra-cli -host 192.168.10.3 -port 9160

集羣測試一

寫入集羣數據

ServiceA 鏈接到 ServiceA:
# set Keyspace1.Standard2['studentAA']['A2A'] = 'a2a'

ServiceB 鏈接到 ServiceA:

# set Keyspace1.Standard2['studentBA']['B2A'] = 'b2a'

ServiceA 鏈接到 ServiceB:
# set Keyspace1.Standard2['studentAB']['A2B'] = 'a2b'

 

獲取集羣數據:

ServiceA 鏈接到 ServiceA :
# get Keyspace1.Standard2['studentAA'],
get Keyspace1.Standard2['studentBA'],
get Keyspace1.Standard2['studentAB']

ServiceB 鏈接到 ServiceA :
# get Keyspace1.Standard2['studentAA'],
get Keyspace1.Standard2['studentBA'],
get Keyspace1.Standard2['studentAB']

ServiceA 鏈接到 ServiceB :
# get Keyspace1.Standard2['studentAA'],
get Keyspace1.Standard2['studentBA'],
get Keyspace1.Standard2['studentAB']

清單 8. 集羣測試清單二

ServiceA 中止 Cassandra 服務,ServiceA 鏈接到 ServiceB 並寫入數據

# set Keyspace1.Standard2['studentAR']['A2R'] = 'a2R'

 

啓動 ServiceA,並連接到 ServiceA 自己,讀取剛纔在 ServiceB 寫入的數據

# bin/cassandra-cli -host 192.168.10.3 -port 9160
# get Keyspace1.Standard2['studentAR']

總結

以上咱們介紹了 Cassandra 的數據模型、節點安裝和配置、經常使用編程語言中使用 Cassandra 以及 Cassandra 的集羣和測試。Cassandra 是一個高性能的 P2P 去中心化的非關係型數據庫,能夠分佈式進行讀寫操做。在系統運行時能夠隨意的添加或刪降字段,是 SNS 應用的理想數據庫。

 

此文參考了:

1.百度百科

2.http://www.ibm.com/developerworks/cn/opensource/os-cn-cassandra/index.html?ca=drs

相關文章
相關標籤/搜索