前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >在java中通过API调用HBase入门学习

在java中通过API调用HBase入门学习

作者头像
zhangheng
发布2020-12-01 10:45:56
1.2K0
发布2020-12-01 10:45:56
举报

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

hbase架构

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

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

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

hbase简单api调用

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

代码语言:javascript
复制
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();
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-11-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • hbase架构
  • hbase简单api调用
相关产品与服务
TDSQL MySQL 版
TDSQL MySQL 版(TDSQL for MySQL)是腾讯打造的一款分布式数据库产品,具备强一致高可用、全球部署架构、分布式水平扩展、高性能、企业级安全等特性,同时提供智能 DBA、自动化运营、监控告警等配套设施,为客户提供完整的分布式数据库解决方案。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档