HBase 1.1.2 优化插入 Region预分配

本文介绍了一种使用Java API向HBase批量插入大量记录的方法,并展示了如何预先创建分区以优化数据分布。通过实例代码详细解释了从创建表、设置预分区到执行批量插入的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


 

 
  秒插入   并且蛋疼的是相当于人为制造的网络风暴。

 

 

 

   
  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 ]

 





转载于:https://www.cnblogs.com/rocky24/p/7aaebae69fcabb134be725d57378d581.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值