Cassandra 插入数据压力测试(2)

下面是真正的压力测试类:

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();     }          

最终压力测试执行完毕,结果如下:

Cassandra 插入数据压力测试

我们验证下结果,我们打开opscenter->DATA EXPLORER,然后我们搜索最后一条记录,看是否存在:

Cassandra 插入数据压力测试

所以我们相信这些数据已经存在在Cassandra 集群中了。

所以,我们看到,在低配置的机器上(Linux VM),就算插入50W条数据也需要672.5秒,也就是11分钟多,也不是很强大嘛! 

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/59c1ecc686e2406a845ceb42702e7dae.html