TTからCassandraへのインポート

Cassandra-0.6にもsstable2json, json2sstableというimport/exportするコマンドがあるが、
これは内部用のものなので、他のDBからのインポート用にはそのまま使えない。

しかも実際使ってみたけど1G程度のJSONインポートでヒープが足らんと言われてしまった。。。

仕方ないのでThrift経由でデータを入れるPerlスクリプトを書く。
参考にしてください。

tchmgr list -pv > tt_data.tsv # TTからのTSVエクスポート
#!/usr/bin/perl

use strict;
use warnings;
use Net::Cassandra;
use Coro;
use Coro::Semaphore;   

$|=1;
open IN, $ARGV[0] or die "cant open" . $ARGV[0]; # tt_data.tsv
my @hosts = qw/127.0.0.1/; # hostを記述
my $keyspace = 'keyspace1';
my @clients;
push @clients, Net::Cassandra->new(hostname => $_)->client for @hosts;

my $coro_num = 2000;
my $count = 0;
my @coros;
my $semaphore = Coro::Semaphore->new($coro_num);
while(<IN>) {
  $count++;
  my $line = $_;
  push(@coros, async {
    chomp $line;
    my ($ttkey, $value) = split /\t/ , $line;
    my ($tt, $key, $column) = split /:/ , $ttkey;
    my $client = $clients[$count % scalar @clients];
    $semaphore->up;
    eval {
      $client->insert(
        $keyspace,
        $key,
        Net::Cassandra::Backend::ColumnPath->new(
          { column_family => 'COLUMNFAMILY', column => $column}
        ),
        $value,
        time,
        Net::Cassandra::Backend::ConsistencyLevel::ALL
      );
    };
    # print $key . "\t" . $column  .  "\t" . $value . "\n";
  });
  $semaphore->down;
} 

close(IN);