Hbase数据库操作工具类

package com.it18zhang.test;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

/**
 * 操作hbase的工具类
 */
public class HbaseUtils {
    
    HBaseAdmin admin = null;
    Configuration configration = null;

    /**
     * 私有构造方法
     */
    private HbaseUtils(){
        configration = new Configuration();

        configration.set("hbase.zookeeper.quorum","spark1:2181");
        configration.set("hbase.rootdir","hdfs://spark1:9000/hbase");

        try {
            admin = new HBaseAdmin(configration);
        } catch (IOException e) {
            e.printStackTrace();

        }
    }

    private static HbaseUtils instance = null;
    
    public static synchronized HbaseUtils getInstance(){
        if(null == instance){
            instance = new HbaseUtils();

        }
        return instance;

    }

    /**
     * 根据表名获取到 Htable 实例
     */
    public HTable getTable(String tableName){
        HTable table = null;
        try {
            table = new HTable(configration,tableName);
        } catch (IOException e) {
            e.printStackTrace();

        }

        return table;

    }

    /**
     * 添加一条记录到 Hbase 表 70 30 128 32 核 200T 8000
     * @param tableName Hbase 表名
     * @param rowkey Hbase 表的 rowkey
     * @param cf Hbase 表的 columnfamily列族
     * @param column Hbase 表的列
     * @param value 写入 Hbase 表的值


     */
    public void put(String tableName,String rowkey,String cf,String column,String value){
        HTable table = getTable(tableName);
        Put put = new Put(Bytes.toBytes(rowkey));
        put.add(Bytes.toBytes(cf),Bytes.toBytes(column),Bytes.toBytes(value));
        try {
            table.put(put);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 得到所有的数据
     * @param tableName
     * @throws IOException
     */
    public  void getAllRows(String tableName) throws IOException {
        HTable table = getTable(tableName);
        //得到用于扫描region的对象
        Scan scan = new Scan();
        //使用HTable得到resultcanner实现类的对象
        ResultScanner resultScanner = table.getScanner(scan);
        for (Result result : resultScanner) {
            Cell[] cells = result.rawCells();
            for (Cell cell : cells) {
                //得到rowkey
                System.out.println(Bytes.toString(CellUtil.cloneRow(cell)));
                //得到列族
                System.out.println(Bytes.toString(CellUtil.cloneFamily(cell)));
                System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell)));
                System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
            }
        }
    }

        public static void main(String[] args) throws IOException {
//HTable table = HBaseUtils.getInstance().getTable("category_clickcount");
//System.out.println(table.getName().getNameAsString());
        String tableName = "user_activity";
       /* String rowkey = "20171122_1";
        String cf="info";
        String column ="click_count";
        String value = "2";*/
        HbaseUtils.getInstance().getAllRows(tableName);

    }
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数据指北Ai

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值