【小白视角】大数据基础实践(四) 分布式数据库HBase的常用操作

本文涉及的产品
云原生大数据计算服务MaxCompute,500CU*H 100GB 3个月
简介: 目录1. 环境配置2. 操作步骤:2.1 环境搭建2.2 Hbase Shell2.3 Java Api3. 结论最后1. 环境配置⚫ 操作系统:Linux(建议 Ubuntu18.04);⚫ Hadoop 版本:3.1.3;⚫ JDK 版本:1.8;⚫ Java IDE:IDEA;⚫ Hadoop 伪分布式配置⚫ HBase1.1.5

目录

1. 环境配置

2. 操作步骤:

2.1 环境搭建

2.2 Hbase Shell

2.3 Java Api

3. 结论

最后

1. 环境配置

⚫ 操作系统:Linux(建议 Ubuntu18.04);

⚫ Hadoop 版本:3.1.3;

⚫ JDK 版本:1.8;

⚫ Java IDE:IDEA;

⚫ Hadoop 伪分布式配置

⚫ HBase1.1.5


2. 操作步骤:

2.1 环境搭建

解压压缩包

image.png


重命名并把权限赋予用户


image.png


配置环境变量

sudo vim ~/.bashrc

image.png

image.png

sudo vim /usr/local/hbase/conf/hbase-env.sh

image.png

sudo vim /usr/local/hbase/conf/hbase-site.xml

image.png


注意一点启动完hadoop之后才能启动hbase

image.png


进入shell

image.png


2.2 Hbase Shell

利用Hbase Shell命令完成以下任务,截图要求包含所执行的命令以及命令运行的结果:

表student_xxx:

image.png

表teacher_xxx

image.png

(1) 创建Hbase数据表student_xxx和teacher_xxx(表名称以姓名首字母结尾);


image.png



(2) 向student_xxx表中插入数据;

image.png


(3) 分别查看student_xxx表所有数据、指定时间戳、指定时间戳范围的数据;

所有数据

image.png


指定时间戳

image.png


指定时间戳范围

image.png


(4) 更改teacher_xxx表的username的VERSIONS>=6,并参考下面teacher表插入数据查看Hbase中所有表;


image.png

image.png

(5) 查看teacher_xxx表特定VERSIONS范围内的数据;


image.png

(6) 使用除ValueFilter以外的任意一个过滤器查看teacher_xxx表的值;

rowkey为20开头的值


image.png


(7) 删除Hbase表中的数据;


查看删除前


image.png


删除Sage


image.png

查看没有Sage了


image.png

(8) 删除Hbase中的表;

通过hbase shell删除一个表,首先需要将表禁用,然后再进行删除,命令如下:

disable 'tablename'
drop 'tablename'

image.png


检验是否存在


image.png

2.3 Java Api

利用Java API编程实现Hbase的相关操作,要求在实验报告中附上完整的源代码以及程序运行前后的Hbase表和数据的情况的截图:

导入所需要的jar包

image.png


callrecord_xxx表



