Hbase工具类

这篇博客介绍了如何在Java环境中使用Hbase,包括必须的依赖引入和一个专注于查询功能的工具类。
摘要由CSDN通过智能技术生成

在Java中使用Hbase

引入依赖

<!--Hbase客户端和服务端-->
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-server</artifactId>
            <version>1.3.1</version>
        </dependency>

工具类

只涉及查询

public  class HbaseUtil {
   
    public static Configuration conf;
    private static Connection connection=null;
    static {
   
        //配置文件
        conf= HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "zookeeper集群地址");
        conf.set("hbase.client.ipc.pool.type","RoundRobinPool");
        conf.set("hbase.client.ipc.pool.size","8");
        //获取连接对象
        try {
   
            connection = ConnectionFactory.createConnection(conf);
        } catch (IOException e) {
   
            e.printStackTrace();
        }
    }


    //判断表是否存在
    public static boolean tableExist(String tabName) throws IOException {
   
        Admin  admin = connection.getAdmin();
        boolean exists = admin.tableExists(TableName.valueOf(tabName));
        //关闭资源
        admin.close();
        return exists;

    }

    // 获得连接
    public static synchronized Connection getCon() {
   
        if (connection == null || connection.isClosed()) {
   
            try {
   
                connection = ConnectionFactory.createConnection(conf);
            } catch (IOException e) {
   
                e.printStackTrace();
            }
        }
        return connection;
    }

    // 关闭连接
/*    public static void close() {
        if (connection != null) {
            try {
                connection.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }*/
    // 数据读取
    //取到一个值
    public static String byGet(String tableName, String rowKey, String family,
                               String qualifier) {
   
        Table table=null;
        try {
   
            table= getCon().getTable(TableName.valueOf(tableName));
            Get get = new Get(Bytes.toBytes(rowKey));
            get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
            Result r = table.get(get);
            if(r!=null){
   
                if(r.listCells()!=null && r.listCells().size()>0){
   
                    table.close();
                    return Bytes.toString(CellUtil.cloneValue(r.listCells().get(0)));
                }
            }
            table.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值