您当前的位置: 首页 > 

宝哥大数据

暂无认证

  • 2浏览

    0关注

    1029博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Pre-Creating Regions

宝哥大数据 发布时间:2018-05-31 09:30:17 ,浏览量:2

前言:默认情况下, HBase创建表时候会自动创建一个region,当插入数据的时候, 数据到会写到这一个region, 直到这个region足够大了才进行切分(split)。一种可以加快批量写入速度的方法是通过预先创建一些空的regions,这样当数据写入HBase时,会按照region分区情况,在集群内做数据的负载均衡。 1、创建分区
    /**
     * 60/2 30个分区 2分钟一个
     * 为什么创建的splitkeys 少一个。
     * 因为使用createTable(HTableDescriptor desc, byte[][] splitKeys) 创建预分区的table, 
     * regions的数目等于splitkeys+1
     * @see org.apache.hadoop.hbase.client.HBaseAdmin.createTable(HTableDescriptor desc, byte[][] splitKeys)
     * Creates a new table with an initial set of empty regions defined by the specified split keys. 
     * The total number of regions created will be the number of split keys plus one. Synchronous operation. 
     * Note : Avoid passing empty split key.
     * @return  byte[][] splitkeys
     */
    public static byte[][] getSplit() {

        byte[][] splits = new byte[29][];
        for (int i = 1; i < 30; i ++) {
            int vi = i * 2;
            String si = (vi < 10) ? ("0" + vi) : "" + vi;//补成两位
            splits[i - 1] = si.getBytes();
        }
        return splits;
    }
2、创建预分区表
//Admin有创建分区表方法
void org.apache.hadoop.hbase.client.HBaseAdmin.createTable(HTableDescriptor htd, byte[][] splits) throws IOException
创建表
    /**
     * hbase创建表
     * @param tableName   表名
     * @param cf                   列族
     * @param split       pre-creating region 表的设计优化, 预创建多个region,避免写数据到一个region上导致读写热点
     * 默认情况下,在创建HBase表的时候会自动创建一个region分区,
     * 当导入数据的时候,所有的HBase客户端都向这一个region写数据,直到这个region足够大了才进行切分
     * 
     */
    public static void create(String tableName, String cf, byte[][] split){
        //创建表管理
        HBaseAdmin admin = null;
        try {
            Configuration conf = HBaseConfiguration.create();
            admin = new HBaseAdmin(conf);
        } catch (Exception e) {
            e.printStackTrace();
        }
        HTableDescriptor htd = new HTableDescriptor(tableName);

        HColumnDescriptor hcd = new HColumnDescriptor(cf);
        hcd.setInMemory(true);
        //设置表中数据的存储生命期,过期数据将自动被删除,例如如果只需要存储最近两天的数据,那么可以设置setTimeToLive(2 * 24 * 60 * 60)。
        //hcd.setTimeToLive(timeToLive)//设置生命周期
        hcd.setMaxVersions(2);   //设置版本数
        htd.addFamily(hcd);
        //如果表不存在,创建表
        try {
            if(split != null){
                if(!admin.tableExists(htd.getName())){
                    admin.createTable(htd, split);
                }
            }else {//默认情况。不预创建region
                admin.createTable(htd);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
关注
打赏
1587549273
查看更多评论
立即登录/注册

微信扫码登录

0.0390s