• HBase第二章:API操作(二)


    系列文章目录

    HBase第一章:集群搭建
    HBase第二章:API操作(一)
    HBase第二章:API操作(二)



    前言

    上次博客我们记录了DDL的操作api,这次博客我们主要记录一下DML的操作api。


    一、创建类

    在这里插入图片描述

    二、代码编写

    1.源代码

    HBaseDML.java

    package com.atguigu;
    
    
    import org.apache.hadoop.hbase.Cell;
    import org.apache.hadoop.hbase.CellUtil;
    import org.apache.hadoop.hbase.CompareOperator;
    import org.apache.hadoop.hbase.TableName;
    import org.apache.hadoop.hbase.client.*;
    import org.apache.hadoop.hbase.filter.ColumnValueFilter;
    import org.apache.hadoop.hbase.filter.FilterList;
    import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
    import org.apache.hadoop.hbase.util.Bytes;
    
    import java.io.IOException;
    
    public class HBaseDML {
        //添加静态属性 connection 指向单例连接
        public static Connection connection=HBaseConnection.connection;
    
        /**
         * 插入数据
         * @param namespace 命名空间名称
         * @param tableName 表格名称
         * @param rowkey 主键
         * @param columFamily 列族名称
         * @param columnName 列名
         * @param value 值
         */
        public static void putCell(String namespace ,String tableName ,String rowkey ,String columFamily,String columnName ,String value) throws IOException {
            //1.获取table
            Table table = connection.getTable(TableName.valueOf(namespace, tableName));
    
            //2.调用相关方法插入数据
            //2.1创建put对象
            Put put = new Put(Bytes.toBytes(rowkey));
    
            //2.2给put对象添加数据
            put.addColumn(Bytes.toBytes(columFamily),Bytes.toBytes(columnName),Bytes.toBytes(value));
    
            //2.3将对象写入对应的方法
            try {
                table.put(put);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            //3.关闭table
            table.close();
        }
    
        /**
         * 读取数据 读取对应的一行中的某一列
         * @param namespace 命名空间名称
         * @param tableName 表格名称
         * @param rowkey 主键
         * @param columFamily 列族名称
         * @param columnName 列名
         */
        public static void getCells(String namespace ,String tableName ,String rowkey ,String columFamily,String columnName) throws IOException {
            //1.获取table
            Table table = connection.getTable(TableName.valueOf(namespace, tableName));
    
            //2.创建get对象
            Get get = new Get(Bytes.toBytes(rowkey));
    
            //如果直接调用 get 方法读取数据 此时读一整行数据
            //如果想读取某一列的数据 需要添加对应的参数
            get.addColumn(Bytes.toBytes(columFamily),Bytes.toBytes(columnName));
            //设置读取数据的版本(默认最新版)
            get.readAllVersions();
    
            try {
                //读取数据 得到 result 对象
                Result result = table.get(get);
    
                //处理数据
                Cell[] cells = result.rawCells();
    
                //测试方法: 直接把读取的数据打印到控制台
                //如果是实际开发 需要再额外写方法 对应处理数据
                for (Cell cell : cells) {
                    String value = new String(CellUtil.cloneValue(cell));
                    System.out.println(value);
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
    
            //3.关闭table
            table.close();
        }
    
        /**
         * 扫描数据
         * @param namespace 命名空间
         * @param tableName 表格名称
         * @param startRow 开始的row 包含的
         * @param stopRow 结束的row 不包含
         */
        public static void scanRows(String namespace ,String tableName ,String startRow,String stopRow) throws IOException {
            //1.获取table
            Table table = connection.getTable(TableName.valueOf(namespace, tableName));
    
            //创建scan对象
            Scan scan = new Scan();
    
            // 如果此时直接调用 会直接扫描整张表
            // 添加参数 来控制扫描的数据
    
            // 起始行(默认包含)
            scan.withStartRow(Bytes.toBytes(startRow));
            // 结束行(默认不包含)
            scan.withStopRow(Bytes.toBytes(stopRow));
    
            try {
                // 读取多行数据,获取scanner
                ResultScanner scanner = table.getScanner(scan);
    
                // result 来记录一行数据 cell 数组
                // ResultScanner 来记录多行数据 result 的数组
                for (Result result : scanner) {
                    Cell[] cells = result.rawCells();
                    for (Cell cell : cells) {
                        System.out.print(new String(CellUtil.cloneRow(cell)) + "-" +
                                new String(CellUtil.cloneFamily(cell)) + "-" +
                                new String(CellUtil.cloneQualifier(cell)) + "-" +
                                new String(CellUtil.cloneValue(cell)) + "\t");
                    }
                    System.out.println();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            //3.关闭table
            table.close();
        }
    
        /**
         * 带过滤的扫描
         * @param namespace 命名空间
         * @param tableName 表格名称
         * @param startRow 开始row
         * @param stopRow 结束 row
         * @param columFamily 列族名称
         * @param columnName 列名
         * @param value value值
         */
        public static void filterScan(String namespace ,String tableName ,String startRow,String stopRow,String columFamily,String columnName ,String value) throws IOException {
            // 1. 获取 table
            Table table = connection.getTable(TableName.valueOf(namespace, tableName));
            // 2. 创建 scan 对象
            Scan scan = new Scan();
    
            // 如果此时直接调用 会直接扫描整张表
            // 添加参数 来控制扫描的数据
    
            // 起始行(默认包含)
            scan.withStartRow(Bytes.toBytes(startRow));
            // 结束行(默认不包含)
            scan.withStopRow(Bytes.toBytes(stopRow));
    
            // 可以添加多个过滤
            FilterList filterList = new FilterList();
    
            // 创建过滤器
            // (1) 结果只保留当前列的数据
            ColumnValueFilter columnValueFilter = new ColumnValueFilter(
                    Bytes.toBytes(columFamily),
                    Bytes.toBytes(columnName),
                    CompareOperator.EQUAL,
                    Bytes.toBytes(value)
            );
    
    
            // (2) 结果保留整行数据
            // 结果同时会保留没有当前列的数据
            SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(
                    Bytes.toBytes(columFamily),
                    Bytes.toBytes(columnName),
                    CompareOperator.EQUAL,
                    Bytes.toBytes(value)
            );
    
            // 本身可以添加多个过滤器
            filterList.addFilter(singleColumnValueFilter);
    
            // 添加过滤
            scan.setFilter(filterList);
    
            try {
                // 读取多行数据 获得 scanner
                ResultScanner scanner = table.getScanner(scan);
                // result 来记录一行数据 cell 数组
                // ResultScanner 来记录多行数据 result 的数组
                for (Result result : scanner) {
                    Cell[] cells = result.rawCells();
                    for (Cell cell : cells) {
                        System.out.print(new String(CellUtil.cloneRow(cell)) + "-" +
                                new String(CellUtil.cloneFamily(cell)) + "-" +
                                new String(CellUtil.cloneQualifier(cell)) + "-" +
                                new String(CellUtil.cloneValue(cell)) + "\t");
                    }
                    System.out.println();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            //3.关闭table
            table.close();
        }
    
        /**
         * 删除一行中的一列数据
         * @param namespace 命名空间
         * @param tableName 表格名称
         * @param rowkey 主键
         * @param columFamily 列族名称
         * @param columnName 列名
         */
        public static void deleteColum(String namespace ,String tableName,String rowkey,String columFamily,String columnName) throws IOException {
            // 1.获取 table
            Table table = connection.getTable(TableName.valueOf(namespace, tableName));
    
            // 2.创建 Delete 对象
            Delete delete = new Delete(Bytes.toBytes(rowkey));
    
    
            // 3.添加删除信息
            // 3.1 删除单个版本
            delete.addColumn(Bytes.toBytes(columFamily),Bytes.toBytes(columnName));
            // 3.2 删除所有版本
            delete.addColumns(Bytes.toBytes(columFamily),Bytes.toBytes(columnName));
    
            try {
                //4.删除数据
                table.delete(delete);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
    
            //5.关闭资源
            table.close();
        }
    
    
        public static void main(String[] args) throws IOException {
    //        putCell("atguigu","bigdata","2021","info","name","zhangsan");
    //        putCell("atguigu","bigdata","2021","name","age","20");
    //        putCell("atguigu","bigdata","2021","name","name","wangwu");
    //        putCell("atguigu","bigdata","2022","info","age","22");
    //        putCell("atguigu","bigdata","2022","info","name","dahai");
    //        putCell("atguigu","bigdata","2022","name","name","songsong");
    
    
    //        getCells("atguigu","bigdata","2021","info","name");
    
    //        scanRows("atguigu","bigdata","2021","2022");
    
    //        filterScan("atguigu","bigdata","2021","2022","info","name","zhangsan");
    
    //        deleteColum("atguigu","bigdata","2021","info","name");
    
            System.out.println("其它代码");
    
            HBaseConnection.closeConnection();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267

    2.api测试

    我们先用上次写的DDL随便创建一个表,命名空间等参数可以自定义。

    createTable("atguigu","bigdata","info","name");
    
    • 1

    describe 'atguigu:bigdata’查看信息。
    在这里插入图片描述
    scan 'atguigu:bigdata’查看详细信息

    在这里插入图片描述

    1.插入数据

    随便插点

    putCell("atguigu","bigdata","2021","info","name","zhangsan");
    putCell("atguigu","bigdata","2021","name","age","20");
    putCell("atguigu","bigdata","2021","name","name","wangwu");
    putCell("atguigu","bigdata","2022","info","age","22");
    putCell("atguigu","bigdata","2022","info","name","dahai");
    putCell("atguigu","bigdata","2022","name","name","songsong");
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    2.数据查询

    查询表中
    rowkey=“2021”
    columFamily=“info”
    columnName=“name”

            getCells("atguigu","bigdata","2021","info","name");
    
    • 1

    在这里插入图片描述在这里插入图片描述

    3.过滤查询

    查询rowkey=2021的数据

    scanRows("atguigu","bigdata","2021","2022");
    
    • 1

    在这里插入图片描述

    4.详细过滤

    filterScan("atguigu","bigdata","2021","2022","info","name","zhangsan");
    
    • 1

    在这里插入图片描述
    这里要解释一下这个查询结果,第一个就是我们要得结果,但是为什么会有后边两个结果呢。
    我们给过滤查询传入了四个参数,我们只会对含有这四个参数的值进行过滤,如果不完全包含这个参数则直接保留。

    5.删除数据

    deleteColum("atguigu","bigdata","2021","info","name");
    
    • 1

    在这里插入图片描述
    在这里插入图片描述


    总结

    hbase的学习还有一些进阶内容,但考虑到博主是初学,没有必要学习太深,所以hbase的内容就学习到这里。

  • 相关阅读:
    牛客网练习题(函数部分)
    winform车辆管理系统VS开发sqlserver数据库CS结构c#编程源码程序
    Android 实现禁止复制
    【系统架构设计】架构核心知识:4 系统可靠性分析与设计
    一口气拿下vue-router所有知识点,薪资暴涨3000
    Java进阶03 IO基础
    华为无线设备配置WMM和优先级映射
    Webpack 官网文字滚动特效
    vue3 + typescript + vite + naive ui + tailwindcss + jsx 仿苹果桌面系统
    springboot+vue练手级项目,真实的在线博客系统
  • 原文地址:https://blog.csdn.net/weixin_50835854/article/details/127452500