当然了,因为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");
- }
- }
Oracle的采样:
我们这里依然用循环插入50W条记录,不同的是,在循环的开始和循环每10000条记录时,我们把时间戳记录在List中,最终把这个List写入文本文件(oracle_input_sample_data.txt):
- /*
- */
- package com.charles.cassandra.demo;
- import java.io.File;
- import java.io.FileWriter;
- import java.sql.Connection;
- import java.sql.Date;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.Statement;
- import java.sql.ResultSetMetaData;
- import java.sql.Timestamp;
- import java.util.ArrayList;
- import java.util.List;
- /**
- *
- * Description:插入50W条记录到关系数据库Oracle中
- *
- * @author charles.wang
- * @created May 19, 2012 5:25:36 PM
- *
- */
- public class OracleStressTest {
- /**
- * 既然要测负载,就尽可能减少方法调用的时间开销,所以我用了最原始的写法
- * @param args
- */
- public static void main(String[] args){
- String url="jdbc:oracle:thin:@192.168.129.14:15210:ora11g";
- String username="Charles_Stress_Test1";
- String password="Charles_Stress_Test1";
- String sDBDriver = "oracle.jdbc.driver.OracleDriver";
- try{
- System.out.println("开始压力测试,我们以预编译的方式插入50W条数据到Oracle中");
- System.out.println("...");
- //标记开始时间
- long startTime=System.currentTimeMillis();
- Class.forName(sDBDriver).newInstance();
- Connection conn = DriverManager.getConnection(url,username,password);
- //因为这里使用预编译语句,所以不用每次都生成新的执行计划
- String rowkey=null;
- String id=null;
- String name=null;
- Date date=null;
- String statementString="insert into Student (rowkey,id,name,create_date )values(?,?,?,?)";;
- PreparedStatement pstmt = conn.prepareStatement(statementString);
- //这个sampleData代表了每插入1W条记录到Oracle数据库用时毫秒的数据样本
- List<Integer> sampleData = new ArrayList<Integer>(51);
- for(int i=0;i<500000;i++){
- long timestamp = System.currentTimeMillis();
- rowkey="a"+i;
- id=""+i;
- name="student"+i;
- date= new Date(timestamp);
- pstmt.setString(1,rowkey);
- pstmt.setString(2, id);
- pstmt.setString(3,name);
- pstmt.setDate(4, date);
- pstmt.execute();
- //判断是否这是起始记录(用于标记起始时间戳)和第N 万条记录(第N万条记录的时间戳)
- if( (i==0) || ( (i+1)%10000==0)){
- sampleData.add((int)(timestamp));
- }
- }
- //关闭相关连接
- pstmt.close();
- conn.close();
- long endTime=System.currentTimeMillis();
- long elapsedTime=endTime-startTime;
- System.out.println("压力测试完毕,用时: "+elapsedTime+" 毫秒");
- //在压力测试结束之后,我们来把样本数据写入文本文件中
- FileWriter fw = new FileWriter(new File("oracle_insert_sample_data.txt"));
- for(int j=0;j<sampleData.size();j++){
- fw.write(sampleData.get(j)+"\n");
- }
- fw.flush();
- fw.close();
- }catch(Exception e){
- System.out.println("数据库连接失败");
- e.printStackTrace();
- }
- }
- }
最终50W条记录插入完毕:控制台显示:

而且我们打开文本文件确定这些时间戳的样本都被记录了:

