Hbase Api 基于1.0.3版本

来源:互联网 发布:notepad python插件 编辑:程序博客网 时间:2024/05/19 08:41
private static Configuration conf = null;private static Connection conn = null;/** * 获取全局唯一的Configuration实例 * * @return */private static synchronized Configuration getConf() {    if (conf == null) {        conf = HBaseConfiguration.create();        conf.set("hbase.zookeeper.quorum", "127.0.0.1");        conf.set("hbase.zookeeper.property.clientPort", "2181");    }    return conf;}/** * 获取全局唯一的HConnection实例 * * @return * @throws IOException */private static synchronized Connection getHbaseConn() throws IOException {    if (conn == null) {        //Hbase1.0.0以后的APi以前的方法被丢弃        conn = ConnectionFactory.createConnection(conf);// HBase 0.99+    }    return conn;}/***  Connection是线程安全的,因此,多个客户端线程可以共享一个Connection* **/public void addHbaseTable(String tableName, String[] tableFaimly) throws IOException {    try {        Table htable = conn.getTable(TableName.valueOf(tableName));        Admin admin = conn.getAdmin();        //table exists        if (admin.tableExists(TableName.valueOf(""))) {            return;        }        HTableDescriptor hdp = new HTableDescriptor(TableName.valueOf(""));        for (String sf : tableFaimly) {            hdp.addFamily(new HColumnDescriptor(sf));        }        admin.createTable(hdp);    } catch (IOException e) {        e.printStackTrace();    } finally {        conn.close();    }}/** * 删除表 */public static void deleteTable(String tableName) throws IOException {    Admin admin = conn.getAdmin();    if (admin.tableExists(TableName.valueOf(tableName))) {        admin.disableTable(TableName.valueOf(tableName));        admin.deleteTable(TableName.valueOf(tableName));    }}/** * 插入单条记录 */public static void inserSigeRow(String tableNmae, String rowKey, String familyCol, String qualifierCol, String rowValu) {    try {        Table tb = conn.getTable(TableName.valueOf(tableNmae));        Put put = new Put(Bytes.toBytes(rowKey));        put.addColumn(familyCol.getBytes(), qualifierCol.getBytes(), rowKey.getBytes());        tb.put(put);    } catch (IOException e) {        e.printStackTrace();    }}/** * 多条数据插入 */public void inserMutilData(String tableName, String row, String family, Map<String, String> items) throws IOException {    if (row == null || row.equals("") || family == null || family.equals("") || items == null || items.size() < 0)        throw new NullPointerException("argument exist null or ''");    List<Put> putList = new ArrayList<Put>();    Table tb = conn.getTable(TableName.valueOf(tableName));    for (Map.Entry<String, String> item : items.entrySet()) {        String qualifier = item.getKey();        String value = item.getValue();        Put put = new Put(Bytes.toBytes(row));        put.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(value));        putList.add(put);    }    tb.put(putList);}/** * * * */public void deleteRow(String tableName, String rowKey) throws IOException {    Delete delete = new Delete(rowKey.getBytes());    Table tb = conn.getTable(TableName.valueOf(tableName));    tb.delete(delete);}/** * * */public static Result getResult(String tableName, String rowKey) throws IOException {    Table tb = conn.getTable(TableName.valueOf(tableName));    Get get = new Get(rowKey.getBytes());    Result st = tb.get(get);    return st;}/**** **/public static ResultScanner getScans(String tableName, String familyCol, String qualitCol, String startRowkey, String endRowkey) throws IOException {    ResultScanner resultScanner = null;    Table table = conn.getTable(TableName.valueOf(tableName));    Scan sc = new Scan();    if (StringUtils.isNotEmpty(familyCol) && StringUtils.isNotBlank(qualitCol)) {        sc.addColumn(familyCol.getBytes(), qualitCol.getBytes());    }    if (StringUtils.isNotEmpty(familyCol) && StringUtils.isEmpty(qualitCol)) {        sc.addFamily(familyCol.getBytes());    }    if (StringUtils.isNotEmpty(startRowkey)) {        sc.setStartRow(startRowkey.getBytes());    }    if (StringUtils.isNotEmpty(endRowkey)) {        sc.setStopRow(endRowkey.getBytes());    }    resultScanner = table.getScanner(sc);    return resultScanner;}public static void mian(String args[]) throws IOException {    Result rs = getResult("test", "123");    Cell[] cells = rs.rawCells();    for (Cell cl : cells) {        System.out.print(Bytes.toString(CellUtil.cloneFamily(cl)));        System.out.print(Bytes.toString(CellUtil.cloneQualifier(cl)));        System.out.print(Bytes.toString(CellUtil.cloneValue(cl)));        System.out.print(Bytes.toString(CellUtil.cloneFamily(cl)));    }    ResultScanner resultScanner = getScans("test", "address", "city", "abc123", "abc234");    Iterator<Result> iterator = resultScanner.iterator();    while (iterator.hasNext()) {        Result result = iterator.next();        Cell[] cell = result.rawCells();        for (Cell cs : cell) {            String rowRecord = Bytes.toString(CellUtil.cloneRow(cs));            String rowFamily = Bytes.toString(CellUtil.cloneFamily(cs));            String rowQuality = Bytes.toString(CellUtil.cloneQualifier(cs));            String rowValue = Bytes.toString(CellUtil.cloneValue(cs));            System.out.println();        }    }}

创建table 时建立预分区

public static void createTable(String tableName, String cfs[], byte[][] splitKey) {        connection = getConnection();        try {            HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();            if (admin.tableExists(tableName)) {                logger.info("table {} is exists", tableName);                return;            }            HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));            for (String str : cfs) {                HColumnDescriptor columnDescriptor = new HColumnDescriptor(str);//                columnDescriptor.setCompressionType(Compression.Algorithm.SNAPPY);                columnDescriptor.setMaxVersions(1);                descriptor.addFamily(columnDescriptor);            }            admin.createTable(descriptor, splitKey);        } catch (IOException e) {            logger.error("create {} table fail", tableName);            e.printStackTrace();        }    }

如图 Student 有10个分区:
这里写图片描述

由于默认Hbase只有一个region,为了避免spilt和热点的问题最好在建表的时候设立预分区:

如下向student表中插入10W条数据,大概需要2秒,而且数据进行了均匀的分布

 public static void mutilPut(String tableName, List<Put> putList) {        connection = getConnection();        BufferedMutator mutator = null;        final BufferedMutator.ExceptionListener exceptionListener = new BufferedMutator.ExceptionListener() {            @Override            public void onException(RetriesExhaustedWithDetailsException e, BufferedMutator bufferedMutator) throws RetriesExhaustedWithDetailsException {                for (int i = 0; i < e.getNumExceptions(); i++) {                    logger.error("sent row fail:" + e.getRow(i));                }            }        };        final BufferedMutatorParams mutatorParam = new BufferedMutatorParams(TableName.valueOf(tableName)).listener(exceptionListener);        mutatorParam.writeBufferSize(10 * 1024 * 102);        try {            mutator = connection.getBufferedMutator(mutatorParam);            mutator.mutate(putList);            mutator.flush();        } catch (IOException e) {            e.printStackTrace();        } finally {            if (mutator != null) {                try {                    mutator.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }private static List<Put> batchPut(){        Random random=new Random();        List<Put> list = new ArrayList<Put>();        for(int i=1;i<=100000;i++){            byte[] rowkey = Bytes.toBytes(random.nextInt(10)+"-"+System.currentTimeMillis()+"-"+i);            Put put = new Put(rowkey);            put.addColumn(Bytes.toBytes("sage"), Bytes.toBytes("age"), Bytes.toBytes("age_"+i));            list.add(put);        }

这里写图片描述

0 0
原创粉丝点击