【大数据计算】(二) HBase 的安装和基础编程

简介: 目录1. 安装HBase1.1 下载安装文件1.2 配置环境变量1.3 添加用户权限1.4 查看HBase版本信息2. HBase的配置2.1 单机模式配置2.1.1 配置hbase-env.sh文件2.1.2 配置hbase-site.xml2.1.3 启动Hbase2.2 伪分布模式配置2.2.1 配置hbase-site.xml3. HBase常用的Shell命令3.1 在HBase中创建表3.2 添加数据3.3 查看数据3.4 删除数据3.5 删除表3.6 查询历史记录3.7 退出HBase数据库4. HBase编程实践4.1 编程题 API

目录

1. 安装HBase

1.1 下载安装文件

1.2 配置环境变量

1.3 添加用户权限

1.4 查看HBase版本信息

2. HBase的配置

2.1 单机模式配置

2.1.1 配置hbase-env.sh文件

2.1.2 配置hbase-site.xml

2.1.3 启动Hbase

2.2 伪分布模式配置

2.2.1 配置hbase-site.xml

3. HBase常用的Shell命令

3.1 在HBase中创建表

3.2 添加数据

3.3 查看数据

3.4 删除数据

3.5 删除表

3.6 查询历史记录

3.7 退出HBase数据库

4. HBase编程实践

4.1 编程题 API文档

4.1.1 第一题

4.1.2 第二题

5. 福利送书

最后

1. 安装HBase

1.1 下载安装文件

1.2 配置环境变量

进入环境中

sudo vim ~/.bashrc


添加箭头所指的两个路径


使得环境生效

source ~/.bashrc


1.3 添加用户权限

sudo chown -R zqc /usr/local/hbase


image.png


1.4 查看HBase版本信息

hbase version


image.png


2. HBase的配置

2.1 单机模式配置

2.1.1 配置hbase-env.sh文件

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


image.png


2.1.2 配置hbase-site.xml

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


2.1.3 启动Hbase

先启动hadoop

start-dfs.sh


再启动hbase

start-hbase.sh


jps出现下面这些即可

image.png


停止 HBase 运行


stop-hbase.sh


停止Hadoop的运行

stop-dfs.sh


2.2 伪分布模式配置

2.2.1 配置hbase-site.xml

image.png


3. HBase常用的Shell命令

3.1 在HBase中创建表

create 'student','Sname','Ssex','Sage','Sdept','course'


image.png


3.2 添加数据

put 'student','95001','Sname','LiYing'


image.png


put 'student','95001','Ssex','male'


image.png


3.3 查看数据

get 'student','95001'


image.png


3.4 删除数据

在 Hbase 中用 delete 以及 deleteall 命令进行删除数据操作

区别:

delete 用于删除一个单元格数据,是 put 的反向操作;

deleteall 用于删除一行数据


delete 'student','95001','Ssex'

image.png


3.5 删除表

删除表需要分两步操作

第一步先让该表不可用,第二步删除表。

比如,要删除student表,可以使用如下命令:


disable 'student'


drop 'student'


image.png


3.6 查询历史记录

先创建一个teacher表

create 'teacher',{NAME=>'username',VERSIONS=>5}


不断put数据

image.png

查询时,默认情况下回显示当前最新版本的数据,如果要查询历史数据,需要指定查询的历史版本数,由于上面设置了保存版本数为5,所以,在查询时制定的历史版本数的有效取值为1到5,具体命令如下:

get 'teacher','91001', {COLUMN=>'username',VERSIONS=>3}


下面是查询版本号为3的

image.png


3.7 退出HBase数据库

exit


image.png


4. HBase编程实践

在IDEA创建项目


为项目添加需要用到的JAR包


JAR包位于HBase安装目录下

如位于:/usr/local/hbase/lib目录下,单击界面中的Libraries选项卡,再单击界面右侧的Add External JARs按钮,选中/usr/local/hbase/lib目录下的所有JAR包,点击OK,继续添加JAR包,选中client-facing-thirdparty下的所有JAR文件,点击OK。


编写Java应用程序

如果程序里面示例网址“hdfs://localhost:9000/hbase”,运行时出错,可以把” localhost ”改成” master ”。


