HBase 表操作实战:创建、插入数据、查询和修改

本文将通过代码和截图演示如何使用 API 和 Shell 命令在 HBase 中创建 Stu_Class 表,插入数据,查询表中所有数据,修改数据,以及根据课程 ID 查询学生信息。

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

代码:

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.util.Bytes;
import org.apache.hadoop.hbase.client.TableDescriptor;
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;

public class CreateStuClassTable {

    private static final byte[] INFO_CF = Bytes.toBytes('class_info');

    public static void main(String[] args) throws Exception {
        Configuration conf = HBaseConfiguration.create();
        Connection conn = ConnectionFactory.createConnection(conf);
        Admin admin = conn.getAdmin();

        TableName tableName = TableName.valueOf('Stu_Class');

        if (admin.tableExists(tableName)) {
            System.out.println(tableName.getNameAsString() + ' already exists!');
        } else {
            TableDescriptorBuilder tableDescBuilder = TableDescriptorBuilder.newBuilder(tableName);

            ColumnFamilyDescriptor cfDesc = ColumnFamilyDescriptorBuilder.newBuilder(INFO_CF).build();
            tableDescBuilder.setColumnFamily(cfDesc);

            TableDescriptor tableDesc = tableDescBuilder.build();

            admin.createTable(tableDesc);

            System.out.println(tableName.getNameAsString() + ' created successfully!');
        }

        admin.close();
        conn.close();
    }
}

命令:

无需在 HBase shell 中执行任何命令。

结果截图:

image-20211123150035740

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

代码:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
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.client.Put;
import org.apache.hadoop.hbase.util.Bytes;

public class InsertDataToStuClassTable {

    private static final byte[] INFO_CF = Bytes.toBytes('class_info');
    private static final byte[] NAME_COL = Bytes.toBytes('name');
    private static final byte[] AGE_COL = Bytes.toBytes('age');
    private static final byte[] SEX_COL = Bytes.toBytes('sex');
    private static final byte[] CID_COL = Bytes.toBytes('cid');
    private static final byte[] CNAME_COL = Bytes.toBytes('cname');

    public static void main(String[] args) throws Exception {
        Configuration conf = HBaseConfiguration.create();
        Connection conn = ConnectionFactory.createConnection(conf);

        TableName tableName = TableName.valueOf('Stu_Class');

        Put put1 = new Put(Bytes.toBytes('1001'));
        put1.addColumn(INFO_CF, NAME_COL, Bytes.toBytes('Tom'));
        put1.addColumn(INFO_CF, AGE_COL, Bytes.toBytes('25'));
        put1.addColumn(INFO_CF, SEX_COL, Bytes.toBytes('male'));
        put1.addColumn(INFO_CF, CID_COL, Bytes.toBytes('3-105'));
        put1.addColumn(INFO_CF, CNAME_COL, Bytes.toBytes('计算机导论'));

        Put put2 = new Put(Bytes.toBytes('1002'));
        put2.addColumn(INFO_CF, NAME_COL, Bytes.toBytes('Arie'));
        put2.addColumn(INFO_CF, AGE_COL, Bytes.toBytes('23'));
        put2.addColumn(INFO_CF, SEX_COL, Bytes.toBytes('male'));
        put2.addColumn(INFO_CF, CID_COL, Bytes.toBytes('3-245'));
        put2.addColumn(INFO_CF, CNAME_COL, Bytes.toBytes('操作系统'));

        Put put3 = new Put(Bytes.toBytes('1003'));
        put3.addColumn(INFO_CF, NAME_COL, Bytes.toBytes('Jim'));
        put3.addColumn(INFO_CF, AGE_COL, Bytes.toBytes('17'));
        put3.addColumn(INFO_CF, SEX_COL, Bytes.toBytes('female'));
        put3.addColumn(INFO_CF, CID_COL, Bytes.toBytes('6-166'));
        put3.addColumn(INFO_CF, CNAME_COL, Bytes.toBytes('数字电路'));

        Put put4 = new Put(Bytes.toBytes('1004'));
        put4.addColumn(INFO_CF, NAME_COL, Bytes.toBytes('Lily'));
        put4.addColumn(INFO_CF, AGE_COL, Bytes.toBytes('21'));
        put4.addColumn(INFO_CF, SEX_COL, Bytes.toBytes('female'));
        put4.addColumn(INFO_CF, CID_COL, Bytes.toBytes('3-245'));
        put4.addColumn(INFO_CF, CNAME_COL, Bytes.toBytes('操作系统'));

        conn.getTable(tableName).put(put1);
        conn.getTable(tableName).put(put2);
        conn.getTable(tableName).put(put3);
        conn.getTable(tableName).put(put4);

        conn.close();
    }
}

命令:

无需在 HBase shell 中执行任何命令。

结果截图:

无。

3. 使用 Shell 命令查询表中所有数据

命令:

scan 'Stu_Class'

结果截图:

image-20211123151647345

4. 使用 Shell 将 1003 行的 cname 改为 网络技术,并查询 1003 行修改后的数据

命令:

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

结果截图:

image-20211123152306492

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

代码:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
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.client.Scan;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.BinaryComparator;
import org.apache.hadoop.hbase.util.Bytes;

public class QueryStuClassTable {

    private static final byte[] INFO_CF = Bytes.toBytes('class_info');
    private static final byte[] NAME_COL = Bytes.toBytes('name');
    private static final byte[] AGE_COL = Bytes.toBytes('age');
    private static final byte[] SEX_COL = Bytes.toBytes('sex');
    private static final byte[] CID_COL = Bytes.toBytes('cid');
    private static final byte[] CNAME_COL = Bytes.toBytes('cname');

    public static void main(String[] args) throws Exception {
        Configuration conf = HBaseConfiguration.create();
        Connection conn = ConnectionFactory.createConnection(conf);

        TableName tableName = TableName.valueOf('Stu_Class');

        Scan scan = new Scan();
        SingleColumnValueFilter filter = new SingleColumnValueFilter(
            INFO_CF,
            CID_COL,
            CompareFilter.CompareOp.EQUAL,
            new BinaryComparator(Bytes.toBytes('3-245'))
        );
        scan.setFilter(filter);

        ResultScanner scanner = conn.getTable(tableName).getScanner(scan);

        for (Result result : scanner) {
            String rowkey = Bytes.toString(result.getRow());
            String name = Bytes.toString(result.getValue(INFO_CF, NAME_COL));
            String age = Bytes.toString(result.getValue(INFO_CF, AGE_COL));
            String sex = Bytes.toString(result.getValue(INFO_CF, SEX_COL));
            String cid = Bytes.toString(result.getValue(INFO_CF, CID_COL));
            String cname = Bytes.toString(result.getValue(INFO_CF, CNAME_COL));

            System.out.println('rowkey: ' + rowkey + ', name: ' + name + ', age: ' + age + ', sex: ' + sex + ', cid: ' + cid + ', cname: ' + cname);
        }

        scanner.close();
        conn.close();
    }
}

命令:

无需在 HBase shell 中执行任何命令。

结果截图:

image-20211123153128706

HBase 表操作实战:创建、插入数据、查询和修改

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

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