博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(四)region代码实现
阅读量:4343 次
发布时间:2019-06-07

本文共 3830 字,大约阅读时间需要 12 分钟。

1.  在创建表时,直接分区,split分为10个区,插入100条数据,每个分区十条数据

import java.io.IOException;import java.math.BigInteger;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.HColumnDescriptor;import org.apache.hadoop.hbase.HTableDescriptor;import org.apache.hadoop.hbase.client.HBaseAdmin;import org.apache.hadoop.hbase.client.HTable;import org.apache.hadoop.hbase.client.Put;import org.apache.hadoop.hbase.util.RegionSplitter.SplitAlgorithm;import net.spy.memcached.compat.log.Logger;import net.spy.memcached.compat.log.LoggerFactory;public class RegionPair  implements SplitAlgorithm{//日志显示private static final Logger LOG = LoggerFactory.getLogger(RegionPair.class);/* * **************************实现预分区 */   public static void main(String[] args) throws IOException {       Configuration configuration = HBaseConfiguration.create();       configuration.set("hbase.zookeeper.quorum", "localhost");       HBaseAdmin hBaseAdmin = new HBaseAdmin(configuration);      String tablename="rt_user_tags_test";if (hBaseAdmin.tableExists(tablename)) {try {hBaseAdmin.disableTable(tablename);hBaseAdmin.deleteTable(tablename);} catch (Exception ex) {ex.printStackTrace();}}       //建表       HTableDescriptor tableDescriptor = new HTableDescriptor("rt_user_tags_test");       //添加列族到表中       tableDescriptor.addFamily(new HColumnDescriptor("tags"));       //拆分策略可重写byte[][] split,在SplitAlgorithm接口中       byte[][] splits = getHexSplits("0", "100", 10);       createTable(hBaseAdmin, tableDescriptor, splits);       for (int i = 0; i < 100; i++) {        if(i<10){        //插入数据insertData----------->表名+配置+主键+列族           insertData("rt_user_tags_test", configuration, (new Integer("000000000"+String.valueOf(i))).toHexString(new Integer("000000000"+String.valueOf(i))), "tags");        }else{           insertData("rt_user_tags_test", configuration, "00000000"+String.valueOf(i), "tags");        }       }   }              /*         * 建表         * 创建表格时         */   public static boolean createTable(HBaseAdmin hBaseAdmin, HTableDescriptor tableDescriptor, byte[][] splits) throws IOException {       try {           hBaseAdmin.createTable(tableDescriptor, splits);           return true;       } catch (Exception e) {           LOG.info("table" + tableDescriptor.getNameAsString() + "already exists!");           return false;       }   }        /*         * 设置startKey,endKey         */   public static byte[][] getHexSplits(String startKey, String endKey, int numRegions) {       //startKey:001, endKey:100, 10regions[001, 010], [011, 020],...       byte[][] splits = new byte[numRegions - 1][];       BigInteger lowestKey = new BigInteger(startKey, 10);       BigInteger highestKey = new BigInteger(endKey, 10);       BigInteger rangge = highestKey.subtract(lowestKey);       BigInteger regionIncrement = rangge.divide(BigInteger.valueOf(numRegions));       lowestKey = lowestKey.add(regionIncrement);       for (int i = 0; i < numRegions - 1; i++) {           BigInteger key = lowestKey.add(regionIncrement.multiply(BigInteger.valueOf(i)));           byte[] b = String.format("%010x", key).getBytes();           splits[i] = b;       }       return splits;   }        /*         * 插入数据         */   public static void insertData(String tableName, Configuration configuration, String rowkey, String columnFamily) throws IOException {       System.out.println("start insert data ......");       HTable table = new HTable(configuration, "rt_user_tags_test");       Put put = new Put(rowkey.getBytes());       put.add(columnFamily.getBytes(), null, "Java".getBytes());       try {           table.put(put);       } catch (IOException e) {           e.printStackTrace();       }       System.out.println("end insert data ......");   }}

 

转载于:https://www.cnblogs.com/apppointint/p/8885311.html

你可能感兴趣的文章
在webconfig中写好连接后,在程序中如何调用?
查看>>
限制用户不能删除SharePoint列表中的条目(项目)
查看>>
feign调用spring clound eureka 注册中心服务
查看>>
ZT:Linux上安装JDK,最准确
查看>>
LimeJS指南3
查看>>
关于C++ const成员的一些细节
查看>>
《代码大全》学习摘要(五)软件构建中的设计(下)
查看>>
C#检测驱动是否安装的问题
查看>>
web-4. 装饰页面的图像
查看>>
微信测试账户
查看>>
Android ListView上拉获取下一页
查看>>
算法练习题
查看>>
学习使用Django一 安装虚拟环境
查看>>
Hibernate视频学习笔记(8)Lazy策略
查看>>
CSS3 结构性伪类选择器(1)
查看>>
IOS 杂笔-14(被人遗忘的owner)
查看>>
前端基础之BOM和DOM
查看>>
[T-ARA/筷子兄弟][Little Apple]
查看>>
编译Libgdiplus遇到的问题
查看>>
【NOIP 模拟赛】Evensgn 剪树枝 树形dp
查看>>