(1) 创建Hbase中的数据表callrecord_xxx(表名称以姓名拼音首字母结尾);

   import org.apache.hadoop.conf.Configuration;
   import org.apache.hadoop.hbase.*;
   import org.apache.hadoop.hbase.client.Admin;
   import org.apache.hadoop.hbase.client.Connection;
   import org.apache.hadoop.hbase.client.ConnectionFactory;
   import org.apache.hadoop.hbase.HBaseConfiguration;
   import java.io.IOException;
   public class Create {
       public static Configuration configuration;
       public static Connection connection;
       public static Admin admin;
       //建立连接
       public static void init(){
           configuration  = HBaseConfiguration.create();
           configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");
           try{
               connection = ConnectionFactory.createConnection(configuration);
               admin = connection.getAdmin();
           }catch (IOException e){
               e.printStackTrace();
           }
       }
       //关闭连接
       public static void close(){
           try{
               if(admin != null){
                   admin.close();
               }
               if(null != connection){
                   connection.close();
               }
           }catch (IOException e){
               e.printStackTrace();
           }
       }
       public static void CreateTable(String tableName) throws IOException {
           if (admin.tableExists(TableName.valueOf(tableName))) {
               System.out.println("Table Exists!!!");
           }
           else{
               HTableDescriptor tableDesc = new HTableDescriptor(tableName);
               tableDesc.addFamily(new HColumnDescriptor("baseinfo"));
               tableDesc.addFamily(new HColumnDescriptor("baseinfo.calltime"));
               tableDesc.addFamily(new HColumnDescriptor("baseinfo.calltype"));
               tableDesc.addFamily(new HColumnDescriptor("baseinfo.phonebrand"));
               tableDesc.addFamily(new HColumnDescriptor("baseinfo.callplace"));
               tableDesc.addFamily(new HColumnDescriptor("baseinfo.callsecond"));
               admin.createTable(tableDesc);
               System.out.println("Create Table Successfully .");
           }
       }
       public static void main(String[] args) {
           String tableName = "callrecord_zqc";
           try {
               init();
               CreateTable(tableName);
               close();
           } catch (Exception e) {
               e.printStackTrace();
           }
       }
   }

