ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

HBaseUtil

2020-06-15 11:38:43  阅读:248  来源: 互联网

标签:HBaseUtil String tableName Exception param tb throws


HBase的JAVA客户端API简单使用,及工具类编写

package com.utils;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class HBaseUtil {

/**
 * 连接HBase
 * @return
 */
public static Connection getHBaseConnection() throws Exception {
    Configuration conf = HBaseConfiguration.create();
    conf.set("hbase.zookeeper.quorum","j01:2181,j02:2181,j03:2181");
    Connection conn = ConnectionFactory.createConnection(conf);
    return conn;
}

/**
 * 获取admin对象
 * @return
 * @throws Exception
 */
public static Admin getHBaseAmin(Connection conn) throws Exception {
   return conn.getAdmin();
}

/**
 * 获取表对象
 * @param tablename
 * @return
 * @throws Exception
 */
public static Table getHBaseTableName(Connection conn,String tablename) throws Exception {
    Table table = conn.getTable(TableName.valueOf(tablename));
    return table;
}

/**
 * 展示单元格数据
 * @param result
 */
public  static  void showData(Result result){
    while(result.advance()){
        Cell cell = result.current();
        String row = new String(CellUtil.cloneRow(cell));
        String family = new String(CellUtil.cloneFamily(cell));
        String qualifier = new String(CellUtil.cloneQualifier(cell));
        String value = new String(CellUtil.cloneValue(cell));
        System.out.println(row+"->"+family+":"+qualifier+"->"+value);
    }

}

/**
 * 删除指定表
 * @param tablename
 * @throws Exception
 */
public  static void deleteTable(String tablename) throws Exception {
    // 创建Admin对象
    Admin admin = HBaseUtil.getHBaseAmin(HBaseUtil.getHBaseConnection());
    // 获取表
    TableName tb = TableName.valueOf("tablename");

    // 判断表是否存在,存在则删除
    if(admin.tableExists(tb)){
        // 禁用表
        admin.disableTable(tb);
        // 判断是否禁用,禁用则删除
        if (admin.isTableDisabled(tb)){
            admin.deleteTable(tb);
        }else{
            System.out.println("please disable table:" + tablename);
        }
    }else{
        System.out.println("this table:" + tablename + " is not exits");
    }
    // 释放资源
    admin.close();
}

/**
 * 修改表结构:修改列族的版本属性
 * @param tableName
 * @param columnFamilyName
 * @param columnFamilyVersion
 * @throws Exception
 */
public static void modifyTableStructrue(String tableName,String columnFamilyName,int columnFamilyVersion) throws Exception {
    // 创建Admin对象
    Admin admin = HBaseUtil.getHBaseAmin(HBaseUtil.getHBaseConnection());
    // 创建列族描述器对象
    ColumnFamilyDescriptorBuilder cfdb = ColumnFamilyDescriptorBuilder.newBuilder(columnFamilyName.getBytes());
    // 设置列族参数
    cfdb.setMaxVersions(columnFamilyVersion);
    // 构建列族
    ColumnFamilyDescriptor cfd = cfdb.build();
    TableName tb = TableName.valueOf(tableName);
    // 表存在则修改属性
    if(admin.tableExists(tb)){
        admin.modifyColumnFamily(tb,cfd);
    }
    admin.close();
}

/**
 * 创建预分region表,指定分割点
 * @param tableName
 * @param columnFamilyName
 * @param regionNum
 * @throws Exception
 */
public static void createCustomRegions(String tableName,String columnFamilyName,String...regionNum) throws Exception {
    // 创建Admin对象
    Admin admin = HBaseUtil.getHBaseAmin(HBaseUtil.getHBaseConnection());
    // 表的描述器
    TableDescriptorBuilder tdb = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));
    // 列族的描述器
    ColumnFamilyDescriptorBuilder cfdb = ColumnFamilyDescriptorBuilder.newBuilder(columnFamilyName.getBytes());
    ColumnFamilyDescriptor cfd = cfdb.build();
    // 将列族添加到表中
    tdb.setColumnFamily(cfd);
    TableDescriptor td = tdb.build();
    byte[][] by = new byte[regionNum.length][1];
    for (int i = 0; i < regionNum.length; i++) {
        by[i] = regionNum[i].getBytes();
    }
    // 建表,指定预分的region的key
    admin.createTable(td,by);
    // 释放资源
    admin.close();
}

/**
 * 创建多个列族,并指定,数据版本个数,和数据存储时间
 * @param tableName
 * @param columnFamilyVersion
 * @param columnFamilyDataTimeLive
 * @param columnFamilyName
 * @throws Exception
 */
public static void createMoreColumnFamilyTable(String tableName,int columnFamilyVersion,int columnFamilyDataTimeLive,String... columnFamilyName) throws Exception {
    // 创建Admin对象
    Admin admin = HBaseUtil.getHBaseAmin(HBaseUtil.getHBaseConnection());
    // 创建表的构造器
    TableDescriptorBuilder tdb = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));
    // 使用集合存储列族对象
    List<ColumnFamilyDescriptor> list = new ArrayList<>();

    for (int i = 0; i < columnFamilyName.length; i++) {
        // 创建列族的构造器
        ColumnFamilyDescriptorBuilder cfdb = ColumnFamilyDescriptorBuilder.newBuilder(columnFamilyName[i].getBytes());
        // 设置列族属性
        // 设置列族数据存储的版本数
        cfdb.setMaxVersions(columnFamilyVersion);
        // 设置列族中数据的存储时间
        cfdb.setTimeToLive((columnFamilyDataTimeLive));
        list.add(cfdb.build());
    }
    // 表的构造器添加列族
    tdb.setColumnFamilies(list);
    // 表描述对象
    TableDescriptor td = tdb.build();
    // 创建多个列族的表
    admin.createTable(td);
    // 释放资源
    admin.close();
}

