日期:2014-05-16  浏览次数:20455 次

试用Cassandra,其写效率太差
Cassandra是由Facebook贡献的开源分布式数据库。其遵从NoSql理念,是结合了Dynamo与BigTable的产物。最近Twitter和Digg都将其数据库由MySql迁往Cassandra。看到其发展势头不错,我就下载下来,做了个测试。

测试环境:
   
    分别在两台机器上部署cassandra.这里说明下关键配置:
配置文件路径是%Cassandra_Home%\conf\storage-conf.xml
<Storage>
<!--两台机器的ClusterName必须相同,作为集群标识 -->
    <ClusterName>BurceServers</ClusterName>
  <AutoBootstrap>false</AutoBootstrap>

    <Keyspaces>
    <Keyspace Name="Keyspace1">
            <KeysCachedFraction>0.01</KeysCachedFraction>
            <ColumnFamily CompareWith="BytesType" Name="Standard1"/>
      <ColumnFamily CompareWith="UTF8Type" Name="Standard2"/>
      <ColumnFamily CompareWith="TimeUUIDType" Name="StandardByUUID1"/>
      <ColumnFamily ColumnType="Super"
                    CompareWith="UTF8Type"
                    CompareSubcolumnsWith="UTF8Type"
                    Name="Super1"
                    Comment="A column family with supercolumns, whose column and subcolumn names are UTF8 strings"/>
    </Keyspace>
  </Keyspaces>

    <Partitioner>org.apache.cassandra.dht.RandomPartitioner</Partitioner>

    <InitialToken></InitialToken>

  <EndPointSnitch>org.apache.cassandra.locator.EndPointSnitch</EndPointSnitch>

    <ReplicaPlacementStrategy>org.apache.cassandra.locator.RackUnawareStrategy</ReplicaPlacementStrategy>

    <ReplicationFactor>1</ReplicationFactor>

  <CommitLogDirectory>c:/cassandra/lib/cassandra/commitlog</CommitLogDirectory>
  <DataFileDirectories>
      <DataFileDirectory>c:/cassandra/lib/cassandra/data</DataFileDirectory>
  </DataFileDirectories>
  <CalloutLocation>c:/cassandra/lib/cassandra/callouts</CalloutLocation>
  <StagingFileDirectory>c:/cassandra/lib/cassandra/staging</StagingFileDirectory>

<!--在这里可以添加多个cassandra服务器-->
    <Seeds>
      <Seed>10.219.101.101</Seed>
<Seed>10.219.101.121</Seed>
  </Seeds>


    <RpcTimeoutInMillis>5000</RpcTimeoutInMillis>
    <CommitLogRotationThresholdInMB>128</CommitLogRotationThresholdInMB>

<!--监听地址必须是本机IP-->
    <ListenAddress>10.219.101.101</ListenAddress>
   <StoragePort>7000</StoragePort>
    <ControlPort>7001</ControlPort>
<!--基于Thrift的cassandra客户端监听地址-->
    <ThriftAddress>10.219.101.101</ThriftAddress>
    <ThriftPort>9160</ThriftPort>
    <ThriftFramedTransport>false</ThriftFramedTransport>


    <SlicedBufferSizeInKB>64</SlicedBufferSizeInKB>

   <ColumnIndexSizeInKB>64</ColumnIndexSizeInKB>

    <MemtableSizeInMB>64</MemtableSizeInMB>
  
  <MemtableObjectCountInMillions>0.1</MemtableObjectCountInMillions>
    <MemtableFlushAfterMinutes>60</MemtableFlushAfterMinutes>

    <ConcurrentReads>8</ConcurrentReads>
  <ConcurrentWrites>32</ConcurrentWrites>

    <CommitLogSync>periodic</CommitLogSync>
    <CommitLogSyncPeriodInMS>10000</CommitLogSyncPeriodInMS>
  <GCGraceSeconds>864000</GCGraceSeconds>
  <BinaryMemtableSizeInMB>256</BinaryMemtableSizeInMB>

</Storage>


除增加了一个cassandra的服务器外,基本采用默认配置。

测试代码:

/**
 * 
 */
package com.tpri.sis.test;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

import me.prettyprint.cassandra.service.CassandraClient;

import org.apache.cassandra.service.Cassandra;
import org.apache.cassandra.service.ColumnPath;
import org.apache.cassandra.service.ConsistencyLevel;
import org.apache.cassandra.service.InvalidRequestException;
import org.apache.cassandra.service.TimedOutException;
import org