1.關閉 SELinux及防火牆

setenforce 0
service iptables stop

2.下載jre-7u45-linux-x64安裝檔

wget http://uni-smr.ac.ru/archive/dev/java/JRE/7/JRE-7.45/jre-7u45-linux-x64.rpm

安裝jre

rpm -ivh jre-7u45-linux-x64.rpm

設定java執行路徑

export JAVA_HOME=/usr/java/jre1.7.0_45/

alternatives  --install  /usr/bin/java java  /usr/java/jre1.7.0_45/bin/java  100

alternatives --config java

(選擇3)

javalocal

從官網下載最新的安裝檔

wget http://ftp.kddilabs.jp/infosystems/apache/cassandra/2.1.2/apache-cassandra-2.1.2-bin.tar.gz

解壓縮至/usr/local底下

tar zxvf apache-cassandra-2.1.2-bin.tar.gz -C /usr/local

設定相關資料夾使用者權限

sudo mkdir -p /var/log/cassandra

sudo chown -R `whoami` /var/log/cassandra

sudo mkdir -p /var/lib/cassandra

sudo chown -R `whoami` /var/lib/cassandra

#啟動

/usr/local/apache-cassandra-2.1.2/bin/Cassandra

#檢查是否執行

ps aux | grep Cassandra

/usr/local/apache-cassandra-2.1.2/bin/nodetool status

/usr/local/apache-cassandra-2.1.2/bin/nodetool -host 127.0.0.1 -p 7199 status

#背景執行

/usr/local/apache-cassandra-2.1.2/bin/cassandra -f &

#交談命令介面 (有點類似 MySQL)

/usr/local/apache-cassandra-2.1.2/bin/cassandra-cli --host localhost

(實際測試 quit 和 exit 無法離開,用 Ctrl-C 才行)

交談模式提供的指令

[default@unknown] ?
List of all CLI commands:
?                                                          Display this message.
help;                                                         Display this help.
help <command>;                         Display detailed, command-specific help.
connect <hostname>/<port>;                            Connect to thrift service.
use <keyspace> [<username> 'password'];                    Switch to a keyspace.
describe keyspace <keyspacename>;                             Describe keyspace.
exit;                                                                  Exit CLI.
quit;                                                                  Exit CLI.
show cluster name;                                         Display cluster name.
show keyspaces;                                          Show list of keyspaces.
show api version;                                       Show server API version.
create keyspace <keyspace> [with <att1>=<value1> [and <att2>=<value2> ...]];
                Add a new keyspace with the specified attribute(s) and value(s).
update keyspace <keyspace> [with <att1>=<value1> [and <att2>=<value2> ...]];
                 Update a keyspace with the specified attribute(s) and value(s).
create column family <cf> [with <att1>=<value1> [and <att2>=<value2> ...]];
        Create a new column family with the specified attribute(s) and value(s).
update column family <cf> [with <att1>=<value1> [and <att2>=<value2> ...]];
            Update a column family with the specified attribute(s) and value(s).
drop keyspace <keyspace>;                                     Delete a keyspace.
drop column family <cf>;                                 Delete a column family.
get <cf>['<key>'];                                       Get a slice of columns.
get <cf>['<key>']['<super>'];                        Get a slice of sub columns.
get <cf> where <column> = <value> [and <column> > <value> and ...] [limit int];
get <cf>['<key>']['<col>'] (as <type>)*;                     Get a column value.
get <cf>['<key>']['<super>']['<col>'] (as <type>)*;      Get a sub column value.
set <cf>['<key>']['<col>'] = <value> (with ttl = <secs>)*;         Set a column.
set <cf>['<key>']['<super>']['<col>'] = <value> (with ttl = <secs>)*;
                                                               Set a sub column.
del <cf>['<key>'];                                                Delete record.
del <cf>['<key>']['<col>'];                                       Delete column.
del <cf>['<key>']['<super>']['<col>'];                        Delete sub column.
count <cf>['<key>'];                                    Count columns in record.
count <cf>['<key>']['<super>'];                 Count columns in a super column.
truncate <column_family>;                      Truncate specified column family.
assume <column_family> <attribute> as <type>;
              Assume a given column family attributes to match a specified type.
list <cf>;                                   List all rows in the column family.
list <cf>[<startKey>:];
                       List rows in the column family beginning with <startKey>.
list <cf>[<startKey>:<endKey>];
        List rows in the column family in the range from <startKey> to <endKey>.
list ... limit N;                                   Limit the list results to N.

建立 keyspace (類似 MySQL 建立資料庫)

