当前位置: 移动技术网 > IT编程>开发语言>Java > Hbase的工具类

Hbase的工具类

2020年07月19日  | 移动技术网IT编程  | 我要评论

以下为hbase在java客户端中会用到的工具类:

package com.doit.oneMore.tools;

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.io.IOException;

/**
 * Author:   reddy
 * Date:     2020/7/17
 * Description:1获取连接
 */
public class HbaseTools {
    /*
    获取hbase连接对象
     */
    public static Connection getHbaseConnection() throws Exception {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","linux01:2181,linux02:2181,linux03:2181");
        return ConnectionFactory.createConnection(conf);
    }
    /*
    获取Hbase的管理对象
     */
    public static Admin getHbaseAdmin() throws Exception {
        return getHbaseConnection().getAdmin();
    }
    /*
    获取表对象
     */
    public static Table getTable(String tbName) throws Exception {
        return getHbaseConnection().getTable(TableName.valueOf(tbName));
    }
    /*
    展示数据
     */
    public static void showData(Result res){
        if(res!=null){
            while(res.advance()){
                Cell cell = res.current();
                byte[] row = CellUtil.cloneRow(cell);
                byte[] family = CellUtil.cloneFamily(cell);
                byte[] qualifier = CellUtil.cloneQualifier(cell);
                byte[] value = CellUtil.cloneValue(cell);
                System.out.println("行键"+row+"=="+"列族"+family+"=="+"属性"+qualifier+"=="+"值"+value);
            }
        }
    }
}

 

本文地址:https://blog.csdn.net/reddy_Hu/article/details/107417484

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网