HBase的api操作

pom文件依赖导入

<dependencies>
        <!--HBase服务器-->
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-server</artifactId>
            <version>1.4.10</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.4.10</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

创建一个HBaseUtil类

package cn.edu.zut;

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.ArrayList;
import java.util.List;

public class HBaseUtil {

    //---------------对表的创建,删除,和查询数据库中所有表--------------------
    private static Configuration conf;

    private static Connection conn;

    private static Admin admin;

    /**
     * 创建表
     * @param tableName
     * @param fms
     * @return
     */
    public static boolean createTable(String tableName,String[] fms){
        //定义返回值
        boolean b = false;
        //判断表是否存在,若存在,则无法创建表
        if (isTableExist(tableName)){
            System.out.println("表" + tableName + "已经存在");
            return b;
        }else {
            //创建表属性对象
            HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
            //创建列族
            if (fms.length == 0){
                //如果没有传入列族,则默认创建列族为info
                descriptor.addFamily(new HColumnDescriptor("info"));
            }else {
                //为表添加列族
                for (String fm : fms)
                    descriptor.addFamily(new HColumnDescriptor(fm));
            }
            //创建表
            try {
                //创建表操作
                admin.createTable(descriptor);
                //创建成功后把返回值复制为true
                b = true;
            } catch (IOException e) {
                e.printStackTrace();
                //若创建表失败则扔出异常
                return b;
            }
        }
        return b;
    }