[default@unknown] create keyspace Keyspace1;
 INFO 01:06:35,433 switching in a fresh Memtable for Migrations at CommitLogContext(file='/var/lib/cassandra/commitlog/CommitLog-1290531909844.log', position=10921)
 INFO 01:06:35,433 Enqueuing flush of Memtable-Migrations@10216117(5950 bytes, 1 operations)
 INFO 01:06:35,433 switching in a fresh Memtable for Schema at CommitLogContext(file='/var/lib/cassandra/commitlog/CommitLog-1290531909844.log', position=10921)
 INFO 01:06:35,434 Enqueuing flush of Memtable-Schema@4254944(2194 bytes, 3 operations)
 INFO 01:06:35,434 Writing Memtable-Migrations@10216117(5950 bytes, 1 operations)
 INFO 01:06:35,807 Completed flushing /var/lib/cassandra/data/system/Migrations-e-1-Data.db (6083 bytes)
 INFO 01:06:35,807 Writing Memtable-Schema@4254944(2194 bytes, 3 operations)
 INFO 01:06:36,145 Completed flushing /var/lib/cassandra/data/system/Schema-e-1-Data.db (2482 bytes)
06e4de78-f724-11df-b552-e700f669bcfc

使用 keyspace (類似 MySQL 使用資料庫)

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

建立 column family (類似 MySQL 的資料表 table)

[default@Keyspace1] create column family Users with comparator=UTF8Type and default_validation_class=UTF8Type;
 INFO 01:08:23,416 switching in a fresh Memtable for Migrations at CommitLogContext(file='/var/lib/cassandra/commitlog/CommitLog-1290531909844.log', position=21688)
 INFO 01:08:23,416 Enqueuing flush of Memtable-Migrations@9102746(6240 bytes, 1 operations)
 INFO 01:08:23,417 Writing Memtable-Migrations@9102746(6240 bytes, 1 operations)
 INFO 01:08:23,647 switching in a fresh Memtable for Schema at CommitLogContext(file='/var/lib/cassandra/commitlog/CommitLog-1290531909844.log', position=21688)
 INFO 01:08:23,648 Enqueuing flush of Memtable-Schema@8529229(2366 bytes, 3 operations)
 INFO 01:08:23,674 Completed flushing /var/lib/cassandra/data/system/Migrations-e-2-Data.db (6373 bytes)
 INFO 01:08:23,675 Writing Memtable-Schema@8529229(2366 bytes, 3 operations)
 INFO 01:08:23,965 Completed flushing /var/lib/cassandra/data/system/Schema-e-2-Data.db (2654 bytes)
47744619-f724-11df-b552-e700f669bcfc

插入資料

[default@Keyspace1] set Users[jsmith][first] = 'John';
Value inserted.

[default@Keyspace1] set Users[jsmith][last] = 'Smith';
Value inserted.

[default@Keyspace1] set Users[jsmith][age] = long(42);
Value inserted.

顯示資料

[default@Keyspace1] get Users[jsmith];
=> (column=age, value=42, timestamp=1290532160984000)
=> (column=first, value=John, timestamp=1290532150815000)
=> (column=last, value=Smith, timestamp=1290532156655000)
Returned 3 results.

再測試

[default@Keyspace1] set Users[jsmith2][age] = long(42);
Value inserted.

[default@Keyspace1] get Users[jsmith2];
=> (column=age, value=42, timestamp=1290533242531000)
Returned 1 results.

[default@Keyspace1] get Users;
Command not found: `get Users;`. Type 'help' or '?' for help.

[default@Keyspace1] get Users[jsmith2][age];
=> (column=age, value=42, timestamp=1290533242531000)

資料結構

  set Users[jsmith][first] = 'John';
        \      \        \          \
         \      \_ key   \          \_ value
          \               \_ column
           \_ column family

看一下 Keyspace1 空間說明

[default@unknown] describe keyspace keyspace1;
Keyspace: Keyspace1:
  Replication Strategy: org.apache.cassandra.locator.SimpleStrategy
    Replication Factor: 1
  Column Families:
    ColumnFamily: Users
      Columns sorted by: org.apache.cassandra.db.marshal.UTF8Type
      Row cache size / save period: 0.0/0
      Key cache size / save period: 200000.0/3600
      Memtable thresholds: 0.14531249999999998/31/60
      GC grace seconds: 864000
      Compaction min/max thresholds: 4/32
      Read repair chance: 1.0
      Built indexes: []

看一下 Cluster Name

[default@unknown] show cluster name;
Test Cluster

看一下版本

