在之前的文章hdfs API学习中,我们已经能够成功连接hdfs,并对文件进行读写。hbase数据库的操作也非常简单,但你需要先大致了解一下hbase的架构。

hbase架构

hbase是基于列存储的nosql数据库,hbase官方参考指南中有很详细的使用说明。个人理解列存储的意思就是物理数据存储不是按行划分,而是按列划分。例如一个成绩表,所有人的高等数据成绩信息在底层存放在一个文件中,所有人的计算机成绩信息存放在底层的另一个文件中,如果你想要获取某人的高等数据成绩信息,那么只用输入某人姓名+高等数学列,数据库就会扫描高等数据成绩信息文件,检索出某人的高等数学成绩,而不会扫描计算机成绩信息文件。

实际上hbase的列存储指的是列族存储,也就是说一堆列组成一个物理存储文件。例如个人信息表,成绩信息可以作为一个列族,其中包含高等数据成绩、计算机成绩信息等列,个人健康信息可以作为一个列族,其中包含身高、体重等列。

如果你想获取一个人的体重信息,那么获取方式key就是个人信息表+个人健康信息列族+体重列(版本概念暂且不提)。

hbase简单api调用

hbase的功能相当丰富,运维也相对比较复杂,下面是对hbase的简单调用,仅供参考学习。如果想了解更多深入的内容,可以参考上边提到的官方参考指南。


package com.gavinzh.learn.hbase;

import com.google.common.collect.Lists;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author jiangmitiao
 */
public class HBaseLearnMain {
    private Connection connection;
    private Admin admin;

    public void init(String baseRootaddr) throws IOException {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("base.rootaddr", baseRootaddr);
        configuration.set("hbase.zookeeper.quorum", "192.168.10.103:2181");
        connection = ConnectionFactory.createConnection(configuration);
        admin = connection.getAdmin();
    }

    public void createTable(String tableNameStr, List<String> colFamilyList) throws IOException {
        TableName tableName = TableName.valueOf(tableNameStr);
        if (admin.tableExists(tableName)) {
            System.err.println(tableNameStr + "已存在");
            return;
        }
        List<ColumnFamilyDescriptor> list = colFamilyList.stream()
            .map(family -> ColumnFamilyDescriptorBuilder.newBuilder(family.getBytes()).build())
            .collect(Collectors.toList());
        TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(tableName)
            .setColumnFamilies(list)
            .build();

        admin.createTable(tableDescriptor);
    }

    public void insertData(String tableNameStr, String rowKey, String colFamily, String col, String value)
        throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableNameStr));
        Put put = new Put(rowKey.getBytes());
        put.addColumn(colFamily.getBytes(), col.getBytes(), value.getBytes());
        table.put(put);
    }

    public String getData(String tableNameStr, String rowKey, String colFamily, String col) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableNameStr));
        Get get = new Get(rowKey.getBytes());
        get.addColumn(colFamily.getBytes(), col.getBytes());
        Result result = table.get(get);
        return new String(result.getValue(colFamily.getBytes(), col.getBytes()));
    }

    public void dropTable(String tableNameStr) throws IOException {
        TableName tableName = TableName.valueOf(tableNameStr);
        if (!admin.tableExists(tableName)) {
            System.err.println(tableNameStr + "不存在");
            return;
        }
        if (!admin.isTableDisabled(tableName)) {
            admin.disableTable(tableName);
        }
        admin.deleteTable(tableName);
    }

    public void destroy() throws IOException {
        if (admin != null) {
            admin.close();
        }
        if (connection != null) {
            connection.close();
        }
    }

    public static void testXiaoming(HBaseLearnMain hBaseLearnMain) throws IOException {
        String tableNameStr = "tableTest1FromApi";
        String colFamily1 = "learn";
        String col = "English";
        String rowKey = "小明";
        hBaseLearnMain.insertData(tableNameStr, rowKey, colFamily1, col, "98");
        hBaseLearnMain.insertData(tableNameStr, rowKey, colFamily1, col, "65");

        String data = hBaseLearnMain.getData(tableNameStr, rowKey, colFamily1, col);
        System.out.println("小明英语成绩:" + data);
    }

    public static void testXiaohong(HBaseLearnMain hBaseLearnMain) throws IOException {
        String tableNameStr = "tableTest1FromApi";
        String colFamily1 = "body";
        String col = "height";
        String rowKey = "小红";
        hBaseLearnMain.insertData(tableNameStr, rowKey, colFamily1, col, "131");
        hBaseLearnMain.insertData(tableNameStr, rowKey, colFamily1, col, "165");

        String data = hBaseLearnMain.getData(tableNameStr, rowKey, colFamily1, col);
        System.out.println("小红身高:" + data);
    }

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

        HBaseLearnMain hBaseLearnMain = new HBaseLearnMain();
        hBaseLearnMain.init("hdfs://hadoop1/hbase");

        String tableNameStr = "tableTest1FromApi";
        List<String> colFamilyList = Lists.newArrayList("learn", "body");
        hBaseLearnMain.createTable(tableNameStr, colFamilyList);

        testXiaoming(hBaseLearnMain);
        testXiaohong(hBaseLearnMain);

        //hBaseLearnMain.dropTable(tableNameStr);

        hBaseLearnMain.destroy();
    }
}

标签: java, hadoop, hbase

添加新评论