HBase 1.1.2 优化插入 Region预分配

预分Region 与 不预分Region 的测试
1 不预分Region:
      23~29秒插入100W数据   并且蛋疼的是每次都写入一个 RegionServer 且  只在一个 Region 相当于人为制造的网络风暴。
2  预分Region(3节点分了3个Region)
     写入 18~19秒 100W 数据。  55555 每秒。(本机网络请求已满。否则应该线性增长

下面是代码; 
(注释部分为不分 Region 的情况)
  1. package com.rocky.util;
  2. import com.rocky.dao.HBaseFactory;
  3. import org.apache.hadoop.conf.Configuration;
  4. import org.apache.hadoop.hbase.*;
  5. import org.apache.hadoop.hbase.client.*;
  6. import org.apache.hadoop.hbase.util.Bytes;
  7. import java.io.IOException;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.Random;
  11. import java.util.UUID;
  12. /**
  13. * Created by rocky_24 on 2016/2/25.
  14. */
  15. public class putData {
  16. public static void main(String[] args) throws IOException {
  17. // /**
  18. // * 分16
  19. // */
  20. // byte[][] preforkRegions = new byte[10+6][];
  21. // int k=0;
  22. // System.out.println(preforkRegions.length);
  23. // for(char i='0';i<='9';i++){
  24. // preforkRegions[k++]=(""+i).getBytes();
  25. // }
  26. // for(char i='a';i<='f';i++){
  27. // preforkRegions[k++]=(""+i).getBytes();
  28. // }
  29. byte[][] preforkRegions = new byte[3][];
  30. preforkRegions [0] = ("5").getBytes();
  31. preforkRegions [1] = ("10").getBytes();
  32. preforkRegions [2] = ("z").getBytes();
  33. createTable("access_logs","f",preforkRegions);
  34. System.out.println(TimeUtils.getIntradayDateAndTime());
  35. String [] pages = {"/","/a.html","/b.html","/c.html"};
  36. Configuration con = HBaseFactory.getConf();
  37. HTable table = new HTable(con,"access_logs");
  38. // Table table = HBaseFactory.getHBaseConnection().getTable(TableName.valueOf("access_logs"));
  39. table.setWriteBufferSize(1024 * 1024 * 6);
  40. table.setAutoFlushTo(false);
  41. int totalRecords = 10000;
  42. Random rand = new Random();
  43. System.out.println("importing " + totalRecords + " records ....");
  44. List<Put> list = new ArrayList<Put>();
  45. for (int i=0; i < totalRecords * 100; i++) {
  46. String rowkey = UUID.randomUUID().toString();
  47. String randomPage = pages[rand.nextInt(pages.length)];
  48. Put put = new Put(rowkey.getBytes());
  49. put.addColumn(Bytes.toBytes("f"), null, Bytes.toBytes(randomPage));
  50. list.add(put);
  51. }
  52. System.out.println("put数据装载完毕:"+list.size());
  53. System.out.println(TimeUtils.getIntradayDateAndTime());
  54. table.put(list);
  55. table.flushCommits();
  56. table.close();
  57. System.out.println("done");
  58. System.out.println(TimeUtils.getIntradayDateAndTime());
  59. }
  60. /**
  61. * 创建表
  62. * @param tableName
  63. * @param columnFamily
  64. * @param spilts
  65. * @throws IOException
  66. */
  67. public static void createTable(String tableName, String columnFamily, byte[][] spilts) throws IOException {
  68. Connection connection = HBaseFactory.getHBaseConnection();
  69. Admin admin = connection.getAdmin();
  70. if (admin.tableExists(TableName.valueOf(tableName))) {
  71. admin.disableTable(TableName.valueOf(tableName));
  72. admin.deleteTable(TableName.valueOf(tableName));
  73. }
  74. HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(tableName));
  75. tableDesc.addFamily(new HColumnDescriptor(columnFamily));
  76. if (spilts == null) {
  77. admin.createTable(tableDesc);
  78. } else {
  79. admin.createTable(tableDesc, spilts);
  80. }
  81. admin.close();
  82. }
  83. }

观察 16010端口 查看 Region 分裂情况如下:

   = =! 多分了一个 Region 因为HBase 在分裂创建时候是拿到
asscii 码的 0 到 数字[10]





posted @ 2016-03-01 11:32  rocky_24  阅读(1147)  评论(2编辑  收藏  举报
希望祖国繁荣,富强! God has given me a gift. Only one. I am the most complete fighter in the world. My whole life, I have trained. I must prove I am worthy of someting. rocky_24