Hbase Java API接口

实验环境

Hadoop2.8.0
Hbase-1.4.9
Centos7.2

Java代码

package pers.haohan.bigdata.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;

public class TestHbaseAPI_1 {

    public static void main(String[] args) throws IOException {

        // 通过java代码访问java数据库

        // 1. 创建配置对象,获取hbase连接
        Configuration conf = HBaseConfiguration.create();

        // 2. 获取hbase连接对象
        // classLoader : Thread.currentThread.getContextClassLoader  获取资源配置文件
        // classpath   : hbase-default.xml, hbase-site.xml

        Connection connection = ConnectionFactory.createConnection(conf);
        System.out.println(connection);

        // 3. 获取操作对象 : Admin
        // Admin admin = new HBaseAdmin(connection);
        Admin admin = connection.getAdmin();

        // 4. 操作数据库: 判断数据库中是否存在某张表
        // 4-1. 判断命名空间

        try {
            NamespaceDescriptor namespace = admin.getNamespaceDescriptor("test");

        } catch (NamespaceNotFoundException e) {
            NamespaceDescriptor nd = NamespaceDescriptor.create("test").build();

            admin.createNamespace(nd);
        }

        // 4-2. 判断hbase中是否存在某张表
        TableName tableName = TableName.valueOf("test:student");
        boolean flg = admin.tableExists(tableName);
        System.out.println(flg);

        if (flg) {

            // 获取指定的表对象
            Table table = connection.getTable(tableName);

            // 查询数据
            // DDL(create,drop, alter), DML(update, insert, delete), DQL(select)
            String rowkey = "1001";
            // string ==> byte[]

            // 字符编码
            Get get = new Get(Bytes.toBytes(rowkey));

            // 查询结果
            Result result = table.get(get);
            boolean empty = result.isEmpty();
            System.out.println("1001数据是否存在 = " + !empty);

            if (empty) {
                // 新增数据
                Put put = new Put(Bytes.toBytes(rowkey));

                String family = "info";
                String column = "name";
                String val = "zhangsan";

                put.addColumn(Bytes.toBytes(family), Bytes.toBytes(column), Bytes.toBytes(val));

                table.put(put);
                System.out.println("增加数据");
            } else {
                // 展示数据
                for (Cell cell : result.rawCells()) {

                    // 输出基本信息
                    // System.out.println("value = " + CellUtil.cloneValue(cell));
                    // value = [B@265adfad

                    System.out.println("value = " + Bytes.toString(CellUtil.cloneValue(cell)));
                    System.out.println("raw = " + Bytes.toString(CellUtil.cloneRow(cell)));
                    System.out.println("family = " + Bytes.toString(CellUtil.cloneFamily(cell)));
                    System.out.println("column = " + Bytes.toString(CellUtil.cloneQualifier(cell)));

                }
            }



        } else {
            // 创建表

            // 创建表描述对象
            HTableDescriptor td = new HTableDescriptor(tableName);

            // 增加列族
            HColumnDescriptor cd = new HColumnDescriptor("info");
            td.addFamily(cd);

            admin.createTable(td);

            System.out.println("表格创建成功");

        }
    }
}

XML文件


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>pers.haohan.bigdatagroupId>
    <artifactId>hbasestudyartifactId>
    <packaging>pompackaging>
    <version>1.0-SNAPSHOTversion>
    <modules>
        <module>hbase_mrmodule>
    modules>

    <dependencies>
        <dependency>
            <groupId>org.apache.hbasegroupId>
            <artifactId>hbase-clientartifactId>
            <version>1.3.1version>
        dependency>

        <dependency>
            <groupId>org.apache.hbasegroupId>
            <artifactId>hbase-serverartifactId>
            <version>1.3.1version>
        dependency>


    dependencies>


project>


你可能感兴趣的:(Hadoop)