image.png

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;
public class Insert {
    public static Configuration configuration;
    public static Connection connection;
    public static Admin admin;
    //建立连接
    public static void init(){
        configuration  = HBaseConfiguration.create();
        configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");
        try{
            connection = ConnectionFactory.createConnection(configuration);
            admin = connection.getAdmin();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    //关闭连接
    public static void close(){
        try{
            if(admin != null){
                admin.close();
            }
            if(null != connection){
                connection.close();
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    public static void InsertRow(String tableName, String rowKey, String colFamily, String col, String val) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        Put put = new Put(rowKey.getBytes());
        put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
        System.out.println("Insert Data Successfully");
        table.put(put);
        table.close();
    }
    public static void main(String[] args) {
        String tableName = "callrecord_zqc";
        String[] RowKeys = {
                "16920210616-20210616-1",
                "18820210616-20210616-1",
                "16920210616-20210616-2",
                "16901236367-20210614-1",
                "16920210616-20210614-1",
                "16901236367-20210614-2",
                "16920210616-20210614-2",
                "17720210616-20210614-1",
        };
        String[] CallTimes = {
                "2021-06-16 14:12:16",
                "2021-06-16 14:13:16",
                "2021-06-16 14:23:16",
                "2021-06-14 09:13:16",
                "2021-06-14 10:23:16",
                "2021-06-14 11:13:16",
                "2021-06-14 12:23:16",
                "2021-06-14 16:23:16",
        };
        String[] CallTypes = {
                "call",
                "call",
                "called",
                "call",
                "called",
                "call",
                "called",
                "called",
        };
        String[] PhoneBrands = {
                "vivo",
                "Huawei",
                "Vivo",
                "Huawei",
                "Vivo",
                "Huawei",
                "Vivo",
                "Oppo",
        };
        String[] CallPlaces = {
                "Fuzhou",
                "Fuzhou",
                "Fuzhou",
                "Fuzhou",
                "Fuzhou",
                "Fuzhou",
                "Fuzhou",
                "Fuzhou",
        };
        String[] CallSeconds = {
                "66",
                "96",
                "136",
                "296",
                "16",
                "264",
                "616",
                "423",
        };
        try {
            init();
            int i = 0;
            while (i < RowKeys.length){
                InsertRow(tableName, RowKeys[i], "baseinfo", "calltime", CallTimes[i]);
                InsertRow(tableName, RowKeys[i], "baseinfo", "calltype", CallTypes[i]);
                InsertRow(tableName, RowKeys[i], "baseinfo", "phonebrand", PhoneBrands[i]);
                InsertRow(tableName, RowKeys[i], "baseinfo", "callplace", CallPlaces[i]);
                InsertRow(tableName, RowKeys[i], "baseinfo", "callsecond", CallSeconds[i]);
                i++;
            }
            close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

image.png

(3) 获取Hbase某张表的所有数据,并返回查询结果;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;
public class List {
   public static Configuration configuration;
   public static Connection connection;
   public static Admin admin;
   //建立连接
   public static void init(){
       configuration  = HBaseConfiguration.create();
       configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");
       try{
           connection = ConnectionFactory.createConnection(configuration);
           admin = connection.getAdmin();
       }catch (IOException e){
           e.printStackTrace();
       }
   }
   //关闭连接
   public static void close(){
       try{
           if(admin != null){
               admin.close();
           }
           if(null != connection){
               connection.close();
           }
       }catch (IOException e){
           e.printStackTrace();
       }
   }
   public static void GetData(String tableName)throws  IOException{
       Table table = connection.getTable(TableName.valueOf(tableName));
       Scan scan = new Scan();
       ResultScanner scanner = table.getScanner(scan);
       for(Result result:scanner)
       {
           ShowCell((result));
       }
   }
   public static void ShowCell(Result result){
       Cell[] cells = result.rawCells();
       for(Cell cell:cells){
           System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
           System.out.println("Timetamp:"+cell.getTimestamp()+" ");
           System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
           System.out.println("column Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
           System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
           System.out.println();
       }
   }
   public static void main(String[] args) {
       String tableName = "callrecord_zqc";
       try {
           init();
           GetData(tableName);
           close();
       } catch (Exception e) {
           e.printStackTrace();
       }
   }
}

image.png

(4) 删除Hbase表中的某条或者某几条数据,并查看删除前后表中的数据情况;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;
public class DeleteData {
   public static Configuration configuration;
   public static Connection connection;
   public static Admin admin;
   //建立连接
   public static void init(){
       configuration  = HBaseConfiguration.create();
       configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");
       try{
           connection = ConnectionFactory.createConnection(configuration);
           admin = connection.getAdmin();
       }catch (IOException e){
           e.printStackTrace();
       }
   }
   //关闭连接
   public static void close(){
       try{
           if(admin != null){
               admin.close();
           }
           if(null != connection){
               connection.close();
           }
       }catch (IOException e){
           e.printStackTrace();
       }
   }
   public static void DeleteRow(String tableName,String rowKey) throws IOException {
       Table table = connection.getTable(TableName.valueOf(tableName));
       table.delete(new Delete(rowKey.getBytes()));
       System.out.println("Delete Data Successfully");
       table.close();
   }
   public static void main(String[] args) {
       String tableName = "callrecord_zqc";
       try {
           init();
           DeleteRow(tableName, "17720210616-20210614-1 ");
           close();
       } catch (Exception e) {
           e.printStackTrace();
       }
   }
}

image.png

(5) 实现给现有的表增加一个列族(如family_xxx),在hbase shell使用describe命令查看前后表的信息;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.HColumnDescriptor;
import java.io.IOException;
public class Append {
    public static Configuration configuration;
    public static Connection connection;
    public static Admin admin;
    //建立连接
    public static void init(){
        configuration  = HBaseConfiguration.create();
        configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");
        try{
            connection = ConnectionFactory.createConnection(configuration);
            admin = connection.getAdmin();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    //关闭连接
    public static void close(){
        try{
            if(admin != null){
                admin.close();
            }
            if(null != connection){
                connection.close();
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    public static void AddRow(String tableName, String rowName) throws IOException {
        TableName table = TableName.valueOf(tableName);
        admin.disableTable(table);          // 关闭表
        HTableDescriptor tableDesc = admin.getTableDescriptor(table);
        HColumnDescriptor family = new HColumnDescriptor(rowName);    //新增列族
        tableDesc.addFamily(family);
        admin.addColumn(table, family);
        admin.enableTableAsync(table);      //打开表
    }
    public static void main(String[] args) {
        String tableName = "callrecord_zqc";
        try {
            init();
            AddRow(tableName, "baseinfo.family_zqc");
            System.out.println("Append Successfully");
            close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

(6) 实现给新增加的列族按自增方式存放数据;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class AddItSelt {
    public static Configuration configuration;
    public static Connection connection;
    public static Admin admin;
    //建立连接
    public static void init(){
        configuration  = HBaseConfiguration.create();
        configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");
        try{
            connection = ConnectionFactory.createConnection(configuration);
            admin = connection.getAdmin();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    //关闭连接
    public static void close(){
        try{
            if(admin != null){
                admin.close();
            }
            if(null != connection){
                connection.close();
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    public static void Incr(String tableName, String rowKey, String colFamily, String col, int step) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        //incrementColumnValue(行号,列族,列,步长)
        table.incrementColumnValue(Bytes.toBytes(rowKey),Bytes.toBytes(colFamily), Bytes.toBytes(col),step);
        System.out.println("Incr Successfully");
        table.close();
    }
    public static void main(String[] args) {
        String tableName = "callrecord_zqc";
        try {
            init();
            Incr(tableName, "18820210616-20210616-1", "baseinfo", "family_zqc", 1);
            close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

image.png

(7) 删除表,并在Hbase Shell中使用命令查看删除前后hbase中所有表;

数据表(callrecord_xxx):

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import java.io.IOException;
public class DeleteTable {
    public static Configuration configuration;
    public static Connection connection;
    public static Admin admin;
    //建立连接
    public static void init(){
        configuration  = HBaseConfiguration.create();
        configuration.set("hbase.rootdir","hdfs://localhost:9000/hbase");
        try{
            connection = ConnectionFactory.createConnection(configuration);
            admin = connection.getAdmin();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    //关闭连接
    public static void close(){
        try{
            if(admin != null){
                admin.close();
            }
            if(null != connection){
                connection.close();
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    public static void DeleteTable(String tableName) throws IOException {
        TableName t = TableName.valueOf(tableName);
        if (admin.tableExists(t)) {
            admin.disableTable(t);
            admin.deleteTable(t);   // 先关闭才能删除
            System.out.println("table:"+tableName+"was deleted successfully");
        }
    }
    public static void main(String[] args) {
        String tableName = "callrecord_zqc";
        try {
            init();
            DeleteTable(tableName);
            close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

image.png

3. 结论

希望读者不要直接复制代码,代码可以直接复制,知识不能。想学好还是自己多推敲一下代码的结构流程。

最后

小生凡一,期待你的关注

相关实践学习
云数据库HBase版使用教程
&nbsp; 相关的阿里云产品:云数据库 HBase 版 面向大数据领域的一站式NoSQL服务,100%兼容开源HBase并深度扩展,支持海量数据下的实时存储、高并发吞吐、轻SQL分析、全文检索、时序时空查询等能力,是风控、推荐、广告、物联网、车联网、Feeds流、数据大屏等场景首选数据库,是为淘宝、支付宝、菜鸟等众多阿里核心业务提供关键支撑的数据库。 了解产品详情:&nbsp;https://cn.aliyun.com/product/hbase &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
1月前
|
人工智能 安全 应用服务中间件
阿里巴巴 MCP 分布式落地实践:快速转换 HSF 到 MCP server
本文分享了阿里巴巴内部将大规模HSF服务快速转换为MCP Server的实践经验,通过Higress网关实现MCP协议卸载,无需修改代码即可接入MCP生态。文章分析了MCP生态面临的挑战,如协议快速迭代和SDK不稳定性,并详细介绍了操作步骤及组件功能。强调MCP虽非终极解决方案,但作为AI业务工程化的起点具有重要意义。最后总结指出,MCP只是AI原生应用发展的第一步,未来还有更多可能性值得探索。
706 48
|
3月前
|
人工智能 前端开发 JavaScript
代码采纳率从 22% 到 33%,通义灵码辅助数据库智能编码实践
通义灵码本质上是一个AI agent,它已经进行了大量的优化。然而,为了更完美或有效地调用模型的潜在能力,我们在使用时仍需掌握一些技巧。通常,大多数人在使用通义灵码时会直接上手,这是 AI agent 的一个优势,即 zero shot 使用,无需任何上下文即可直接使用通义灵码的能力。
|
26天前
|
监控 Linux 应用服务中间件
Linux多节点多硬盘部署MinIO:分布式MinIO集群部署指南搭建高可用架构实践
通过以上步骤,已成功基于已有的 MinIO 服务,扩展为一个 MinIO 集群。该集群具有高可用性和容错性,适合生产环境使用。如果有任何问题,请检查日志或参考MinIO 官方文档。作者联系方式vx:2743642415。
273 56
|
20天前
|
安全 JavaScript 前端开发
HarmonyOS NEXT~HarmonyOS 语言仓颉:下一代分布式开发语言的技术解析与应用实践
HarmonyOS语言仓颉是华为专为HarmonyOS生态系统设计的新型编程语言,旨在解决分布式环境下的开发挑战。它以“编码创造”为理念,具备分布式原生、高性能与高效率、安全可靠三大核心特性。仓颉语言通过内置分布式能力简化跨设备开发,提供统一的编程模型和开发体验。文章从语言基础、关键特性、开发实践及未来展望四个方面剖析其技术优势,助力开发者掌握这一新兴工具,构建全场景分布式应用。
143 35
|
2月前
|
SQL 存储 分布式数据库
分布式存储数据恢复—hbase和hive数据库数据恢复案例
分布式存储数据恢复环境: 16台某品牌R730xd服务器节点,每台服务器节点上有数台虚拟机。 虚拟机上部署Hbase和Hive数据库。 分布式存储故障: 数据库底层文件被误删除,数据库不能使用。要求恢复hbase和hive数据库。
104 12
|
2月前
|
存储 负载均衡 测试技术
ACK Gateway with Inference Extension:优化多机分布式大模型推理服务实践
本文介绍了如何利用阿里云容器服务ACK推出的ACK Gateway with Inference Extension组件,在Kubernetes环境中为多机分布式部署的LLM推理服务提供智能路由和负载均衡能力。文章以部署和优化QwQ-32B模型为例,详细展示了从环境准备到性能测试的完整实践过程。
|
3月前
|
并行计算 PyTorch 算法框架/工具
融合AMD与NVIDIA GPU集群的MLOps:异构计算环境中的分布式训练架构实践
本文探讨了如何通过技术手段混合使用AMD与NVIDIA GPU集群以支持PyTorch分布式训练。面对CUDA与ROCm框架互操作性不足的问题,文章提出利用UCC和UCX等统一通信框架实现高效数据传输,并在异构Kubernetes集群中部署任务。通过解决轻度与强度异构环境下的挑战,如计算能力不平衡、内存容量差异及通信性能优化,文章展示了如何无需重构代码即可充分利用异构硬件资源。尽管存在RDMA验证不足、通信性能次优等局限性,但该方案为最大化GPU资源利用率、降低供应商锁定提供了可行路径。源代码已公开,供读者参考实践。
228 3
融合AMD与NVIDIA GPU集群的MLOps:异构计算环境中的分布式训练架构实践
|
3月前
|
人工智能 运维 监控
领先AI企业经验谈:探究AI分布式推理网络架构实践
当前,AI行业正处于快速发展的关键时期。继DeepSeek大放异彩之后,又一款备受瞩目的AI智能体产品Manus横空出世。Manus具备独立思考、规划和执行复杂任务的能力,其多智能体架构能够自主调用工具。在GAIA基准测试中,Manus的性能超越了OpenAI同层次的大模型,展现出卓越的技术实力。
|
3月前
|
数据库
|
5月前
|
数据采集 人工智能 分布式计算
MaxFrame:链接大数据与AI的高效分布式计算框架深度评测与实践!
阿里云推出的MaxFrame是链接大数据与AI的分布式Python计算框架,提供类似Pandas的操作接口和分布式处理能力。本文从部署、功能验证到实际场景全面评测MaxFrame,涵盖分布式Pandas操作、大语言模型数据预处理及企业级应用。结果显示,MaxFrame在处理大规模数据时性能显著提升,代码兼容性强,适合从数据清洗到训练数据生成的全链路场景...
228 5
MaxFrame:链接大数据与AI的高效分布式计算框架深度评测与实践!