HBase 表操作实战:使用 API 和 Shell 查询、修改数据

目标: 使用 NoSQL 语言(HBase)操作 Stu_Class 表,并进行数据查询、修改等操作。

数据结构:

Stu_Class
basic_info class_info
name age sex cid cname
1001 Tom 25 male 3-105 计算机导论
1002 Arie 23 male 3-245 操作系统
1003 Jim 17 female 6-166 数字电路
1004 Lily 21 female 3-245 操作系统

操作步骤:

  1. 使用 API 在 HBase 中创建 Stu_Class 表,并在 HBase shell 中展示所有的表。

    使用 Java API 创建表:

    Configuration conf = HBaseConfiguration.create();
    HBaseAdmin admin = new HBaseAdmin(conf);
    
    HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf('Stu_Class'));
    tableDesc.addFamily(new HColumnDescriptor('basic_info'));
    tableDesc.addFamily(new HColumnDescriptor('class_info'));
    
    admin.createTable(tableDesc);
    admin.close();
    

    在 HBase shell 中展示所有表:

    list
    

    结果如下:

    Stu_Class
    
  2. 使用 API 将数据插入到表中

    使用 Java API 插入数据:

    Configuration conf = HBaseConfiguration.create();
    HTable table = new HTable(conf, 'Stu_Class');
    
    Put put1 = new Put(Bytes.toBytes('1001'));
    put1.add(Bytes.toBytes('basic_info'), Bytes.toBytes('name'), Bytes.toBytes('Tom'));
    put1.add(Bytes.toBytes('basic_info'), Bytes.toBytes('age'), Bytes.toBytes('25'));
    put1.add(Bytes.toBytes('basic_info'), Bytes.toBytes('sex'), Bytes.toBytes('male'));
    put1.add(Bytes.toBytes('class_info'), Bytes.toBytes('cid'), Bytes.toBytes('3-105'));
    put1.add(Bytes.toBytes('class_info'), Bytes.toBytes('cname'), Bytes.toBytes('计算机导论'));
    table.put(put1);
    
    Put put2 = new Put(Bytes.toBytes('1002'));
    put2.add(Bytes.toBytes('basic_info'), Bytes.toBytes('name'), Bytes.toBytes('Arie'));
    put2.add(Bytes.toBytes('basic_info'), Bytes.toBytes('age'), Bytes.toBytes('23'));
    put2.add(Bytes.toBytes('basic_info'), Bytes.toBytes('sex'), Bytes.toBytes('male'));
    put2.add(Bytes.toBytes('class_info'), Bytes.toBytes('cid'), Bytes.toBytes('3-245'));
    put2.add(Bytes.toBytes('class_info'), Bytes.toBytes('cname'), Bytes.toBytes('操作系统'));
    table.put(put2);
    
    Put put3 = new Put(Bytes.toBytes('1003'));
    put3.add(Bytes.toBytes('basic_info'), Bytes.toBytes('name'), Bytes.toBytes('Jim'));
    put3.add(Bytes.toBytes('basic_info'), Bytes.toBytes('age'), Bytes.toBytes('17'));
    put3.add(Bytes.toBytes('basic_info'), Bytes.toBytes('sex'), Bytes.toBytes('female'));
    put3.add(Bytes.toBytes('class_info'), Bytes.toBytes('cid'), Bytes.toBytes('6-166'));
    put3.add(Bytes.toBytes('class_info'), Bytes.toBytes('cname'), Bytes.toBytes('数字电路'));
    table.put(put3);
    
    Put put4 = new Put(Bytes.toBytes('1004'));
    put4.add(Bytes.toBytes('basic_info'), Bytes.toBytes('name'), Bytes.toBytes('Lily'));
    put4.add(Bytes.toBytes('basic_info'), Bytes.toBytes('age'), Bytes.toBytes('21'));
    put4.add(Bytes.toBytes('basic_info'), Bytes.toBytes('sex'), Bytes.toBytes('female'));
    put4.add(Bytes.toBytes('class_info'), Bytes.toBytes('cid'), Bytes.toBytes('3-245'));
    put4.add(Bytes.toBytes('class_info'), Bytes.toBytes('cname'), Bytes.toBytes('操作系统'));
    table.put(put4);
    
    table.close();
    
  3. 使用 Shell 命令查询表中所有数据

    在 HBase shell 中查询所有数据:

    scan 'Stu_Class'
    

    结果如下:

    ROW             COLUMN+CELL
    1001           column=basic_info:age, timestamp=1632254404178, value=25
    1001           column=basic_info:name, timestamp=1632254404178, value=Tom
    1001           column=basic_info:sex, timestamp=1632254404178, value=male
    1001           column=class_info:cid, timestamp=1632254404178, value=3-105
    1001           column=class_info:cname, timestamp=1632254404178, value=计算机导论
    1002           column=basic_info:age, timestamp=1632254404178, value=23
    1002           column=basic_info:name, timestamp=1632254404178, value=Arie
    1002           column=basic_info:sex, timestamp=1632254404178, value=male
    1002           column=class_info:cid, timestamp=1632254404178, value=3-245
    1002           column=class_info:cname, timestamp=1632254404178, value=操作系统
    1003           column=basic_info:age, timestamp=1632254404178, value=17
    1003           column=basic_info:name, timestamp=1632254404178, value=Jim
    1003           column=basic_info:sex, timestamp=1632254404178, value=female
    1003           column=class_info:cid, timestamp=1632254404178, value=6-166
    1003           column=class_info:cname, timestamp=1632254404178, value=数字电路
    1004           column=basic_info:age, timestamp=1632254404178, value=21
    1004           column=basic_info:name, timestamp=1632254404178, value=Lily
    1004           column=basic_info:sex, timestamp=1632254404178, value=female
    1004           column=class_info:cid, timestamp=1632254404178, value=3-245
    1004           column=class_info:cname, timestamp=1632254404178, value=操作系统
    
  4. 使用 Shell 将 1003 行的 cname 改为 '网络技术',并查询 1003 行修改后的数据

    在 HBase shell 中修改数据:

    put 'Stu_Class', '1003', 'class_info:cname', '网络技术'
    

    在 HBase shell 中查询修改后的数据:

    get 'Stu_Class', '1003'
    

    结果如下:

    COLUMN        CELL
    basic_info:age timestamp=1632254404178, value=17
    basic_info:name timestamp=1632254404178, value=Jim
    basic_info:sex timestamp=1632254404178, value=female
    class_info:cid timestamp=1632254404178, value=6-166
    class_info:cname timestamp=1632255235245, value=网络技术
    1 row(s) in 0.0250 seconds
    
  5. 使用 API 查询选修课程为 3-245 的学生信息

    使用 Java API 查询选修课程为 3-245 的学生信息:

    Configuration conf = HBaseConfiguration.create();
    HTable table = new HTable(conf, 'Stu_Class');
    
    Scan scan = new Scan();
    scan.addColumn(Bytes.toBytes('basic_info'), Bytes.toBytes('name'));
    scan.addColumn(Bytes.toBytes('basic_info'), Bytes.toBytes('age'));
    scan.addColumn(Bytes.toBytes('basic_info'), Bytes.toBytes('sex'));
    scan.addColumn(Bytes.toBytes('class_info'), Bytes.toBytes('cid'));
    scan.addColumn(Bytes.toBytes('class_info'), Bytes.toBytes('cname'));
    scan.setFilter(new SingleColumnValueFilter(Bytes.toBytes('class_info'), Bytes.toBytes('cid'), CompareFilter.CompareOp.EQUAL, Bytes.toBytes('3-245')));
    
    ResultScanner scanner = table.getScanner(scan);
    for (Result result : scanner) {
        System.out.println(Bytes.toString(result.getValue(Bytes.toBytes('basic_info'), Bytes.toBytes('name'))) + '	' +
                Bytes.toString(result.getValue(Bytes.toBytes('basic_info'), Bytes.toBytes('age'))) + '	' +
                Bytes.toString(result.getValue(Bytes.toBytes('basic_info'), Bytes.toBytes('sex'))) + '	' +
                Bytes.toString(result.getValue(Bytes.toBytes('class_info'), Bytes.toBytes('cid'))) + '	' +
                Bytes.toString(result.getValue(Bytes.toBytes('class_info'), Bytes.toBytes('cname'))));
    }
    
    scanner.close();
    table.close();
    

    结果如下:

    Arie	23	male	3-245	操作系统
    Lily	21	female	3-245	操作系统
    

注意:

  • 代码中需引入 HBase 相关依赖包。
  • 以上操作需要提前启动 HBase 服务。
  • 请根据实际情况修改代码和命令中的表名、列名等信息。

通过本教程,您将掌握如何使用 API 和 Shell 操作 HBase 表,包括创建、插入、查询和修改数据等基本操作。这些技能将帮助您在实际项目中有效地使用 HBase 进行数据存储和管理。

HBase 表操作实战:使用 API 和 Shell 查询、修改数据

原文地址: https://www.cveoy.top/t/topic/owBf 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录