编译运行


4.1 编程题 API文档

4.1.1 第一题

image.png


利用4中的程序,创建上表:表scores的概念视图如上图所示


用学生的名字name作为行键(rowKey)

年级grade是一个只有一个列的列族

score是一个列族。

每一门课程都是score的一个列,如English、math、Chinese等。score 的列可以随时添加。

添加如下数据:

image.png

  • 创建表
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("grade"));
            tableDesc.addFamily(new HColumnDescriptor("score"));
            tableDesc.addFamily(new HColumnDescriptor("score.english"));
            tableDesc.addFamily(new HColumnDescriptor("score.math"));
            tableDesc.addFamily(new HColumnDescriptor("score.chinese"));
            admin.createTable(tableDesc);
            System.out.println("Create Table Successfully .");
        }
    }
    public static void main(String[] args) {
        String tableName = "scores_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 = "scores_zqc";
        String[] RowKeys = {
                "dandan",
                "sansan",
        };
        String[] Grades = {
                "6",
                "6",
        };
        String[] English = {
                "95",
                "87"
        };
        String[] Math = {
                "100",
                "95",
        };
        String[] Chinese = {
                "92",
                "98",
        };
        try {
            init();
            int i = 0;
            while (i < RowKeys.length){
                InsertRow(tableName, RowKeys[i], "grade", "", Grades[i]);
                InsertRow(tableName, RowKeys[i], "score", "english", English[i]);
                InsertRow(tableName, RowKeys[i], "score", "math", Math[i]);
                InsertRow(tableName, RowKeys[i], "score", "chinese", Chinese[i]);
                i++;
            } //031904102 zqc
            close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

image.png

hbase shell中查看数据

image.png


4.1.2 第二题

创建并插入相关数据后,查看Hbase java api 文档

在ExampleForHBase 类中添加两个个函数分别实现一个rowKey 过滤器(RowFilter)以及一个单列值过滤器(SingleColumValueFilter);

之后通过这两个函数分别做如下查询:


插入成功

image.png

查询Zhangshan 的年级以及相关成绩,打印在控制台中并截图。


查询数学成绩低于100的所有人的名字,打印在控制台中并截图。


插入数据

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 InsertTwo {
    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 = "scores_zqc_two";
        String[] RowKeys = {
                "Leelei",
                "Dandan",
                "Sansan",
                "Zhanshan",
        };
        String[] Grades = {
                "6",
                "6",
                "6",
                "6",
        };
        String[] English = {
                "78",
                "95",
                "67",
                "66",
        };
        String[] Math = {
                "78",
                "95",
                "100",
                "66",
        };
        String[] Chinese = {
                "90",
                "92",
                "60",
                "66",
        };
        try {
            init();
            int i = 0;
            while (i < RowKeys.length){
                InsertRow(tableName, RowKeys[i], "grade", "", Grades[i]);
                InsertRow(tableName, RowKeys[i], "score", "english", English[i]);
                InsertRow(tableName, RowKeys[i], "score", "math", Math[i]);
                InsertRow(tableName, RowKeys[i], "score", "chinese", Chinese[i]);
                i++;
            }
            close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

image.png

  • 查询
import java.io.IOException;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
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.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.SubstringComparator;
import org.apache.hadoop.hbase.util.Bytes;
public class FindTwo {
    public static Configuration configuration;
    public static Connection connection;
    public static Admin admin;
    public static void init() {
        configuration = HBaseConfiguration.create();
        configuration.set("hbase.rootdir", "hdfs://192.168.0.108: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(connection !=null) {
                connection.close();
            }
        }catch(IOException e) {
            e.printStackTrace();
        }
    }
    public static void findRowFilter(String tablename,String rowkey) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tablename));
        Scan scan = new Scan();
        System.out.println("RowKey: "+rowkey);
        Filter filter = new RowFilter(CompareOp.EQUAL,new SubstringComparator(rowkey));
        scan.setFilter(filter);
        ResultScanner scanner = table.getScanner(scan);
        for (Result res : scanner) {
            List<Cell> cells = res.listCells();
            for (Cell cell : cells) {
                // 打印rowkey,family,qualifier,value
                System.out.println(Bytes.toString(CellUtil.cloneRow(cell))
                        + ": " + Bytes.toString(CellUtil.cloneFamily(cell))
                        + ": "+Bytes.toString(CellUtil.cloneQualifier(cell))
                        +": "+ Bytes.toString(CellUtil.cloneValue(cell)) );
            }
        }
        scanner.close();
    }
    public static void findSingleColumValueFilter(String tablename,String colFamily,String col) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tablename));
        Scan scan = new Scan();
        System.out.println("\nthe value of math in colFamily < 100:");
        Filter filter = new SingleColumnValueFilter(Bytes.toBytes(colFamily),
                Bytes.toBytes(col),
                CompareOp.GREATER,
                Bytes.toBytes("100"));
        scan.setFilter(filter);
        ResultScanner scanner = table.getScanner(scan);
        for (Result res : scanner) {
            System.out.println("name: "+Bytes.toString(res.getRow()));
        } 
        scanner.close();
    }
    public static void main(String[] args) throws IOException {
        init();
        findRowFilter("scores_zqc_two","Zhanshan");
        findSingleColumValueFilter("scores_zqc_two","score","math");
        close();
    }
}

