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

1. 环境配置

⚫ 操作系统:Linux(建议 Ubuntu18.04);
⚫ Hadoop 版本:3.1.3;
⚫ JDK 版本:1.8;
⚫ Java IDE:IDEA;
⚫ Hadoop 伪分布式配置
⚫ HBase1.1.5

2. 操作步骤:

2.1 环境搭建

  1. 解压压缩包
    在这里插入图片描述

  2. 重命名并把权限赋予用户
    在这里插入图片描述
    在这里插入图片描述

  3. 配置环境变量

sudo vim ~/.bashrc在这里插入图片描述
在这里插入图片描述

sudo vim /usr/local/hbase/conf/hbase-env.sh
在这里插入图片描述
sudo vim /usr/local/hbase/conf/hbase-site.xml
在这里插入图片描述

  1. 注意一点启动完hadoop之后才能启动hbase
    在这里插入图片描述

  2. 进入shell
    在这里插入图片描述

2.2 Hbase Shell

利用Hbase Shell命令完成以下任务,截图要求包含所执行的命令以及命令运行的结果:
表student_xxx:
在这里插入图片描述
表teacher_xxx
在这里插入图片描述
(1) 创建Hbase数据表student_xxx和teacher_xxx(表名称以姓名首字母结尾);
在这里插入图片描述

在这里插入图片描述

(2) 向student_xxx表中插入数据;
在这里插入图片描述

(3) 分别查看student_xxx表所有数据、指定时间戳、指定时间戳范围的数据;
所有数据
在这里插入图片描述

指定时间戳
在这里插入图片描述

指定时间戳范围
在这里插入图片描述

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

(5) 查看teacher_xxx表特定VERSIONS范围内的数据;
在这里插入图片描述

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

在这里插入图片描述

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

  • 查看删除前
    在这里插入图片描述

  • 删除Sage
    在这里插入图片描述

  • 查看没有Sage了
    在这里插入图片描述

(8) 删除Hbase中的表;
通过hbase shell删除一个表,首先需要将表禁用,然后再进行删除,命令如下:

disable 'tablename'
drop 'tablename'

在这里插入图片描述

检验是否存在
在这里插入图片描述

2.3 Java Api

利用Java API编程实现Hbase的相关操作,要求在实验报告中附上完整的源代码以及程序运行前后的Hbase表和数据的情况的截图:
导入所需要的jar包
在这里插入图片描述
在这里插入图片描述
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();
           }
       }
   }


在这里插入图片描述

(2) 向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 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();
        }
    }
}

在这里插入图片描述

(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();

       }
   }
}

在这里插入图片描述

(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();
       }
   }
}

在这里插入图片描述

(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();

        }
    }
}

在这里插入图片描述

(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();

        }
    }
}

在这里插入图片描述

3. 结论

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

最后

小生凡一,期待你的关注

  • 43
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 42
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 42
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小生凡一

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值