    /**
     * 建立连接
     * @param zookeeperQuorum
     */
    public static void setConnection(String zookeeperQuorum){
        //获取hbase配置文件
        conf = HBaseConfiguration.create();
        //配置hbase.zookeeper.quorum
        conf.set("hbase.zookeeper.quorum",zookeeperQuorum);
        try {
            //创建连接
            conn = ConnectionFactory.createConnection(conf);
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
    }

    /**
     * 获取连接
     * @return
     */
    public static Connection getConnection(){
        return conn;
    }

    /**
     * 获取hbase操作对象
     * @return
     */
    public static Admin getAdmin(){
        try {
            //获取hbase操作对象
            admin = conn.getAdmin();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return admin;
    }

    /**
     * 删除表
     * @param tableName
     * @return
     */
    public static boolean deleteTable(String tableName){
        boolean b = false;
        //首先判断表是否存在,只有表存在才可以执行删除操作
        if (isTableExist(tableName)){
            try {
                //首先禁用要删除的表
                admin.disableTable(TableName.valueOf(tableName));
                //删除表单的操作
                admin.deleteTable(TableName.valueOf(tableName));
                b = true;
            } catch (IOException e) {
                e.printStackTrace();
                return b;
            }
        }else {
            System.out.println(tableName + "表不存在");
            return b;
        }
        return b;
    }

    /**
     * 查询所有表
     * @return
     */
    public static String[] listTableName(){
        TableName[] tns;
        try {
            //获取TableName数组类型的表名
            tns = admin.listTableNames();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        //由于返回值类型是String数组类型的,所以需要把TableName数组类型转换成String数组类型
        String[] ts = new String[tns.length];
        for (int i = 0; i < tns.length; i++) {
            ts[i] = tns[i].toString();
        }
        return ts;
    }

    /**
     * 判断表是否存在
     * @param tableName
     * @return
     */
    public static boolean isTableExist(String tableName){

        TableName tableName1 = TableName.valueOf(tableName);
        boolean b = false;
        try {
            //判断表是否存在
            b = admin.tableExists(tableName1);
        } catch (IOException e) {
            e.printStackTrace();
            return b;
        }
        return b;
    }


    //-----------------------对表数据的操作---------------------------


    /**
     * 插入单条数据
     * @param tableName
     * @param cell
     * @return
     */
    public static boolean insert(String tableName,SampleCell cell){
        boolean b = false;

        //判断表是否存在,若表不存在,则插入失败
        if (!isTableExist(tableName)){
            System.err.println(tableName + "表不存在");
            return b;
        }
        //设置Put对象,并设置Rowkey
        Put put = new Put(Bytes.toBytes(cell.getRowkey()));
        //给put对象添加列族名、列名以及数据
        put.addColumn(Bytes.toBytes(cell.getFamilyName()),
                Bytes.toBytes(cell.getColName()),
                Bytes.toBytes(cell.getData()));
        try {
            //获取表操作对象
            Table table = conn.getTable(TableName.valueOf(tableName));
            //执行插入数据到表中
            table.put(put);
            b = true;
            //释放资源
            table.close();
        } catch (IOException e) {
            e.printStackTrace();
            return b;
        }
        return b;
    }

    /**
     * 批量插入数据
     * @param tableName
     * @param cells
     * @return
     */
    public static boolean insert(String tableName, List<SampleCell> cells){
        boolean b = false;
        //判断表是否存在,若表不存在则无法插入数据到表中
        if (!isTableExist(tableName)){
            System.err.println(tableName + "表不存在");
            return b;
        }
        //设置Put对象
        List<Put> puts = new ArrayList<>();
        //遍历集合中的SampleCell对象,并把对象中的数据添加到Put对象里
        for (SampleCell cell:cells) {
            //设置Put对象,并添加Rowkey
            Put put = new Put(Bytes.toBytes(cell.getRowkey()));
            //添加到Put对象中列族名、列名和数据
            put.addColumn(Bytes.toBytes(cell.getFamilyName()),
                    Bytes.toBytes(cell.getColName()),
                    Bytes.toBytes(cell.getData()));
            //把Put对象添加到集合中
            puts.add(put);
        }
        try {
            //获取表操作对象
            Table table = conn.getTable(TableName.valueOf(tableName));
            //执行批量数据的插入
            table.put(puts);
            b = true;
            table.close();
        } catch (IOException e) {
            e.printStackTrace();
            return b;
        }
        return b;
    }

    /**
     * 更新数据
     * @param tableName
     * @param cell
     * @return
     */
    public static boolean update(String tableName,SampleCell cell){
        //更新和插入单条数据的代码一样,顾调用插入单条数据的方法
        return insert(tableName,cell);
    }

    /**
     * 删除数据
     * @param tableName
     * @param cell
     * @return
     */
    public static boolean delete(String tableName,SampleCell cell) {

        boolean b = false;
        //判断表是否存在
        if (!isTableExist(tableName)){
            System.err.println(tableName + "表不存在");
            return b;
        }
        try {
            //获取表对象
            Table table = conn.getTable(TableName.valueOf(tableName));
            //获取Delete对象并且设置Rowkey
            Delete delete = new Delete(Bytes.toBytes(cell.getRowkey()));
            //添加要删除的列族名和列名所对应的数据
            delete.addColumn(Bytes.toBytes(cell.getFamilyName()),
                    Bytes.toBytes(cell.getColName()));
            //执行删除操作
            table.delete(delete);
            b = true;
            table.close();
        } catch (IOException e) {
            e.printStackTrace();
            return b;
        }

        return b;
    }

    /**
     * 扫描全表
     * @param tableName
     * @return
     * @throws IOException
     */
    public static List<SampleCell> listAll(String tableName) throws IOException {
        //创建返回值类型List集合
        List<SampleCell> sampleCellList = new ArrayList<>();
        //判断表是否存在
        if (!isTableExist(tableName)){
            System.err.println(tableName + "表不存在");
            //若不存在,则返回空集合
            return sampleCellList;
        }
        //获取表对象
        Table table = conn.getTable(TableName.valueOf(tableName));
        //设置Scan对象
        Scan scan = new Scan();
        //扫描全表
        ResultScanner rs = table.getScanner(scan);
        //遍历Result对象
        for (Result r:rs) {
            //遍历Cell对象
            for (Cell cell:r.rawCells()) {
                //获取SampleCell对象
                SampleCell sampleCell = new SampleCell();
                sampleCell.setFamilyName(Bytes.toString(CellUtil.cloneFamily(cell)));
                sampleCell.setColName(Bytes.toString(CellUtil.cloneQualifier(cell)));
                sampleCell.setRowkey(Bytes.toString(CellUtil.cloneRow(cell)));
                sampleCell.setData(Bytes.toString(CellUtil.cloneValue(cell)));
                //把数据添加到集合中
                sampleCellList.add(sampleCell);
            }


        }
        table.close();
        return sampleCellList;
    }
    public static List<SampleCell> queryByRowKey(String tableName,String rowKey) throws IOException {
        //创建返类型List集合
        List<SampleCell> sampleCellList = new ArrayList<>();
        //判断表是否存在
        if (!isTableExist(tableName)){
            System.err.println(tableName + "表不存在");
            //不存在,返回空集合
            return sampleCellList;
        }
        //获取表对象
        Table table = conn.getTable(TableName.valueOf(tableName));
        //获取Get对象并设置Rowkey
        Get get = new Get(Bytes.toBytes(rowKey));
        //获取数据
        Result result = table.get(get);
        //遍历结果
        for (Cell cell:result.rawCells()){
            SampleCell sampleCell = new SampleCell();
            sampleCell.setFamilyName(Bytes.toString(CellUtil.cloneFamily(cell)));
            sampleCell.setColName(Bytes.toString(CellUtil.cloneQualifier(cell)));
            sampleCell.setRowkey(Bytes.toString(CellUtil.cloneRow(cell)));
            sampleCell.setData(Bytes.toString(CellUtil.cloneValue(cell)));
            sampleCellList.add(sampleCell);

        }
        table.close();
        return sampleCellList;
    }



    /**
     * 释放资源
     * @throws IOException
     */
    public static void closeAll() throws IOException {
        //若相关对象不为空,则关闭相关资源
        if (admin != null){
            admin.close();
        }

        if (conn != null){
            conn.close();
        }

    }
}

实体类SampleCell

package cn.edu.zut;

public class SampleCell {

    private String familyName;
    private String colName;
    private String rowkey;
    private String data;

    public String getFamilyName() {
        return familyName;
    }

    public void setFamilyName(String familyName) {
        this.familyName = familyName;
    }

    public String getColName() {
        return colName;
    }

    public void setColName(String colName) {
        this.colName = colName;
    }

    public String getRowkey() {
        return rowkey;
    }

    public void setRowkey(String rowkey) {
        this.rowkey = rowkey;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "SampleCell{" +
                "familyName='" + familyName + '\'' +
                ", colName='" + colName + '\'' +
                ", rowkey='" + rowkey + '\'' +
                ", data='" + data + '\'' +
                '}';
    }
}

开启集群的服务,测试:

package cn.edu.zut;

import cn.edu.zut.hw.houyakun.HBaseUtil;
import cn.edu.zut.hw.houyakun.SampleCell;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class HBaseUtilTest {

    //初始化变量
    @Before
    public void init(){
        HBaseUtil.setConnection("node01:2181,node02:2181,node03:2181");
        HBaseUtil.getConnection();
        HBaseUtil.getAdmin();
        System.out.println("------------初始化变量----------------");
    }

    //------------------------------------
    //测试表的创建
    @Test
    public void createTableTest(){
        String[] fms = {"info","edu"};
        boolean b = HBaseUtil.createTable("Student",fms);
        if (b){
            System.out.println("表创建成功");
        }
    }

    //测试表的删除
    @Test
    public void deleteTableTest(){
        boolean b = HBaseUtil.deleteTable("Student");
        if (b){
            System.out.println("表删除成功");
        }else {
            System.out.println("表删除失败");
        }
    }

    //测试表的遍历
    @Test
    public void listTableNameTest(){
        String[] ltns =  HBaseUtil.listTableName();
        if (ltns.length ==0){
            System.out.println("无表存在");
        }else {
            for (String ltn : ltns
                 ) {
                System.out.println(ltn);

            }
        }
    }
    //----------------------------------------------------
    //测试插入一条数据
    @Test
    public void insertTest(){
        SampleCell sampleCell = new SampleCell();
        sampleCell.setFamilyName("info");
        sampleCell.setColName("sex");
        sampleCell.setRowkey("10001");
        sampleCell.setData("男");

        if (HBaseUtil.insert("Student",sampleCell)){
            System.out.println("数据插入成功");
        }else {
            System.out.println("数据插入失败");
        }
    }

    //插入多条数据
    @Test
    public void insertsTest(){
        List<SampleCell> sampleCellList = new ArrayList<>();
        SampleCell sampleCell1 = new SampleCell();
        sampleCell1.setFamilyName("info");
        sampleCell1.setColName("name");
        sampleCell1.setRowkey("10002");
        sampleCell1.setData("李四");
        SampleCell sampleCell2 = new SampleCell();
        sampleCell2.setFamilyName("info");
        sampleCell2.setColName("name");
        sampleCell2.setRowkey("10003");
        sampleCell2.setData("王五");
        sampleCellList.add(sampleCell1);
        sampleCellList.add(sampleCell2);
        if ( HBaseUtil.insert("Student",sampleCellList)){
            System.out.println("批量数据插入成功");
        }else {
            System.out.println("批量数据插入失败");
        }
    }

    //测试更新数据
    @Test
    public void updateTest(){
        SampleCell sampleCell = new SampleCell();
        sampleCell.setFamilyName("info");
        sampleCell.setColName("sex");
        sampleCell.setRowkey("10001");
        sampleCell.setData("女");
        if (HBaseUtil.insert("Student",sampleCell)){
            System.out.println("数据更新成功");
        }else {
            System.out.println("数据更新失败");
        }
    }

    //测试删除数据
    @Test
    public void deleteTest(){
        SampleCell sampleCell = new SampleCell();
        sampleCell.setFamilyName("info");
        sampleCell.setColName("sex");
        sampleCell.setRowkey("10001");
        sampleCell.setData("女");
        if (HBaseUtil.delete("Student",sampleCell)){
            System.out.println("数据删除成功");
        }else {
            System.out.println("数据删除失败");
        }
    }

    //查询所有数据
    @Test
    public void listAllTest() throws IOException {
        List<SampleCell> sampleCellList = new ArrayList<>();
        sampleCellList = HBaseUtil.listAll("Student");
        if (sampleCellList.size() == 0){
            System.out.println("表中无数据");
        }else {
            for (SampleCell sampleCell:sampleCellList) {
                System.out.println("--------****************----------");
                System.out.println("列族名: " + sampleCell.getFamilyName());
                System.out.println("列名: " + sampleCell.getColName());
                System.out.println("RowKey名: " + sampleCell.getRowkey());
                System.out.println("数据: " + sampleCell.getData());
            }
        }
    }

    //按照Rowkey查询
    @Test
    public void queryByRowKeyTest() throws IOException {
        List<SampleCell> sampleCellList = new ArrayList<>();
        sampleCellList = HBaseUtil.queryByRowKey("Student","10001");
        if (sampleCellList.size() == 0){
            System.out.println("表中无数据");
        }else {
            for (SampleCell sampleCell:sampleCellList) {
                System.out.println("--------****************----------");
                System.out.println("列族名: " + sampleCell.getFamilyName());
                System.out.println("列名: " + sampleCell.getColName());
                System.out.println("RowKey名: " + sampleCell.getRowkey());
                System.out.println("数据: " + sampleCell.getData());
            }
        }
    }

    //关闭资源
    @After
    public void closeAll() throws IOException {
        HBaseUtil.closeAll();
        System.out.println("------------释放资源--------------");
    }
}
发布了59 篇原创文章 · 获赞 4 · 访问量 4516

猜你喜欢

转载自blog.csdn.net/weixin_45102492/article/details/103013452
今日推荐