image.png

5. 福利送书

image.png


【内容简介】


《Java多线程与大数据处理实战》对 Java 的多线程及主流大数据中间件对数据的处理进行了较为详细的讲解。

本书主要讲了Java的线程创建方法和线程的生命周期,方便我们管理多线程的线程组和线程池,设置线程的优先级,设置守护线程,学习多线程的并发、同步和异步操作,了解 Java 的多线程并发处理工具(如信号量、多线程计数器)等内容。

引入了 Spring Boot、Spring Batch、Quartz、Kafka 等大数据中间件。这为学习Java 多线程和大数据处理的读者提供了良好的参考。多线程和大数据的处理是许多开发岗位面试中容易被问到的知识点。

学好多线程的知识点,无论是对于日后的开发工作,还是正要前往一线开发岗位的面试准备,都是非常有用的。

本书既适合高等院校的计算机类专业的学生学习,也适合从事软件开发相关行业的初级和中级开发人员。

【评论区】和 【点赞区】 会抽一位粉丝送出这本书籍嗷~


当然如果没有中奖的话,可以到当当,京东北京大学出版社的自营店进行购买。


也可以关注我!每周都会送一本出去哒~


相关实践学习
云数据库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
相关文章
|
4月前
|
SQL 分布式计算 大数据
请问本地安装了大数据计算MaxCompute studio,如何验证联通性及基本DDL操作呢?
请问本地安装了大数据计算MaxCompute studio,如何验证联通性及基本DDL操作呢?
27 0
|
4月前
|
大数据 Docker 容器
大数据 安装指南-----利用docker
大数据 安装指南-----利用docker
42 0
|
1月前
|
存储 Java Linux
Linux安装HBase的详细教程及常用方法
Linux安装HBase的详细教程及常用方法
101 1
|
3月前
|
Java Shell 分布式数据库
HBase基础编程
HBase基础编程
41 0
|
4月前
|
Shell 分布式数据库 Apache
HBase 安装
HBase 安装
44 0
|
4月前
|
大数据 Python Windows
Python大数据之Python进阶(二)多任务编程-进程
Python大数据之Python进阶(二)多任务编程-进程
34 0
|
4月前
|
分布式计算 大数据 Hadoop
Python大数据之PySpark(二)PySpark安装
Python大数据之PySpark(二)PySpark安装
219 0
|
4月前
|
数据采集 搜索推荐 Java
【大数据实训】用Hbase模拟电影搜索引擎(四)
【大数据实训】用Hbase模拟电影搜索引擎(四)
53 1
|
4月前
|
NoSQL 物联网 大数据
【补充】助力工业物联网,工业大数据之AirFlow安装
【补充】助力工业物联网,工业大数据之AirFlow安装
59 1
|
4月前
|
物联网 大数据
助力工业物联网,工业大数据之服务域:安装主题分析实现【三十】
助力工业物联网,工业大数据之服务域:安装主题分析实现【三十】
22 0

热门文章

最新文章