HBase Java API操作数据库

时间:2023-03-24 21:11:07


场景


在上面将开发环境搭建起来,要想操作操作数据库除了使用HBase Shell


还可以使用JAVA API对HBase进行操作。

注:

关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

HBase Java API 与HBase数据模型的对应关系

HBase Java API操作数据库

实现

1、首先在IDEA中新建Maven项目,pom文件中添加依赖

<dependencies>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>2.4.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-common</artifactId>
            <version>2.4.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-protocol</artifactId>
            <version>2.4.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-server</artifactId>
            <version>2.4.5</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

2、然后新建包,包下新建类,首先新建建立连接和关闭连接的方法

public static Configuration cfg = HBaseConfiguration.create();
    public static Connection conn;

    /**
     * 建立连接
     * @throws IOException
     */
    public static void getConn() throws IOException {
        cfg.set("hbase.zookeeper.quorum","192.168.148.128:2181,192.168.148.131:2181,192.168.148.132:2181");
        //创建数据库连接
        conn = ConnectionFactory.createConnection(cfg);
    }

    /**
     * 关闭连接
     * @throws IOException
     */
    public static void closeConn() throws IOException {
        conn.close();
    }

然后这里的zookeeper的配置参考上面配置HBase的hbase-site.xml中的配置。

这里配置的是master、slave1、slave2的ip,另外要在本地电脑hosts文件中加上集群节点的映射。

HBase Java API操作数据库

3、获取所有表,新建方法

/**
     * 列出所有的表
     * @throws IOException
     */
    public static void listAllTable() throws IOException {
        //Admin 用于管理HBase数据库的表信息
        Admin admin = conn.getAdmin();
        for(TableName name : admin.listTableNames())
        {
            System.out.println(name);
        }
    }

然后在main方法中调用

public static void main(String[] args) throws IOException {
        getConn();
        listAllTable();
        closeConn();
    }

HBase Java API操作数据库

注意:

org.apache.hadoop.hbase.client.Admin是为管理HBase而提供的接口,在Connection

实例调用getAdmin()和close()方法期间有效。使用Admin接口可以实现的主要

HBase Shell命令包括create, list, drop, enable, disable, alter,相应方法如下所示。

HBase Java API操作数据库

4、创建表方法

/*
    创建表
     */
    public static void createTable(String tableName,String ...familyNames) throws IOException {
        //Admin 用于管理HBase数据库的表信息
        Admin admin = conn.getAdmin();
        //表名
        TableName tn = TableName.valueOf(tableName);
        //如果表已经存在
        if(admin.tableExists(tn))
        {
            //先使表无效
            admin.disableTable(tn);
            //删除表
            admin.deleteTable(tn);
        }
        //HTableDescriptor 包含了表的名字及其对应的列族
        HTableDescriptor hTableDescriptor = new HTableDescriptor(tn);
        for(String family:familyNames)
        {
            hTableDescriptor.addFamily(new HColumnDescriptor(family));
        }
        admin.createTable(hTableDescriptor);
        admin.close();
    }

HBase Java API操作数据库

5、查看表结构方法

/*
    查看表结构
     */
    public static void describe(String tableName) throws IOException {
        Admin admin = conn.getAdmin();
        TableName tn = TableName.valueOf(tableName);
        HTableDescriptor hTableDescriptor = admin.getTableDescriptor(tn);
        for(HColumnDescriptor hcd :hTableDescriptor.getColumnFamilies())
        {
            System.out.println(hcd.getNameAsString());
        }
    }

HBase Java API操作数据库

6、插入数据方法

/*
        插入数据
     */
    public static void putdata() throws IOException {
        cfg.set("hbase.zookeeper.quorum","192.168.148.128:2181,192.168.148.131:2181,192.168.148.132:2181");
        //创建数据库连接
        conn = ConnectionFactory.createConnection(cfg);
        Table tb = conn.getTable(TableName.valueOf("scores"));
        Put p = new Put(Bytes.toBytes("zhangsan"));
        p.addColumn(Bytes.toBytes("grade"),Bytes.toBytes(""),Bytes.toBytes("3"));
        p.addColumn(Bytes.toBytes("grade"),Bytes.toBytes("math"),Bytes.toBytes("58"));
        p.addColumn(Bytes.toBytes("course"),Bytes.toBytes("art"),Bytes.toBytes("98"));
        tb.put(p);
    }

HBase Java API操作数据库

 

注意:

Table接口用于和HBase中的表进行通信,代表了该表的实例,使用Connection的

getTable(TableName tableName)方法可以获取该接口的实例,用于获取、添加、删除、扫描HBase表中的数据。

Table接口包含的主要方法如下

HBase Java API操作数据库

7、获取数据方法

/*
    获取数据
    */
    public static void getdata() throws IOException {
        cfg.set("hbase.zookeeper.quorum","192.168.148.128:2181,192.168.148.131:2181,192.168.148.132:2181");
        //创建数据库连接
        conn = ConnectionFactory.createConnection(cfg);
        Table tb = conn.getTable(TableName.valueOf("scores"));
        Get get = new Get(Bytes.toBytes("zhangsan"));
        //get.addFamily(Bytes.toBytes("grade"));
        //get.addColumn(Bytes.toBytes("grade"),Bytes.toBytes("math"));
        Result result = tb.get(get);
        System.out.println(String.format("resule.value=%s,resule.toString():%s",Bytes.toString(result.value()),result));
    }

注意:

按行键获取

Get get = new Get(Bytes.toBytes("zhangsan"));

HBase Java API操作数据库

按行键、列族获取

Get get = new Get(Bytes.toBytes("zhangsan"));
get.addFamily(Bytes.toBytes("grade"));

HBase Java API操作数据库


按行键、列族、列修饰符获取

Get get = new Get(Bytes.toBytes("zhangsan"));
get.addColumn(Bytes.toBytes("grade"),Bytes.toBytes("math"));

HBase Java API操作数据库