[default@unknown] show api version;
19.4.0

顯示所有 keyspaces

[default@unknown] show keyspaces;
Keyspace: Keyspace1:
  Replication Strategy: org.apache.cassandra.locator.SimpleStrategy
    Replication Factor: 1
  Column Families:
    ColumnFamily: Users
      Columns sorted by: org.apache.cassandra.db.marshal.UTF8Type
      Row cache size / save period: 0.0/0
      Key cache size / save period: 200000.0/3600
      Memtable thresholds: 0.14531249999999998/31/60
      GC grace seconds: 864000
      Compaction min/max thresholds: 4/32
      Read repair chance: 1.0
      Built indexes: []
Keyspace: system:
  Replication Strategy: org.apache.cassandra.locator.LocalStrategy
    Replication Factor: 1
  Column Families:
    ColumnFamily: HintsColumnFamily (Super)
    "hinted handoff data"
      Columns sorted by: org.apache.cassandra.db.marshal.BytesType/org.apache.cassandra.db.marshal.BytesType
      Row cache size / save period: 0.0/0
      Key cache size / save period: 0.01/3600
      Memtable thresholds: 0.14531249999999998/31/60
      GC grace seconds: 0
      Compaction min/max thresholds: 4/32
      Read repair chance: 0.0
      Built indexes: []
    ColumnFamily: IndexInfo
    "indexes that have been completed"
      Columns sorted by: org.apache.cassandra.db.marshal.UTF8Type
      Row cache size / save period: 0.0/0
      Key cache size / save period: 0.01/3600
      Memtable thresholds: 0.14531249999999998/31/60
      GC grace seconds: 0
      Compaction min/max thresholds: 4/32
      Read repair chance: 0.0
      Built indexes: []
    ColumnFamily: LocationInfo
    "persistent metadata for the local node"
      Columns sorted by: org.apache.cassandra.db.marshal.BytesType
      Row cache size / save period: 0.0/0
      Key cache size / save period: 0.01/3600
      Memtable thresholds: 0.14531249999999998/31/60
      GC grace seconds: 0
      Compaction min/max thresholds: 4/32
      Read repair chance: 0.0
      Built indexes: []
    ColumnFamily: Migrations
    "individual schema mutations"
      Columns sorted by: org.apache.cassandra.db.marshal.TimeUUIDType
      Row cache size / save period: 0.0/0
      Key cache size / save period: 0.01/3600
      Memtable thresholds: 0.14531249999999998/31/60
      GC grace seconds: 0
      Compaction min/max thresholds: 4/32
      Read repair chance: 0.0
      Built indexes: []
    ColumnFamily: Schema
    "current state of the schema"
      Columns sorted by: org.apache.cassandra.db.marshal.UTF8Type
      Row cache size / save period: 0.0/0
      Key cache size / save period: 0.01/3600
      Memtable thresholds: 0.14531249999999998/31/60
      GC grace seconds: 0
      Compaction min/max thresholds: 4/32
      Read repair chance: 0.0
      Built indexes: []

參考來源:

[研究] Apache Cassandra 2.0.3 安裝 (CentOS 6.5 x64)
http://shaurong.blogspot.tw/2013/12/apache-cassandra-203-centos-65-x64.html

[研究] Cassandra 1.2.8 Cluster 架設 (CentOS 6.4 x64)
http://shaurong.blogspot.com/2013/07/cassandra-128-cluster-centos-64-x64.html
http://forum.icst.org.tw/phpbb/viewtopic.php?t=80053

[研究] Cassandra 0.7.0 Cluster 架設 (CentOS 5.5x86/Fedora 14x86)
http://forum.icst.org.tw/phpbb/viewtopic.php?t=19351

[研究][問題] Cassandra + PHP + thrift 安裝(Fedora 14 x86)
http://forum.icst.org.tw/phpbb/viewtopic.php?t=19333

[研究][問題] Cassandra + PHP + thrift 安裝(CentOS 5.5 x86)
http://forum.icst.org.tw/phpbb/viewtopic.php?t=19332

[研究] Apache Cassandra 0.7.0 (NoSQL)卡珊多拉資料庫 安裝與使用
http://forum.icst.org.tw/phpbb/viewtopic.php?t=19309

By tony

自由軟體愛好者~喜歡不斷的思考各種問題,有新的事物都會想去學習嘗試 做實驗並熱衷研究 沒有所謂頂天的技術 只有謙虛及不斷的學習 精進專業,本站主要以分享系統及網路相關知識、資源而建立。 Github http://stnet253.github.io

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料