我们的集群是双节点集群,机器配置比较垃圾,都是2个Linux VM.
具体配置为:
cpu信息:在 /proc/cpuinfo中
内存信息:在/proc/meminfo中
我们用最基本的Cassandra thrift API 来实现插入50万条记录到这个Cassandra集群的负载。为了公平起见,我们不去设置备份,也就是把repliaction因子设为1,这样它不需要额外的写副本的开销:
最终代码如下:
下面的类是工具类,因为在信息实际存在Cassandra内存中,所以这个工具类负责在内存和字符串之间转换:
- /*
- */
- package com.charles.cassandra.util;
- import java.io.UnsupportedEncodingException;
- import java.nio.ByteBuffer;
- /**
- *
- * Description: 这个类提供了一些Cassandra操作的工具类
- *
- * @author charles.wang
- * @created May 19, 2012 11:18:27 AM
- *
- */
- public class CassandraOperationUtil {
- /**
- *因为在Cassandra中,信息都存在内存的,所以都是以ByteBuffer形式存储的,但是ByteBuffer对于人类来说没有String可读性强
- *所以这个方法可以吧字符串转为ByteBuffer
- */
- public static ByteBuffer stringToByteBuffer(String s) throws UnsupportedEncodingException{
- return ByteBuffer.wrap(s.getBytes("UTF-8"));
- }
- /**
- *因为在Cassandra中,信息都存在内存的,所以都是以ByteBuffer形式存储的,但是ByteBuffer对于人类来说没有String可读性强
- *所以对称的,这个方法吧ByteBuffer转为人类可读的字符串
- */
- public static String byteBufferToString (ByteBuffer b) throws UnsupportedEncodingException{
- //先构建一个字节数组
- byte[] bytes = new byte[b.remaining()];
- //吧bytebuffer里面的内容全部存入字节数组
- b.get(bytes);
- //然后把这些bytes转为String
- return new String(bytes,"UTF-8");
- }
- }
下面是真正的压力测试类:
- package com.charles.cassandra.demo;
- import java.io.UnsupportedEncodingException;
- import java.nio.ByteBuffer;
- import java.util.List;
- import org.apache.cassandra.thrift.Cassandra;
- import org.apache.cassandra.thrift.Column;
- import org.apache.cassandra.thrift.ColumnOrSuperColumn;
- import org.apache.cassandra.thrift.ColumnParent;
- import org.apache.cassandra.thrift.ConsistencyLevel;
- import org.apache.cassandra.thrift.InvalidRequestException;
- import org.apache.cassandra.thrift.NotFoundException;
- import org.apache.cassandra.thrift.TBinaryProtocol;
- import org.apache.cassandra.thrift.TimedOutException;
- import org.apache.cassandra.thrift.UnavailableException;
- import org.apache.thrift.TException;
- 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 com.charles.cassandra.util.CassandraOperationUtil;
- public class CassandraClusterStressTest
- {
- public static void main(String[] args)
- throws TException, InvalidRequestException,
- UnavailableException, UnsupportedEncodingException,
- NotFoundException, TimedOutException
- {
- //包装好的socket
- TTransport tr = new TFramedTransport(new TSocket("192.168.129.34",9160));
- TProtocol proto = new TBinaryProtocol(tr);
- Cassandra.Client client = new Cassandra.Client(proto);
- tr.open();
- if(!tr.isOpen())
- {
- System.out.println("无法连接到服务器!");
- return;
- }
- System.out.println("开始压力测试,我们插入50W条数据到2节点集群中");
- System.out.println("...");
- //标记开始时间
- long startTime = System.currentTimeMillis();
- client.set_keyspace("Charles_Stress_Test");//使用Charles_Stress_Test keyspace
- ColumnParent parent = new ColumnParent("student");//column family
- /*
- * 这里我们插入50万条数据到Student内
- * 每条数据包括id和name
- */
- String key_user_id = "a";
- for(int i = 0;i < 500000;i++)
- {
- String k = key_user_id + i;//row key
- long timestamp = System.currentTimeMillis();//时间戳
- //每行的第一个字段(id字段)
- Column idColumn = new Column(CassandraOperationUtil.stringToByteBuffer("id"));//字段名
- idColumn.setValue(CassandraOperationUtil.stringToByteBuffer(i + ""));//字段值
- idColumn.setTimestamp(timestamp);//时间戳
- client.insert(
- CassandraOperationUtil.stringToByteBuffer(k),
- parent,
- idColumn,
- ConsistencyLevel.ONE);
- //每行的第二个字段(name字段)
- Column nameColumn = new Column(CassandraOperationUtil.stringToByteBuffer("name"));
- nameColumn.setValue(CassandraOperationUtil.stringToByteBuffer("student" + i));
- nameColumn.setTimestamp(timestamp);
- client.insert(
- CassandraOperationUtil.stringToByteBuffer(k),
- parent,
- nameColumn,
- ConsistencyLevel.ONE);
- }
- //标记结束时间
- long endTime = System.currentTimeMillis();
- //标记一共用时
- long elapsedTime = endTime-startTime;
- System.out.println("压力测试完毕,用时: "+elapsedTime+" 毫秒");
- //关闭连接
- tr.close();
- }
- }
最终压力测试执行完毕,结果如下:
我们验证下结果,我们打开opscenter->DATA EXPLORER,然后我们搜索最后一条记录,看是否存在:
所以我们相信这些数据已经存在在Cassandra 集群中了。
所以,我们看到,在低配置的机器上(Linux VM),就算插入50W条数据也需要672.5秒,也就是11分钟多,也不是很强大嘛!
等会我会对比在关系数据库Oracle中插入50W条记录,来对比下性能。
本文转自 charles_wang888 51CTO博客,原文链接:http://blog.51cto.com/supercharles888/869276,如需转载请自行联系原作者