首页 > 其他分享 >熟悉常用的HBase操作02(问题)

熟悉常用的HBase操作02(问题)

时间:2024-12-26 14:55:22浏览次数:4  
标签:02 String admin Bytes tableName static 熟悉 table HBase

实验内容与完成情况:

(一)编程实现以下指定功能,并用Hadoop提供的HBase Shell命令完成相同任务:

  HBase Shell运行截图:

(1) 列出HBase所有的表的相关信息,例如表名;

 

(2) 在终端打印出指定的表的所有记录数据;

 

(3) 向已经创建好的表添加和删除指定的列族或列;

 

(4) 清空指定的表的所有记录数据;

 

(5) 统计表的行数。

 

编程运行结果:

 

   三个代码的配置文件都是HBseConfig:

 

  代码:

package hbase;

 

import hbase.HBaseConfig;

import org.apache.hadoop.hbase.client.*;

import org.apache.hadoop.hbase.*;

import org.apache.hadoop.hbase.util.Bytes;

 

import java.io.IOException;

 

public class HBaseOperations {

    private static Connection connection;

    private static Admin admin;

 

    static {

        try {

            connection = ConnectionFactory.createConnection(HBaseConfig.getConfig());

            admin = connection.getAdmin();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

 

    // (1) 列出所有表信息

    public static void listTables() throws IOException {

        HTableDescriptor[] tableDescriptors = admin.listTables();

        System.out.println("HBase 表列表:");

        for (HTableDescriptor table : tableDescriptors) {

            System.out.println(" - " + table.getTableName());

        }

    }

 

    // (2) 打印指定表的所有记录

    public static void printTable(String tableName) throws IOException {

        Table table = connection.getTable(TableName.valueOf(tableName));

        Scan scan = new Scan();

        ResultScanner scanner = table.getScanner(scan);

 

        for (Result result : scanner) {

            System.out.println("行: " + Bytes.toString(result.getRow()));

            for (Cell cell : result.rawCells()) {

                System.out.println(" - 列: " + Bytes.toString(CellUtil.cloneFamily(cell)) + ":" +

                        Bytes.toString(CellUtil.cloneQualifier(cell)) +

                        ", 值: " + Bytes.toString(CellUtil.cloneValue(cell)));

            }

        }

        table.close();

    }

 

    // (3) 添加和删除列族

    public static void modifyColumnFamily(String tableName, String columnFamily, boolean add) throws IOException {

        TableName table = TableName.valueOf(tableName);

        if (!admin.tableExists(table)) {

            System.out.println("表不存在.");

            return;

        }

 

        if (add) {

            HColumnDescriptor columnDescriptor = new HColumnDescriptor(columnFamily);

            admin.addColumn(table, columnDescriptor);

            System.out.println("已添加列族: " + columnFamily);

        } else {

            admin.deleteColumn(table, Bytes.toBytes(columnFamily));

            System.out.println("已删除列族: " + columnFamily);

        }

    }

 

    // (4) 清空表中的所有记录

    public static void truncateTable(String tableName) throws IOException {

        TableName table = TableName.valueOf(tableName);

        if (admin.tableExists(table)) {

            admin.disableTable(table);

            admin.truncateTable(table, true);

            System.out.println("表 " + tableName + " 已清空.");

        } else {

            System.out.println("表不存在.");

        }

    }

 

    // (5) 统计表的行数

    public static void countRows(String tableName) throws IOException {

        Table table = connection.getTable(TableName.valueOf(tableName));

        Scan scan = new Scan();

        ResultScanner scanner = table.getScanner(scan);

 

        int rowCount = 0;

        for (Result ignored : scanner) {

            rowCount++;

        }

        System.out.println("表 " + tableName + " 总行数: " + rowCount);

        table.close();

    }

 

    // 关闭连接

    public static void closeConnection() {

        try {

            if (admin != null) admin.close();

            if (connection != null) connection.close();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

 

    public static void main(String[] args) {

        try {

            // 测试功能

            String tableName = "Student";

            listTables();

            printTable(tableName);

            modifyColumnFamily(tableName, "列族", true);

            modifyColumnFamily(tableName, "列族2", true);

            modifyColumnFamily(tableName, "列族", false);

            truncateTable(tableName);

            countRows(tableName);

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            closeConnection();

        }

    }

}

 

(二)HBase数据库操作

1. 现有以下关系型数据库中的表和数据(见表14-3到表14-5),要求将其转换为适合于HBase存储的表并插入数据:

表14-3 学生表(Student)

学号(S_No)

姓名(S_Name)

性别(S_Sex)

年龄(S_Age)

2015001

Zhangsan

male

23

2015002

Mary

female

22

2015003

Lisi

male

24

 

表14-4 课程表(Course)

课程号(C_No)

课程名(C_Name)

学分(C_Credit)

123001

Math

2.0

123002

Computer Science

5.0

123003

English

3.0

 

表14-5 选课表(SC)

学号(SC_Sno)

课程号(SC_Cno)

成绩(SC_Score)

2015001

123001

86

2015001

123003

69

2015002

123002

77

2015002

123003

99

2015003

123001

98

2015003

123002

95

 

 

 

运行截图:

 

 

代码:

package hbase;

 

import org.apache.hadoop.hbase.client.*;

import org.apache.hadoop.hbase.*;

import org.apache.hadoop.hbase.util.Bytes;

 

import java.io.IOException;

 

public class HBaseOperations1 {

    private static Connection connection;

    private static Admin admin;

 

    static {

        try {

            connection = ConnectionFactory.createConnection(HBaseConfig.getConfig());

            admin = connection.getAdmin();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

 

    // 创建表

    public static void createTable(String tableName, String[] columnFamilies) throws IOException {

        TableName table = TableName.valueOf(tableName);

        if (admin.tableExists(table)) {

            System.out.println("表 " + tableName + " 已经存在,正在删除...");

            admin.disableTable(table);

            admin.deleteTable(table);

        }

        HTableDescriptor tableDescriptor = new HTableDescriptor(table);

        for (String cf : columnFamilies) {

            tableDescriptor.addFamily(new HColumnDescriptor(cf));

        }

        admin.createTable(tableDescriptor);

        System.out.println("表 " + tableName + " 创建成功。");

    }

 

    // 插入数据

    public static void addRecord(String tableName, String rowKey, String columnFamily, String column, String value) throws IOException {

        Table table = connection.getTable(TableName.valueOf(tableName));

        Put put = new Put(Bytes.toBytes(rowKey));

        put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));

        table.put(put);

        table.close();

        System.out.println("已插入记录到表 " + tableName + ": 行键=" + rowKey + ", " + columnFamily + ":" + column + "=" + value);

    }

 

    // 关闭连接

    public static void closeConnection() {

        try {

            if (admin != null) admin.close();

            if (connection != null) connection.close();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

 

    public static void main(String[] args) {

        try {

            // 1. 创建学生表

            String studentTable = "Student";

            String[] studentCF = {"info"};

            createTable(studentTable, studentCF);

 

            // 插入学生表数据

            addRecord(studentTable, "2015001", "info", "name", "Zhangsan");

            addRecord(studentTable, "2015001", "info", "sex", "male");

            addRecord(studentTable, "2015001", "info", "age", "23");

 

            addRecord(studentTable, "2015002", "info", "name", "Mary");

            addRecord(studentTable, "2015002", "info", "sex", "female");

            addRecord(studentTable, "2015002", "info", "age", "22");

 

            addRecord(studentTable, "2015003", "info", "name", "Lisi");

            addRecord(studentTable, "2015003", "info", "sex", "male");

            addRecord(studentTable, "2015003", "info", "age", "24");

 

            // 2. 创建课程表

            String courseTable = "Course";

            String[] courseCF = {"details"};

            createTable(courseTable, courseCF);

 

            // 插入课程表数据

            addRecord(courseTable, "123001", "details", "name", "Math");

            addRecord(courseTable, "123001", "details", "credit", "2.0");

 

            addRecord(courseTable, "123002", "details", "name", "Computer Science");

            addRecord(courseTable, "123002", "details", "credit", "5.0");

 

            addRecord(courseTable, "123003", "details", "name", "English");

            addRecord(courseTable, "123003", "details", "credit", "3.0");

 

            // 3. 创建选课表

            String scTable = "SC";

            String[] scCF = {"score"};

            createTable(scTable, scCF);

 

            // 插入选课表数据

            addRecord(scTable, "2015001:123001", "score", "score", "86");

            addRecord(scTable, "2015001:123003", "score", "score", "69");

            addRecord(scTable, "2015002:123002", "score", "score", "77");

            addRecord(scTable, "2015002:123003", "score", "score", "99");

            addRecord(scTable, "2015003:123001", "score", "score", "98");

            addRecord(scTable, "2015003:123002", "score", "score", "95");

 

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            closeConnection();

        }

    }

}

 

2. 请编程实现以下功能:

(1)createTable(String tableName, String[] fields)

创建表,参数tableName为表的名称,字符串数组fields为存储记录各个字段名称的数组。要求当HBase已经存在名为tableName的表的时候,先删除原有的表,然后再创建新的表。

(2)addRecord(String tableName, String row, String[] fields, String[] values)

向表tableName、行row(用S_Name表示)和字符串数组fields指定的单元格中添加对应的数据values。其中,fields中每个元素如果对应的列族下还有相应的列限定符的话,用“columnFamily:column”表示。例如,同时向“Math”、“Computer Science”、“English”三列添加成绩时,字符串数组fields为{“Score:Math”, ”Score:Computer Science”, ”Score:English”},数组values存储这三门课的成绩。

(3)scanColumn(String tableName, String column)

浏览表tableName某一列的数据,如果某一行记录中该列数据不存在,则返回null。要求当参数column为某一列族名称时,如果底下有若干个列限定符,则要列出每个列限定符代表的列的数据;当参数column为某一列具体名称(例如“Score:Math”)时,只需要列出该列的数据。

(4)modifyData(String tableName, String row, String column)

修改表tableName,行row(可以用学生姓名S_Name表示),列column指定的单元格的数据。

(5)deleteRow(String tableName, String row)

删除表tableName中row指定的行的记录。

运行截图:

 

代码:

package hbase;

 

import hbase.HBaseConfig;

import org.apache.hadoop.hbase.client.*;

import org.apache.hadoop.hbase.*;

import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

 

public class HBaseOperations2 {

    private static Connection connection;

    private static Admin admin;

 

    static {

        try {

            connection = ConnectionFactory.createConnection(HBaseConfig.getConfig());

            admin = connection.getAdmin();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

 

    // 创建表

    public static void createTable(String tableName, String[] fields) throws IOException {

        TableName table = TableName.valueOf(tableName);

 

        // 获取 Admin 对象

        Admin admin = connection.getAdmin();

 

        // 如果表存在,先删除表

        if (admin.tableExists(table)) {

            System.out.println("表 " + tableName + " 已经存在,正在删除...");

            admin.disableTable(table);

            admin.deleteTable(table);

            System.out.println("表 " + tableName + " 已删除");

        }

 

        // 创建表描述符

        HTableDescriptor tableDescriptor = new HTableDescriptor(table);

 

        // 根据 fields 添加列族

        for (String field : fields) {

            String[] parts = field.split(":");

            String columnFamily = parts[0];

            if (!tableDescriptor.hasFamily(Bytes.toBytes(columnFamily))) {

                tableDescriptor.addFamily(new HColumnDescriptor(columnFamily));

            }

        }

 

        // 创建表

        admin.createTable(tableDescriptor);

        System.out.println("表 " + tableName + " 创建成功。");

 

        admin.close();

    }

 

    // 添加记录

    public static void addRecord(String tableName, String row, String[] fields, String[] values) throws IOException {

        if (fields.length != values.length) {

            throw new IllegalArgumentException("字段和对应值数组长度必须一致。");

        }

 

        Table table = connection.getTable(TableName.valueOf(tableName));

        Put put = new Put(Bytes.toBytes(row));

 

        for (int i = 0; i < fields.length; i++) {

            String field = fields[i];

            String[] parts = field.split(":");

            if (parts.length != 2) {

                throw new IllegalArgumentException("无效字段格式,期待 '列族:列名',但是得到了: " + field);

            }

 

            String columnFamily = parts[0];

            String column = parts[1];

            put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(values[i]));

        }

 

        table.put(put);

        table.close();

        System.out.println("行 " + row + " 的记录已添加到表 " + tableName);

    }

 

    // 浏览列数据

    public static void scanColumn(String tableName, String column) throws IOException {

        Table table = connection.getTable(TableName.valueOf(tableName));

        Scan scan = new Scan();

        ResultScanner scanner = table.getScanner(scan);

        for (Result result : scanner) {

            if (column.contains(":")) {

                String[] parts = column.split(":");

                String columnFamily = parts[0];

                String qualifier = parts[1];

                byte[] value = result.getValue(Bytes.toBytes(columnFamily), Bytes.toBytes(qualifier));

                System.out.println("行: " + Bytes.toString(result.getRow()) + ", 列: " + column + ", 值: " + Bytes.toString(value));

            } else {

                for (Cell cell : result.rawCells()) {

                    if (Bytes.toString(CellUtil.cloneFamily(cell)).equals(column)) {

                        System.out.println("行: " + Bytes.toString(result.getRow()) + ", 列: " + Bytes.toString(CellUtil.cloneQualifier(cell)) + ", 值: " + Bytes.toString(CellUtil.cloneValue(cell)));

                    }

                }

            }

        }

        table.close();

    }

 

    // 修改数据

    public static void modifyData(String tableName, String row, String column, String newValue) throws IOException {

        Table table = connection.getTable(TableName.valueOf(tableName));

        String[] parts = column.split(":");

        String columnFamily = parts[0];

        String qualifier = parts.length > 1 ? parts[1] : "";

        Put put = new Put(Bytes.toBytes(row));

        put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(qualifier), Bytes.toBytes(newValue));

        table.put(put);

        table.close();

        System.out.println("表 " + tableName + " 中的数据已修改");

    }

 

    // 删除行

    public static void deleteRow(String tableName, String row) throws IOException {

        Table table = connection.getTable(TableName.valueOf(tableName));

        Delete delete = new Delete(Bytes.toBytes(row));

        table.delete(delete);

        table.close();

        System.out.println("表 " + tableName + " 中的行 " + row + " 已删除");

    }

 

    // 关闭连接

    public static void closeConnection() {

        try {

            if (admin != null) admin.close();

            if (connection != null) connection.close();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

 

    public static void main(String[] args) {

        try {

            String tableName = "Students";

            String[] fields = {"Score:Math", "Score:Computer Science", "Score:English"};

            String[] values = {"85", "90", "88"};

            String row = "John";

 

            // 测试功能

            createTable(tableName, fields);

            addRecord(tableName, row, fields, values);

            scanColumn(tableName, "Score:Math");

            modifyData(tableName, row, "Score:Math", "95");

            scanColumn(tableName, "Score:Math");

            deleteRow(tableName, row);

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            closeConnection();

        }

    }

}

出现的问题:

  1. 其实这个是我从第二个实验就遇到的问题--导包。
  2. 打开hbase shell无法正常运行

解决方案(列出遇到的问题和解决办法,列出没有解决的问题):

1.导包成功后截图:

1.

 

2.打开hbase之前要先打开zookeeper(此实验需要hbase ,hdfs,zookeeper)

 

 

标签:02,String,admin,Bytes,tableName,static,熟悉,table,HBase
From: https://www.cnblogs.com/aixin52129211/p/18632817

相关文章

  • DataGrip2024.3完整版的安装教程(附激活,常见问题处理)
    卸载老版本DataGrip首先,如果小伙伴的电脑上有安装老版本的DataGrip,需要将其彻底卸载掉,如下所示(没有安装则不用管,直接安装即可):TIP:如果你之前使用过本站提供的 激活到2025年版本脚本,需要执行对应卸载脚本/适用2024版本/JetBrains2023最新全家桶/jetbra/scripts/unin......
  • HDFS操作02(遇到的问题和解决)
    编程实现一个类“MyFSDataInputStream”,该类继承“org.apache.hadoop.fs.FSDataInputStream”,要求如下:实现按行读取HDFS中指定文件的方法“readLine()”,如果读到文件末尾,则返回空,否则返回文件一行的文本。 查看Java帮助手册或其它资料,用“java.net.URL”和“org.apache.hadoop.......
  • 2024年免费项目管理软件大盘点——20款必备优秀工具推荐
    在当今快节奏的商业环境中,项目管理软件已成为团队协作和项目成功的关键工具。无论是初创企业还是大型企业,选择合适的项目管理软件都能显著提高工作效率、优化资源分配并确保项目按时完成。随着技术的不断进步,2024年涌现出许多优秀的免费项目管理工具,它们不仅功能强大,而且易于使用......
  • 熟悉常用的Linux操作和Hadoop操作(实验过程存在的问题和解决)
    实验步骤1)cd命令:切换目录(1) 切换到目录“/usr/local”(2) 切换到当前目录的上一级目录(3) 切换到当前登录Linux系统的用户的自己的主文件夹2)ls命令:查看文件与目录查看目录“/usr”下的所有文件和目录3)mkdir命令:新建目录(1)进入“/tmp”目录,创建一个名为“a”的目录,并查看“/tm......
  • 2025知识库工具搭建攻略:塑造知识管理新范式
    在当今这个信息爆炸的时代,知识已然成为个人与组织发展的核心驱动力。随着2025年的临近,如何搭建一套高效、智能的知识库工具,塑造全新的知识管理范式,成为众多有识之士关注的焦点。一、明确搭建知识库工具的目标在开启搭建之旅前,精准锚定目标至关重要。对于企业而言,是期望通......
  • 2025最全大数据工程师学习路线(建议收藏)
      找工作、写论文、项目实训以及实战项目课程学习私信我哟【不要错过文末彩蛋】申明:本文旨在为【大数据自学者|大数据专业学生|工资低的程序员(Java/Python等)】提供一个从入门到入职的的大数据技术学习路径,不适合5年以上大数据工程师的进阶学习。前言:一、个人介绍二、......
  • C++杂记02 指针
    好久没有更新推文了,最近换了工作,时间相对多了一点,有一点时间把过去的一些笔记内容给整理一下。能坚持学习和整理是一件很难的事情,当下大多数人的生活都相当碎片化,很多事做着做着就中断了,希望我能把我学习C++和OpenFOAM的一些内容写完。指针在OpenFOAM里面是一个很常见的内容,例如......
  • FastReport 2024年回顾与2025年展望:创新与跨平台发展之路
    2024年对于FastReport来说是充满挑战和收获的一年。FastReport在多个领域取得了重要突破和创新成果,不仅不断优化现有产品,还推出了一些令人振奋的新功能和新产品。接下来,FastReport将总结今年的关键成就,并展望未来的发展。FastReport的报表生成器(无论VCL平台还是.NET平台),跨平台......
  • 「省选联考 2023」人员调度
    离独立想出正解只差一步了。我的做法是使用网络流武器,抛弃了贪心的思考。虽然没有锻炼到贪心能力,但是加深了对网络流的理解吧。考虑撤销可以用线段树分治,故只考虑加入的情况。我们发现这个模型很像费用流,于是考虑建模。源点向所有员工连边,容量为\(1\),费用为其能力值。所......
  • 2024.12.26 os lab3
    2024.12.26oslab3原代码地址:https://github.com/BUPT-OS/easy_lab/tree/lab3运行未修改的代码,并且注释掉cout时发生错误:malloc():corruptedtopsize如果不注释cout,可以正常运行1.不注释cout时堆内存的详细分析1.程序启动阶段在程序启动时,堆的初始状态为空,堆顶指......