Apache Cassandra 0.7.0 インストール・設定・起動・Javaからインサート(備忘録)

インストール

$ mkdir -p ~/apache/cassandra
$ cd ~/apache/cassandra/
$ wget http://www.meisei-u.ac.jp/mirror/apache/dist//cassandra/0.7.0/apache-cassandra-0.7.0-bin.tar.gz
$ tar xvzf apache-cassandra-0.7.0-bin.tar.gz
$ mv apache-cassandra-0.7.0/ 0.7.0

設定

CASSANDRA_HOME
$ vi /home/ktakeda47/.bashrc
export CASSANDRA_HOME=/home/ktakeda47/apache/cassandra/0.7.0
export CASSANDRA_CONF=$CASSANDRA_HOME/conf
export CASSANDRA_MAIN=org.apache.cassandra.thrift.CassandraDaemon
$ source ~/.bashrc
Java1.6が必要
$ java -version
java version "1.6.0_20"
各種ディレクトリの場所指定
$ cd 0.7.0/
$ vi conf/cassandra.yaml
# directories where Cassandra should store data on disk.
#data_file_directories:
#    - /var/lib/cassandra/data
data_file_directories:
    - /home/ktakeda47/apache/cassandra/0.7.0/data
# commit log
#commitlog_directory: /var/lib/cassandra/commitlog
commitlog_directory: /home/ktakeda47/apache/cassandra/0.7.0/commitlog
# saved caches
#saved_caches_directory: /var/lib/cassandra/saved_caches
saved_caches_directory: /home/ktakeda47/apache/cassandra/0.7.0/saved_caches
$ mkdir data
$ mkdir commitlog
$ mkdir saved_caches
log4jログファイルの出力先指定
$ vi conf/log4j-server.properties
#log4j.appender.R.File=/var/log/cassandra/system.log
log4j.appender.R.File=/home/ktakeda47/apache/cassandra/0.7.0/log/system.log
$ mkdir log
JMXのポート変更
$ vi conf/cassandra-env.sh
# Specifies the default port over which Cassandra will be available for
# JMX connections.
#JMX_PORT="8080"
JMX_PORT="9090"

起動

$ ./bin/cassandra -f
cliでアクセス
$ ./bin/cassandra-cli
Welcome to cassandra CLI.
Type 'help;' or '?' for help. Type 'quit;' or 'exit;' to quit.
[default@unknown] 
[default@unknown] connect localhost/9160;
Connected to: "Test Cluster" on localhost/9160
キースペース作成
[default@unknown] create keyspace Sample1;
[default@unknown] use Sample1;
Authenticated to keyspace: Sample1
カラムファミリー作成
[default@Sample1] create column family Standard1; 
[default@Sample1] create column family Super1 with COLUMN_TYPE=Super and COMPARATOR=BytesType and SUBCOMPARATOR=BytesType;
[default@Sample1] create column family utf8Order with COMPARATOR=UTF8Type;
[default@Sample1] create column family time_utf8Order with COLUMN_TYPE=Super and COMPARATOR=LongType and SUBCOMPARATOR=UTF8Type;
データモデル確認
[default@Sample1] show keyspaces;
       WARNING: Could not connect to the JMX on localhost:8080, information won't be shown.
 Keyspace: Sample1:
  Replication Strategy: org.apache.cassandra.locator.SimpleStrategy
    Replication Factor: 1
  Column Families:
    ColumnFamily: Standard1
      Columns sorted by: org.apache.cassandra.db.marshal.BytesType
      Row cache size / save period: 0.0/0
      Key cache size / save period: 200000.0/3600
      Memtable thresholds: 0.4359375/93/60
      GC grace seconds: 864000
      Compaction min/max thresholds: 4/32
      Read repair chance: 1.0
    ColumnFamily: Super1 (Super)
      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: 200000.0/3600
      Memtable thresholds: 0.4359375/93/60
      GC grace seconds: 864000
      Compaction min/max thresholds: 4/32
      Read repair chance: 1.0
    ColumnFamily: time_utf8Order (Super)
      Columns sorted by: org.apache.cassandra.db.marshal.LongType/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.4359375/93/60
      GC grace seconds: 864000
      Compaction min/max thresholds: 4/32
      Read repair chance: 1.0
    ColumnFamily: utf8Order
      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.4359375/93/60
      GC grace seconds: 864000
      Compaction min/max thresholds: 4/32
      Read repair chance: 1.0
なんかwarn出てますよorz

Javaからアクセス

package sample;

import java.nio.ByteBuffer;

import org.apache.cassandra.thrift.Cassandra.Client;
import org.apache.cassandra.thrift.Column;
import org.apache.cassandra.thrift.ColumnParent;
import org.apache.cassandra.thrift.ConsistencyLevel;
import org.apache.cassandra.thrift.InvalidRequestException;
import org.apache.cassandra.thrift.TimedOutException;
import org.apache.cassandra.thrift.UnavailableException;
import org.apache.cassandra.utils.ByteBufferUtil;
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 org.apache.thrift.transport.TTransportException;

public class SimpleInsert {

 public SimpleInsert() {
 }

 public static final void main(String[] args) {
  TTransport tTransport = new TFramedTransport(new TSocket("localhost", 9160));
  TProtocol tProtocol = new TBinaryProtocol(tTransport);
  try {
   tTransport.open();
  } catch (TTransportException e) {
   throw new RuntimeException(e);
  }
  
  Client client = new Client(tProtocol);
  try {
   client.set_keyspace("Sample1");
   ByteBuffer key = ByteBufferUtil.bytes("sample1");
   ColumnParent columnParent = new ColumnParent("Standard1")
    .setSuper_column((ByteBuffer) null);
   Column column = new Column(
     ByteBufferUtil.bytes("hoge"),
     ByteBufferUtil.bytes("sample_value"),
     System.currentTimeMillis());
   client.insert(key, columnParent, column, ConsistencyLevel.ONE);
   tTransport.flush();
  } catch (TTransportException e) {
   throw new RuntimeException(e);
  } catch (InvalidRequestException e) {
   throw new RuntimeException(e);
  } catch (UnavailableException e) {
   throw new RuntimeException(e);
  } catch (TimedOutException e) {
   throw new RuntimeException(e);
  } catch (TException e) {
   throw new RuntimeException(e);
  } finally {
   tTransport.close();
  }
 }

}
確認
$ ./bin/cassandra_cli
なんか入ってる?
[default@Sample1] connect localhost/9160;
[default@Sample1] use Sample1;
[default@Sample1] get Standard1['sample1'];
       => (column=686f6765, value=73616d706c655f76616c7565, timestamp=1296460094273)Returned 1 results.
see also: http://gihyo.jp/dev/serial/01/cassandra/0001
see also: http://d.hatena.ne.jp/amanar/20110109/1294570753

0 件のコメント:

コメントを投稿