/**
 * 创建单列族的表,设置列族数据版本个数和数据存储时间
 * @param tableName
 * @param columnFamilyName
 * @param columnFamilyVersions
 * @param columnFamilyDataliveTime
 * @throws Exception
 */
public static void createOneColumnFamilyTable(String tableName,String columnFamilyName,int columnFamilyVersions,int columnFamilyDataliveTime) throws Exception {
    // 创建Admin对象
    Admin admin = HBaseUtil.getHBaseAmin(HBaseUtil.getHBaseConnection());
    // 创建表的构造器
    TableDescriptorBuilder tdb = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName.getBytes()));
    // 创建列族构造器
    ColumnFamilyDescriptorBuilder cfdb = ColumnFamilyDescriptorBuilder.newBuilder(columnFamilyName.getBytes());
    // 设置列族属性
    // 设置数据版本个数和存储时间
    cfdb.setTimeToLive(columnFamilyDataliveTime);
    cfdb.setMaxVersions(columnFamilyVersions);
    // 创建列族描述对象
    ColumnFamilyDescriptor cfd = cfdb.build();
    // 向表中加列族
    tdb.setColumnFamily(cfd);
    // 创建表的描述对象
    TableDescriptor td = tdb.build();
    // 创建表
    admin.createTable(td);
    // 释放资源
    admin.close();
}

/**
 * 删除指定列数据
 * @param tableName
 * @param rowKey
 * @param family
 * @param column
 * @throws Exception
 */
public static void deleteData(String tableName,String rowKey,String family,String column) throws Exception {
    // 获取表
    Table tb = HBaseUtil.getHBaseTableName(HBaseUtil.getHBaseConnection(), tableName);
    // 创建delete对象,指定删除行
    Delete delete = new Delete(rowKey.getBytes());
    // 指定删除列数据
    delete.addColumn(family.getBytes(),column.getBytes());
    // 删除数据
    tb.delete(delete);
    // 释放资源
    tb.close();
}

/**
 * 删除指定列族
 * @param tableName
 * @param rowKey
 * @param family
 * @throws Exception
 */
public static void deleteFamily(String tableName,String rowKey,String family) throws Exception {
    // 获取表
    Table tb = HBaseUtil.getHBaseTableName(HBaseUtil.getHBaseConnection(), tableName);
    // 创建delete对象,指定删除行
    Delete delete = new Delete(rowKey.getBytes());
    // 指定删除列族
    delete.addFamily(family.getBytes());
    // 删除数据
    tb.delete(delete);
    // 释放资源
    tb.close();
}

/**
 * 删除指定行
 * @param tableName
 * @param rowKey
 * @throws Exception
 */
public static void deleteRow(String tableName,String rowKey) throws Exception {
    // 获取表
    Table tb = HBaseUtil.getHBaseTableName(HBaseUtil.getHBaseConnection(), tableName);
    // 创建delete对象,指定删除行
    Delete delete = new Delete(rowKey.getBytes());
    // 删除数据
    tb.delete(delete);
    // 释放资源
    tb.close();
}

/**
 * 扫描指定rk范围数据
 * @param tableName
 * @param startRowKey
 * @param endRowKey
 * @throws Exception
 */
public static void scanData(String tableName,String startRowKey,String endRowKey) throws Exception {
    // 获取表
    Table tb = HBaseUtil.getHBaseTableName(HBaseUtil.getHBaseConnection(), tableName);
    // 获取扫描对象
    Scan scan = new Scan();
    // 从startRowKey开始扫描,到endRowKey结束,含头不含尾
    scan.withStartRow(startRowKey.getBytes());
    scan.withStopRow(endRowKey.getBytes());
    ResultScanner scanner = tb.getScanner(scan);
    Iterator<Result> it = scanner.iterator();
    while(it.hasNext()){
        Result result = it.next();
        HBaseUtil.showData(result);
    }
    tb.close();
}

/**
 * 获取单行或多行数据
 * @param tableName
 * @param rowKey
 * @throws Exception
 */
public static void getRowDatas(String tableName,String... rowKey) throws Exception {
    // 获取表
    Table tb = HBaseUtil.getHBaseTableName(HBaseUtil.getHBaseConnection(), tableName);
    // 创建集合存储行数据
    List<Get> list = new ArrayList<>();
    for (int i = 0; i < rowKey.length; i++) {
        Get get = new Get(rowKey[i].getBytes());
        list.add(get);
    }
    Result[] results = tb.get(list);
    // 展示行数据
    for (Result result:results) {
        HBaseUtil.showData(result);
        System.out.println(result);
    }
    tb.close();
}

/**
 * 指定行,列族,列插入数据
 * @param tableName
 * @param rowKey
 * @param columnFamily
 * @param column
 * @param value
 * @throws Exception
 */
public static void putData(String tableName,String rowKey,String columnFamily,String column,String value) throws Exception {
    // 获取表
    Table tb = HBaseUtil.getHBaseTableName(HBaseUtil.getHBaseConnection(), tableName);
    // 获取Put对象,并设置行健
    Put put = new Put(rowKey.getBytes());
    // 设置添加值
    put.addColumn(columnFamily.getBytes(),column.getBytes(),value.getBytes());
    // 添加数据
    tb.put(put);
    tb.close();
}

}

标签:HBaseUtil,String,tableName,Exception,param,tb,throws
来源: https://blog.csdn.net/AnameJL/article/details/106726943

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有