手机版
你好,游客 登录 注册
背景:
阅读新闻

用JFreeChart 来分析Cassandra/Oracle插入海量数据的性能

[日期:2012-05-24] 来源:Linux社区  作者:supercharles [字体:大 中 小]

当然了,因为Cassandra的存储是基于内存的,所以我们定义了一个工具类用于转换字符串和字节数组:

  1. /*  
  2.  */ 
  3. package com.charles.cassandra.util; 
  4.  
  5. import Java.io.UnsupportedEncodingException; 
  6. import java.nio.ByteBuffer; 
  7.  
  8. /** 
  9.  * 
  10.  * Description: 这个类提供了一些Cassandra操作的工具类 
  11.  * 
  12.  * @author charles.wang 
  13.  * @created May 19, 2012 11:18:27 AM 
  14.  *  
  15.  */ 
  16. public class CassandraOperationUtil { 
  17.      
  18.     /** 
  19.      *因为在Cassandra中,信息都存在内存的,所以都是以ByteBuffer形式存储的,但是ByteBuffer对于人类来说没有String可读性强 
  20.      *所以这个方法可以吧字符串转为ByteBuffer 
  21.      */ 
  22.     public static ByteBuffer stringToByteBuffer(String s) throws UnsupportedEncodingException{ 
  23.          
  24.         return ByteBuffer.wrap(s.getBytes("UTF-8")); 
  25.     } 
  26.      
  27.     /** 
  28.      *因为在Cassandra中,信息都存在内存的,所以都是以ByteBuffer形式存储的,但是ByteBuffer对于人类来说没有String可读性强 
  29.      *所以对称的,这个方法吧ByteBuffer转为人类可读的字符串 
  30.      */ 
  31.     public static String byteBufferToString (ByteBuffer b) throws UnsupportedEncodingException{ 
  32.          
  33.         //先构建一个字节数组 
  34.         byte[] bytes = new byte[b.remaining()]; 
  35.         //吧bytebuffer里面的内容全部存入字节数组 
  36.         b.get(bytes); 
  37.         //然后把这些bytes转为String 
  38.         return new String(bytes,"UTF-8"); 
  39.     } 
  40.  
  41. } 

Oracle的采样:

我们这里依然用循环插入50W条记录,不同的是,在循环的开始和循环每10000条记录时,我们把时间戳记录在List中,最终把这个List写入文本文件(oracle_input_sample_data.txt):

  1. /*  
  2.  */ 
  3. package com.charles.cassandra.demo; 
  4.  
  5.  
  6. import java.io.File; 
  7. import java.io.FileWriter; 
  8. import java.sql.Connection; 
  9. import java.sql.Date; 
  10. import java.sql.DriverManager; 
  11. import java.sql.PreparedStatement; 
  12. import java.sql.ResultSet; 
  13. import java.sql.Statement; 
  14. import java.sql.ResultSetMetaData; 
  15. import java.sql.Timestamp; 
  16. import java.util.ArrayList; 
  17. import java.util.List; 
  18.  
  19.  
  20. /** 
  21.  * 
  22.  * Description:插入50W条记录到关系数据库Oracle中 
  23.  * 
  24.  * @author charles.wang 
  25.  * @created May 19, 2012 5:25:36 PM 
  26.  *  
  27.  */ 
  28.  
  29. public class OracleStressTest { 
  30.  
  31.   
  32.      
  33.      
  34.      
  35.     /** 
  36.      * 既然要测负载,就尽可能减少方法调用的时间开销,所以我用了最原始的写法 
  37.      * @param args 
  38.      */ 
  39.      
  40.     public static void main(String[] args){ 
  41.          
  42.         String url="jdbc:oracle:thin:@192.168.129.14:15210:ora11g"; 
  43.         String username="Charles_Stress_Test1"; 
  44.         String password="Charles_Stress_Test1"; 
  45.          
  46.         String sDBDriver = "oracle.jdbc.driver.OracleDriver"; 
  47.  
  48.         try{ 
  49.              
  50.             System.out.println("开始压力测试,我们以预编译的方式插入50W条数据到Oracle中"); 
  51.             System.out.println("..."); 
  52.             //标记开始时间 
  53.             long startTime=System.currentTimeMillis(); 
  54.                      
  55.                      
  56.             Class.forName(sDBDriver).newInstance(); 
  57.             Connection conn = DriverManager.getConnection(url,username,password); 
  58.              
  59.             //因为这里使用预编译语句,所以不用每次都生成新的执行计划 
  60.              
  61.             String rowkey=null; 
  62.             String id=null; 
  63.             String name=null; 
  64.             Date date=null; 
  65.             String statementString="insert into Student (rowkey,id,name,create_date )values(?,?,?,?)";; 
  66.             
  67.             PreparedStatement pstmt = conn.prepareStatement(statementString); 
  68.              
  69.             //这个sampleData代表了每插入1W条记录到Oracle数据库用时毫秒的数据样本 
  70.             List<Integer> sampleData = new ArrayList<Integer>(51); 
  71.              
  72.             for(int i=0;i<500000;i++){ 
  73.                  
  74.                 long timestamp = System.currentTimeMillis(); 
  75.                 rowkey="a"+i; 
  76.                 id=""+i; 
  77.                 name="student"+i; 
  78.                 date= new Date(timestamp); 
  79.                   
  80.                 pstmt.setString(1,rowkey); 
  81.                 pstmt.setString(2, id); 
  82.                 pstmt.setString(3,name); 
  83.                 pstmt.setDate(4, date); 
  84.                 pstmt.execute(); 
  85.                  
  86.                 //判断是否这是起始记录(用于标记起始时间戳)和第N 万条记录(第N万条记录的时间戳) 
  87.                 if( (i==0) || ( (i+1)%10000==0)){ 
  88.                     sampleData.add((int)(timestamp)); 
  89.                 } 
  90.             } 
  91.              
  92.             //关闭相关连接 
  93.             pstmt.close(); 
  94.             conn.close(); 
  95.              
  96.             long endTime=System.currentTimeMillis(); 
  97.             long elapsedTime=endTime-startTime; 
  98.              
  99.             System.out.println("压力测试完毕,用时: "+elapsedTime+" 毫秒"); 
  100.              
  101.             //在压力测试结束之后,我们来把样本数据写入文本文件中 
  102.             FileWriter fw = new FileWriter(new File("oracle_insert_sample_data.txt")); 
  103.             for(int j=0;j<sampleData.size();j++){ 
  104.                 fw.write(sampleData.get(j)+"\n"); 
  105.             } 
  106.             fw.flush(); 
  107.             fw.close(); 
  108.              
  109.         }catch(Exception e){ 
  110.             System.out.println("数据库连接失败"); 
  111.             e.printStackTrace(); 
  112.         } 
  113.          
  114.         
  115.         
  116.     } 
  117.      
  118.  
  119. } 

 

最终50W条记录插入完毕:控制台显示:

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

 

linux
相关资讯       Cassandra 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款