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

本文涉及的产品
云原生大数据计算服务 MaxCompute,5000CU*H 100GB 3个月
云原生大数据计算服务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. 结论

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

最后

小生凡一,期待你的关注

相关实践学习
lindorm多模间数据无缝流转
展现了Lindorm多模融合能力——用kafka API写入,无缝流转在各引擎内进行数据存储和计算的实验。
云数据库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月前
|
关系型数据库 OLAP API
非“典型”向量数据库AnalyticDB PostgreSQL及RAG服务实践
本文介绍了非“典型”向量数据库AnalyticDB PostgreSQL及其RAG(检索增强生成)服务的实践应用。 AnalyticDB PostgreSQL不仅具备强大的数据分析能力,还支持向量查询、全文检索和结构化查询的融合,帮助企业高效构建和管理知识库。
97 19
|
1月前
|
缓存 NoSQL JavaScript
Vue.js应用结合Redis数据库:实践与优化
将Vue.js应用与Redis结合,可以实现高效的数据管理和快速响应的用户体验。通过合理的实践步骤和优化策略,可以充分发挥两者的优势,提高应用的性能和可靠性。希望本文能为您在实际开发中提供有价值的参考。
59 11
|
1月前
|
数据采集 人工智能 分布式计算
MaxFrame:链接大数据与AI的高效分布式计算框架深度评测与实践!
阿里云推出的MaxFrame是链接大数据与AI的分布式Python计算框架,提供类似Pandas的操作接口和分布式处理能力。本文从部署、功能验证到实际场景全面评测MaxFrame,涵盖分布式Pandas操作、大语言模型数据预处理及企业级应用。结果显示,MaxFrame在处理大规模数据时性能显著提升,代码兼容性强,适合从数据清洗到训练数据生成的全链路场景...
103 5
MaxFrame:链接大数据与AI的高效分布式计算框架深度评测与实践!
|
1月前
|
人工智能 分布式计算 大数据
MaxFrame 产品评测:大数据与AI融合的Python分布式计算框架
MaxFrame是阿里云MaxCompute推出的自研Python分布式计算框架,支持大规模数据处理与AI应用。它提供类似Pandas的API,简化开发流程,并兼容多种机器学习库,加速模型训练前的数据准备。MaxFrame融合大数据和AI,提升效率、促进协作、增强创新能力。尽管初次配置稍显复杂,但其强大的功能集、性能优化及开放性使其成为现代企业与研究机构的理想选择。未来有望进一步简化使用门槛并加强社区建设。
93 7
|
2月前
|
存储 人工智能 数据管理
|
1月前
|
存储 人工智能 数据管理
媒体声音|专访阿里云数据库周文超博士:AI就绪的智能数据平台设计思路
在生成式AI的浪潮中,数据的重要性日益凸显。大模型在实际业务场景的落地过程中,必须有海量数据的支撑:经过训练、推理和分析等一系列复杂的数据处理过程,才能最终产生业务价值。事实上,大模型本身就是数据处理后的产物,以数据驱动的决策与创新需要通过更智能的平台解决数据多模处理、实时分析等问题,这正是以阿里云为代表的企业推动 “Data+AI”融合战略的核心动因。
|
2月前
|
弹性计算 安全 关系型数据库
活动实践 | 自建数据库迁移到云数据库
通过阿里云RDS,用户可获得稳定、安全的企业级数据库服务,无需担心数据库管理与维护。该方案使用RDS确保数据库的可靠性、可用性和安全性,结合ECS和DTS服务,实现自建数据库平滑迁移到云端,支持WordPress等应用的快速部署与运行。通过一键部署模板,用户能迅速搭建ECS和RDS实例,完成数据迁移及应用上线,显著提升业务灵活性和效率。
|
2月前
|
运维 监控 Cloud Native
云原生之运维监控实践:使用 taosKeeper 与 TDinsight 实现对 时序数据库TDengine 服务的监测告警
在数字化转型的过程中,监控与告警功能的优化对保障系统的稳定运行至关重要。本篇文章是“2024,我想和 TDengine 谈谈”征文活动的三等奖作品之一,详细介绍了如何利用 TDengine、taosKeeper 和 TDinsight 实现对 TDengine 服务的状态监控与告警功能。作者通过容器化安装 TDengine 和 Grafana,演示了如何配置 Grafana 数据源、导入 TDinsight 仪表板、以及如何设置告警规则和通知策略。欢迎大家阅读。
74 0
|
3月前
|
机器学习/深度学习 分布式计算 算法
【大数据分析&机器学习】分布式机器学习
本文主要介绍分布式机器学习基础知识,并介绍主流的分布式机器学习框架,结合实例介绍一些机器学习算法。
595 5
|
3月前
|
关系型数据库 MySQL Linux
Linux环境下MySQL数据库自动定时备份实践
数据库备份是确保数据安全的重要措施。在Linux环境下,实现MySQL数据库的自动定时备份可以通过多种方式完成。本文将介绍如何使用`cron`定时任务和`mysqldump`工具来实现MySQL数据库的每日自动备份。
285 3

热门文章

最新文章