Hbase Basics (3) - Hbase API

Table of contents

1. Preparation

2. Establish a connection

3. Create a table

4. Insert data

Five, view the specified data

6. Query all data

7. Delete specified data

Eight, delete the table


1. Preparation

创建maven工程,在pom.xml中导入依赖
内容如下
    <dependencies>
        <!-- hbase依赖 -->
        <!-- 单元测试依赖 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <!-- hbase客户端依赖 -->
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.2.1</version>
        </dependency>
        <!-- hbase核心依赖 -->
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-common</artifactId>
            <version>1.2.1</version>
        </dependency>
    </dependencies>

在resource中导入三个配置文件
hbase/conf/hbase-site.xml
hadoop-2.7.4/etc/hadoop/core-site.xml
hadoop-2.7.4/etc/hadoop/log4j.properties 

2. Establish a connection

// 初始化Configuration对象
    private Configuration configuration = null;

    // 初始化连接
    private Connection connection = null;

    // 获取HBase,与HBase建立连接
    @Before
    public void connectHbase() throws IOException {
        //获取Configuration对象
        configuration = HBaseConfiguration.create();

        // hbase客户端寻找hbase的读写数据不需要经过HMaster,只需要知道hbase所经过的Zppkeeper集群地址即可
        configuration.set("hbase.zookeeper.quorum", "hadoop01.bgd01, hadoop02.bgd01, hadoop03.bgd01");

        // 获取连接
        connection = ConnectionFactory.createConnection(configuration);
    }

3. Create a table

// 创建数据表
    @Test
    public void createTable() throws IOException {
        // 获取表的管理对象
        Admin admin = connection.getAdmin();

        // 创建表的描述对象,并指定表名
        HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf("t_user_info".getBytes()));

        // 构建第一个列族描述对象,并指定列族名
        HColumnDescriptor hcd1 = new HColumnDescriptor("base_info");

        // 构建第二个列族描述对象,并指定列族名
        HColumnDescriptor hcd2 = new HColumnDescriptor("extra_info");

        // 为列族设定一个版本数量,最小为1,最大为3
        hcd2.setVersions(1,3);

        // 将列族描述的对象添加到表描述中
        tableDescriptor.addFamily(hcd1).addFamily(hcd2);

        // 利用表管理器来创建表
        admin.createTable(tableDescriptor);

        // 关闭连接
        admin.close();
        connection.close();
    }

4. Insert data

// 插入数据
    @Test
    public void putData() throws IOException {
        // 创建table对象,调用该对象来添加数据
        Table table = connection.getTable(TableName.valueOf("t_user_info"));

        // 创建一个集合,用于存放Put对象
        ArrayList<Put> puts = new ArrayList<>();

        // 构建put对象(KV形式),并指定行键
        Put put01 = new Put(Bytes.toBytes("user001"));
        put01.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("zhangsan"));
        put01.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("password"), Bytes.toBytes("123456"));

        Put put02 = new Put("user002".getBytes());
        put02.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("username"), Bytes.toBytes("lisi"));
        put02.addColumn(Bytes.toBytes("extra_info"), Bytes.toBytes("married"), Bytes.toBytes("false"));

        // 把所有的put对象添加到一个集合中
        puts.add(put01);
        puts.add(put02);

        // 提交所有的插入数据的记录
        table.put(puts);

        // 关闭连接
        table.close();
        connection.close();
    }

Five, view the specified data

// 查看指定数据
    @Test
    public void getData() throws IOException {
        // 获取一个table对象
        Table table = connection.getTable(TableName.valueOf("t_user_info"));

        // 创建get查询参数对象,指定要获取的是哪一行
        Get get = new Get("user001".getBytes());

        // 返回查询结果的数据
        Result result = table.get(get);

        // 获取结果中的所有cell
        List<Cell> cells = result.listCells();

        // 遍历所有的cell
        for (Cell cell : cells) {
            // 获取行键
            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)));
        }

        // 关闭
        table.close();
        connection.close();
    }

6. Query all data

// 获取全部数据
    @Test
    public void scanData() throws IOException {
        // 获取table对象
        Table table = connection.getTable(TableName.valueOf("t_user_info"));

        // 创建scan对象
        Scan scan = new Scan();

        // 获取查询的数据
        ResultScanner scanner = table.getScanner(scan);

        // 获取ResultScanner所有数据,返回迭代器
        Iterator<Result> iterator = scanner.iterator();

        // 便利迭代器
        while (iterator.hasNext()) {
            // 获取当前每一行结果数据
            Result result = iterator.next();
            // 获取当前每一行中所有的cell对象
            List<Cell> cells = result.listCells();

            // 迭代所有的cell
            for (Cell cell : cells) {
                // 获取行键
                byte[] rowArray = cell.getRowArray();
                // 获取列族
                byte[] familyArray = cell.getFamilyArray();
                // 获取列族下的列名称
                byte[] qualifierArray = cell.getQualifierArray();
                // 列字段的值
                byte[] valueArray = cell.getValueArray();

                // 打印rowArray、familyArray、qualifierArray、valueArray
                System.out.println("行键:" + new String(rowArray, cell.getRowOffset(), cell.getRowLength()));
                System.out.println("列族:" + new String(familyArray, cell.getFamilyOffset(), cell.getFamilyLength()));
                System.out.println(" :" + "列:" + new String(qualifierArray, cell.getQualifierOffset(), cell.getQualifierLength()));
                System.out.println(" " + "值:" + new String(valueArray, cell.getValueOffset(), cell.getValueLength()));
            }
            System.out.println("--------------------");
        }
        // 关闭
    }

7. Delete specified data

//删除指定列的数据
    @Test
    public void deleteData() throws IOException {
        // 获取table对象
        Table table = connection.getTable(TableName.valueOf("t_user_info"));

        // 获取delete对象,需要一个rowkey
        Delete delete = new Delete("user001".getBytes());

        // 在delete对象中指定要删除的列族-列名称
        delete.addColumn("base_info".getBytes(), "password".getBytes());

        // 执行删除操作
        table.delete(delete);

        // 关闭连接
        table.close();
        connection.close();
    }

Eight, delete the table

//删除表
    @Test
    public void deleteTable() throws IOException {
        // 获取一个表的管理器
        Admin admin = connection.getAdmin();

        // 删除表时先需要禁用表(disable),然后再删除(delete)
        admin.disableTable(TableName.valueOf("t_user_info"));
        admin.deleteTable(TableName.valueOf("t_user_info"));

        // 关闭连接
        admin.close();
        connection.close();
    }

Guess you like

Origin blog.csdn.net/weixin_63507910/article/details/130067061