基于JAVA API访问HBASE的基础操作

1、系统环境

基于HADOOP2.10.1部署的HBASE2.3.4集群

2、实现代码

package com.lineqi.hbase;


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

import java.io.IOException;
import java.util.*;

/*实现如下功能
 对表的管理:创建、删除、truncate、判断表是否存在
 1、检查表否存在:IsExistsTable
 2、创建表:CreateTable
 3、删除表:dropTable
 4、truncate表:truncateTable
 对表中记录的操作:插入、查询、删除记录
 1、插入记录到业务表中:addData
 2、获取表中所有数据:getAllData
 3、根据rowkey获取表中某一行数据:getRowKeyData
 4、获取某一列的数据:getColumnData
 5、根据rowkey删除数据:deleteData
 * @author lineqi
 * create  2021-03-06 11:34
 */
public class HBaseOperate {
    public static void main(String[] args) throws IOException {
        //IsExistsTable("stu");
        createTable("stu1","info","others","test");
        //dropTable("stu1");
        //truncateTable("stu");
        //addData("stu", "002", "info", "age", "18");
        //getAllData("stu");
        //getRowKeyData("stu","002");
        //getColumnData("stu","001","info","age");
        //deleteData("stu","001","002");

    }
    //定义一个静态conf
    public static Configuration conf;
    static{
        //使用HBaseConfiguration的单例方法实例化
        conf=HBaseConfiguration.create();
        //指定zookeeper的IP地址和允许客户端连接的端口号
        conf.set("hbase.zookeeper.quorum", "192.168.0.104:2181,192.168.0.105:2181,192.168.0.106:2181");
    }
    //定义一个conn静态方法
    public static Connection conn() throws IOException {
        Connection conn =ConnectionFactory.createConnection(conf);
        return conn;
    }
    //定义一个hbaseadmin静态方法
    public static HBaseAdmin ha() throws IOException {
        Connection conn =ConnectionFactory.createConnection(conf);
        HBaseAdmin ha= (HBaseAdmin) conn().getAdmin();
        return ha;
    }
    //1、判断表是否存在
    public static boolean IsExistsTable(String tablename) throws IOException {
        return ha().tableExists(TableName.valueOf(tablename));
    }

    //2、创建表
    public static void createTable(String tablename,String... ColumnFamliy) throws IOException {

        if(IsExistsTable(tablename)){
            System.out.println("表已存在,请重新输入!");
        }else {

            //TableDescriptorBuilder TDB = new TableDescriptorBuilder(TableName.valueOf(tablename));
            HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tablename));
            //创建多个列簇
            for(String cf : ColumnFamliy){
                descriptor.addFamily(new HColumnDescriptor(cf));
            }
            //根据对表的配置,创建表
            ha().createTable(descriptor);
            System.out.println("table: " + tablename + "创建成功!");
        }
    }
    //3、truncate表
    public static void truncateTable(String tablename) throws IOException {

        if(ha().tableExists(TableName.valueOf(tablename))){
            ha().disableTable(TableName.valueOf(tablename));
            ha().truncateTable(TableName.valueOf(tablename),true);
            System.out.println("truncate table successfully!");
        }else{
            System.out.println("table is not exists!");
        }

    }
    //4、删除表
    public static void dropTable(String tablename) throws IOException {

        if (ha().tableExists(TableName.valueOf(tablename))) {
            ha().disableTable(TableName.valueOf(tablename));
            ha().deleteTable(TableName.valueOf(tablename));
            System.out.println("delete table successfully!");
        }
        else{
            System.out.println("table is not exists!");
        }

    }
    //5、插入数据
    public static void addData(String tablename,String rowkey,String columnFamily,String columnName,String value) throws IOException {

        if(ha().tableExists(TableName.valueOf(tablename))) {
            Table table = conn().getTable(TableName.valueOf(tablename));
            Put put = new Put(Bytes.toBytes(rowkey));
            put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnName), Bytes.toBytes(value));
            table.put(put);
            table.close();
            System.out.println(" insert successfully!");
        }else{
            System.out.println("insert failed!");
        }

    }
    //6、查询所有数据
    public static void getAllData(String tablename) throws IOException {
        if(ha().tableExists(TableName.valueOf(tablename))) {
            Table table = conn().getTable(TableName.valueOf(tablename));
            Scan scan = new Scan();
            ResultScanner rs = table.getScanner(scan);
            for(Result res:rs){
                Cell cells[] = res.rawCells();
                for(Cell cell:cells){
                    System.out.println("rowkey:"+Bytes.toString(CellUtil.cloneRow(cell)));
                    System.out.println("column famliy:"+Bytes.toString(CellUtil.cloneFamily(cell)));
                    System.out.println("column name:"+Bytes.toString(CellUtil.cloneQualifier(cell)));
                    System.out.println("value:"+Bytes.toString(CellUtil.cloneValue(cell)));
                }

            }
        }else{
            System.out.println("not find records in table,please change others table name,try it again");
        }

    }
    //7、根据rowkey获取某一行数据
    public static void getRowKeyData(String tablename,String rowkey) throws IOException {
        if (ha().tableExists(TableName.valueOf(tablename))){
            Table table = conn().getTable(TableName.valueOf(tablename));
            Get get = new Get(Bytes.toBytes(rowkey));
            Result result=table.get(get);
            for(Cell cell:result.rawCells()){
                System.out.println("rowkey:"+Bytes.toString(CellUtil.cloneRow(cell)));
                System.out.println("column famliy:"+Bytes.toString(CellUtil.cloneFamily(cell)));
                System.out.println("column name:"+Bytes.toString(CellUtil.cloneQualifier(cell)));
                System.out.println("value:"+Bytes.toString(CellUtil.cloneValue(cell)));
            }
        }else{
            System.out.println("table is not exists!");
        }
    }
    //8、根据rowkey获取某一列数据
    public static void getColumnData(String tablename,String rowkey,String columnFa,String columnName) throws IOException {
        if(ha().tableExists(TableName.valueOf(tablename))){
            Table table = conn().getTable(TableName.valueOf(tablename));
            Get get = new Get(Bytes.toBytes(rowkey));
            get.addColumn(Bytes.toBytes(columnFa),Bytes.toBytes(columnName));
            Result result = table.get(get);
            for(Cell cell:result.rawCells()){
                System.out.println("value:"+Bytes.toString(CellUtil.cloneValue(cell)));
            }
        }else{
            System.out.println("table is not exists!");
        }
    }
    //9、删除数据
    public static void deleteData(String tablename,String...rows) throws IOException {
        if(ha().tableExists(TableName.valueOf(tablename))){
            Table table = conn().getTable(TableName.valueOf(tablename));
            List<Delete> deleteList = new ArrayList<>();
            for(String row:rows){
                Delete delete = new Delete(Bytes.toBytes(row));
                deleteList.add(delete);
            }
            table.delete(deleteList);
            table.close();
        }else{
            System.out.println("table is not exists!");
        }
    }

}

3、总结

1、DBA转大数据确实需要有很大一段路需要走
2、HTableDescriptor 在hbase2.0已经废弃了,3.0会被移出,这里找了半天也没有找到相应的替换方法,确实有些菜呀

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值