测试环境:
分别在两台机器上部署cassandra.这里说明下关键配置:
配置文件路径是%Cassandra_Home%\conf\storage-conf.xml
BurceServers false 0.01 org.apache.cassandra.dht.RandomPartitioner org.apache.cassandra.locator.EndPointSnitch org.apache.cassandra.locator.RackUnawareStrategy 1 c:/cassandra/lib/cassandra/commitlog c:/cassandra/lib/cassandra/data c:/cassandra/lib/cassandra/callouts c:/cassandra/lib/cassandra/staging 10.219.101.101 10.219.101.121 5000 128 10.219.101.101 7000 7001 10.219.101.101 9160 false 64 64 64 0.1 60 8 32 periodic 10000 864000 256
除增加了一个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.apache.cassandra.service.UnavailableException; import org.apache.commons.lang.RandomStringUtils; import org.apache.commons.lang.StringUtils; import org.apache.thrift.TException; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.protocol.TProtocol; import org.apache.thrift.transport.TSocket; import org.apache.thrift.transport.TTransport; import org.apache.thrift.transport.TTransportException; /** * @author brucepang * */ public class CassandraClientDemo { /** * */ public CassandraClientDemo() { } /** * @param args */ public static void main(String[] args) { try { TTransport tr = new TSocket("10.219.101.101", 9160); TProtocol pro = new TBinaryProtocol(tr); Cassandra.Client cli = new Cassandra.Client(pro); tr.open(); String key = null; String name, age; ColumnPath namePath = new ColumnPath("Standard1", null, "name" .getBytes("UTF-8")); ColumnPath agePath = new ColumnPath("Standard1", null, "age" .getBytes("UTF-8")); String keySpace = "Keyspace1"; long time = 0; long l1 = System.currentTimeMillis(); for (int i = 0; i < 100; i++) { key = String.valueOf(i); name = RandomStringUtils.random(5,"abcdefghefsdf"); time = System.currentTimeMillis(); cli.insert(keySpace, key, namePath, name.getBytes("UTF-8"), time, ConsistencyLevel.ONE); cli.insert(keySpace, key, agePath, key.getBytes("UTF-8"), time, ConsistencyLevel.ONE); } long l2 = System.currentTimeMillis(); long ch = l2 - l1; System.out.println(ch); } catch (TTransportException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvalidRequestException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnavailableException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TimedOutException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
测试结果:1 写100条数据,耗时59922毫秒,将近1分钟;