Java 远程连接在 VMware Ubuntu 中的 Hbase 并进行简单的 CRUD(不使用 Maven)

之前上传的 CSDN,水印就懒得再做处理了
Hbase 作业 1

Environmental Preparation

Network Check

先对环境进行准备,确保虚拟机和宿主机能够互 Ping,确保两台主机能够网络通畅

宿主机打开 Powershell 对虚拟机进行 Ping 操作

同样的方式,打开虚拟机中 Ubhntu 的 Terminal 对宿主机进行 ping 操作

Launch Hadoop And Hbase Software

启动 Hadoop 和 Hbase 环境,HBase 需依赖 HDFS 存储数据,且需 ZooKeeper 协调服务,是否启动成功可以通过 jps 命令来查看

start-all.sh
start-hbase.sh
hbase shell

Create New Project in Idea

使用 Idea IDE 创建一个简单的 Java 项目(不使用 Maven)

Lanch Check

简单启动一下经常是否能够正常调用启动

Add the Hbase Jar Package to The Project

解压课程发的压缩包,获取得到 jar 包,后面的 Hadoop 同样

将 jar 包添加到项目中

Add the Hdaoop Jar Package to The Project

Hadoop 导入的 jar 可以看 https://www.cnblogs.com/zlslch/p/5837145.html,需要哪些路径的 jar 包

Set Hadoop environment variables

Hadoop 在 Windows 上运行需要额外的工具来处理,添加到目的路径

Create Java Class

创建 3 个 Java 类

  • HBaseConnetction.java 连接类
  • HBaseCRUD.java CRUD 类
  • HBaseTest.java 测试类

源码如下:

package io.github.charlie;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.client.coprocessor.AggregationClient;

/**
 * @Date: 2025/3/16
 * @Description: HBaseConnection
 */
public class HBaseConnection {
    public Configuration conf;
    public Connection conn;
    public AggregationClient aggregationClient;

    HBaseConnection() {
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "Dev");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        conf.set("hbase.master", "Dev:2181");
        try {
            conn = ConnectionFactory.createConnection(conf);
            System.out.println("HBase Connection Success");
        } catch (Exception e) {
            System.out.println("HBase Connection Failed");
            e.printStackTrace();
        }
        aggregationClient = new AggregationClient(conf);
    }

    public Configuration getConf() {
        return conf;
    }

    public Connection getConn() {
        return conn;
    }
}
package io.github.charlie;

import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.omg.CORBA.PUBLIC_MEMBER;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @Date: 2025/3/16
 * @Description: HBaseCRUD
 */
public class HBaseCRUD {
    public HBaseConnection hBaseConnection;
    public Admin admin;
    TableName tableName;
    HTableDescriptor tableDescriptor;
    HColumnDescriptor columnDescriptor;
    Table table;

    HBaseCRUD() {
        hBaseConnection = new HBaseConnection();
        getAdmin();
    }

    public void getAdmin() {
        try {
            admin = hBaseConnection.getConn().getAdmin();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Table getTable(String tableName) throws IOException {
        table = hBaseConnection.getConn().getTable(TableName.valueOf(tableName));
        return table;
    }

    public void createTable(String name, String[] columnFamily) throws IOException {
        tableName = TableName.valueOf(name);
        if (admin.tableExists(tableName)) {
            admin.disableTable(tableName);
            admin.deleteTable(tableName);
            System.out.println(tableName.toString() + " table exists");
        }
        tableDescriptor = new HTableDescriptor(tableName);
        for (String column : columnFamily) {
            columnDescriptor = new HColumnDescriptor(column);
            tableDescriptor.addFamily(columnDescriptor);
        }
        admin.createTable(tableDescriptor);
    }

    public void insertAndModify(String name, String rowKey, String columnFamilyName, String columnName, long timestamp, String value) throws IOException {
        getTable(name);
        Put put = new Put(Bytes.toBytes(rowKey));
        put.addColumn(Bytes.toBytes(columnFamilyName), Bytes.toBytes(columnName), timestamp, Bytes.toBytes(value));
        table.put(put);
        table.close();
    }

    public void selectByGet(String name, String rowKey, String columnFamilyName, String columnName) throws IOException {
        Get get = new Get(Bytes.toBytes(rowKey));
        Result result = getTable(name).get(get);
        byte[] value = result.getValue(Bytes.toBytes(columnFamilyName), Bytes.toBytes(columnName));
        System.out.println(Bytes.toString(value));
    }

    public void selectByCell(String name, String rowKey, String columnFamilyName, String columnName, int maxVersion) throws IOException {
        Get get = new Get(Bytes.toBytes(rowKey));
        get.setMaxVersions(maxVersion);
        Result result = getTable(name).get(get);
        List<Cell> cells = result.getColumnCells(Bytes.toBytes(columnFamilyName), Bytes.toBytes(columnName));
        for (Cell cell : cells) {
           byte[] value = CellUtil.cloneValue(cell);
           System.out.println(Bytes.toString(value));
        }
    }

    public void selectByRow(String name, String rowKey) throws IOException {
        Get get = new Get(Bytes.toBytes(rowKey));
        Result result = getTable(name).get(get);
        for (Cell cell : result.rawCells()) {
            System.out.println(new String(CellUtil.getCellKeyAsString(cell)));
            System.out.println(new String(CellUtil.cloneFamily(cell)));
            System.out.println(new String(CellUtil.cloneQualifier(cell)));
            System.out.println(new String(CellUtil.cloneValue(cell)));
            System.out.println(cell.getTimestamp());
        }
    }

    public void turncateTable(String name) throws IOException {
        getAdmin();
        tableName = TableName.valueOf(name);
        admin.disableTable(tableName);
        admin.truncateTable(tableName, false);
    }

    public static long timestampStringConvert2Long(String timestamp){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
        LocalDateTime localDateTime = LocalDateTime.parse(timestamp, formatter);
        return LocalDateTime.from(localDateTime).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }
}
package io.github.charlie;

/**
 * @Date: 2025/3/16
 * @Description: HBaseTest
 */
public class HBaseTest {
    public static void main(String[] args) {
        HBaseConnection hBaseConnection = new HBaseConnection();
    }
}

Test Connection

Create Table

package io.github.charlie;

import java.io.IOException;

/**
 * @Date: 2025/3/16
 * @Description: HBaseTest
 */
public class HBaseTest {
    public static void main(String[] args) throws IOException {
        // HBase连接测试
        // HBaseConnection hBaseConnection = new HBaseConnection();

        // 操作测试
        HBaseCRUD hBaseCRUD = new HBaseCRUD();

        String name = "kpl";
        String rowKey = "0001";
        long timestamp = 5L;
        String columnFamilyName = "role";
        String attr_name = "name";
        String attr_type = "type";
        String attr_duty = "duty";
        String value_name = "Hanxin";
        String value_type = "Warrior";
        String value_duty = "Wild";
        String[] columnFamily = {"role", "attack", "defence"};

        hBaseCRUD.createTable(name, columnFamily);

    }
}

Insert Data Demo

package io.github.charlie;

import java.io.IOException;

/**
 * @Date: 2025/3/16
 * @Description: HBaseTest
 */
public class HBaseTest {
    public static void main(String[] args) throws IOException {
        // HBase连接测试
        // HBaseConnection hBaseConnection = new HBaseConnection();

        // 操作测试
        HBaseCRUD hBaseCRUD = new HBaseCRUD();

        String name = "kpl";
        String rowKey = "0001";
        long timestamp = 5L;
        String columnFamilyName = "role";
        String attr_name = "name";
        String attr_type = "type";
        String attr_duty = "duty";
        String value_name = "Hanxin";
        String value_type = "Warrior";
        String value_duty = "Wild";
        String[] columnFamily = {"role", "attack", "defence"};

//        hBaseCRUD.createTable(name, columnFamily);

        hBaseCRUD.insertAndModify(name, rowKey, columnFamilyName, attr_name, timestamp, value_name);
        hBaseCRUD.insertAndModify(name, rowKey, columnFamilyName, attr_type, timestamp, value_type);
        hBaseCRUD.insertAndModify(name, rowKey, columnFamilyName, attr_duty, timestamp, value_duty);
    }
}

Select Data Demo

package io.github.charlie;

import java.io.IOException;

/**
 * @Date: 2025/3/16
 * @Description: HBaseTest
 */
public class HBaseTest {
    public static void main(String[] args) throws IOException {
        // HBase连接测试
        // HBaseConnection hBaseConnection = new HBaseConnection();

        // 操作测试
        HBaseCRUD hBaseCRUD = new HBaseCRUD();

        String name = "kpl";
        String rowKey = "0001";
        long timestamp = 5L;
        String columnFamilyName = "role";
        String attr_name = "name";
        String attr_type = "type";
        String attr_duty = "duty";
        String value_name = "Hanxin";
        String value_type = "Warrior";
        String value_duty = "Wild";
        String[] columnFamily = {"role", "attack", "defence"};

//        hBaseCRUD.createTable(name, columnFamily);

//        hBaseCRUD.insertAndModify(name, rowKey, columnFamilyName, attr_name, timestamp, value_name);
//        hBaseCRUD.insertAndModify(name, rowKey, columnFamilyName, attr_type, timestamp, value_type);
//        hBaseCRUD.insertAndModify(name, rowKey, columnFamilyName, attr_duty, timestamp, value_duty);

        hBaseCRUD.selectByGet(name, rowKey, columnFamilyName, attr_name);
        hBaseCRUD.selectByRow(name, rowKey);
    }
}

Turncate Table Demo

package io.github.charlie;

import java.io.IOException;

/**
 * @Date: 2025/3/16
 * @Description: HBaseTest
 */
public class HBaseTest {
    public static void main(String[] args) throws IOException {
        // HBase连接测试
        // HBaseConnection hBaseConnection = new HBaseConnection();

        // 操作测试
        HBaseCRUD hBaseCRUD = new HBaseCRUD();

        String name = "kpl";
        String rowKey = "0001";
        long timestamp = 5L;
        String columnFamilyName = "role";
        String attr_name = "name";
        String attr_type = "type";
        String attr_duty = "duty";
        String value_name = "Hanxin";
        String value_type = "Warrior";
        String value_duty = "Wild";
        String[] columnFamily = {"role", "attack", "defence"};

//        hBaseCRUD.createTable(name, columnFamily);

//        hBaseCRUD.insertAndModify(name, rowKey, columnFamilyName, attr_name, timestamp, value_name);
//        hBaseCRUD.insertAndModify(name, rowKey, columnFamilyName, attr_type, timestamp, value_type);
//        hBaseCRUD.insertAndModify(name, rowKey, columnFamilyName, attr_duty, timestamp, value_duty);

//        hBaseCRUD.selectByGet(name, rowKey, columnFamilyName, attr_name);
//        hBaseCRUD.selectByRow(name, rowKey);

        hBaseCRUD.turncateTable(name);
    }
}

Delete Data Demo

关于删除,《HBase 不睡觉书第 4 章》中写到

Add Delete Method

package io.github.charlie;

import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.omg.CORBA.PUBLIC_MEMBER;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @Date: 2025/3/16
 * @Description: HBaseCRUD
 */
public class HBaseCRUD {
    public HBaseConnection hBaseConnection;
    public Admin admin;
    TableName tableName;
    HTableDescriptor tableDescriptor;
    HColumnDescriptor columnDescriptor;
    Table table;

    HBaseCRUD() {
        hBaseConnection = new HBaseConnection();
        getAdmin();
    }

    public void getAdmin() {
        try {
            admin = hBaseConnection.getConn().getAdmin();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Table getTable(String tableName) throws IOException {
        table = hBaseConnection.getConn().getTable(TableName.valueOf(tableName));
        return table;
    }

    public void createTable(String name, String[] columnFamily) throws IOException {
        tableName = TableName.valueOf(name);
        if (admin.tableExists(tableName)) {
            admin.disableTable(tableName);
            admin.deleteTable(tableName);
            System.out.println(tableName.toString() + " table exists");
        }
        tableDescriptor = new HTableDescriptor(tableName);
        for (String column : columnFamily) {
            columnDescriptor = new HColumnDescriptor(column);
            tableDescriptor.addFamily(columnDescriptor);
        }
        admin.createTable(tableDescriptor);
        System.out.println(tableName.toString() + " table created");
    }

    public void insertAndModify(String name, String rowKey, String columnFamilyName, String columnName, long timestamp, String value) throws IOException {
        getTable(name);
        Put put = new Put(Bytes.toBytes(rowKey));
        put.addColumn(Bytes.toBytes(columnFamilyName), Bytes.toBytes(columnName), timestamp, Bytes.toBytes(value));
        table.put(put);
        table.close();
    }

    public void selectByGet(String name, String rowKey, String columnFamilyName, String columnName) throws IOException {
        Get get = new Get(Bytes.toBytes(rowKey));
        Result result = getTable(name).get(get);
        byte[] value = result.getValue(Bytes.toBytes(columnFamilyName), Bytes.toBytes(columnName));
        System.out.println(Bytes.toString(value));
    }

    public void selectByCell(String name, String rowKey, String columnFamilyName, String columnName, int maxVersion) throws IOException {
        Get get = new Get(Bytes.toBytes(rowKey));
        get.setMaxVersions(maxVersion);
        Result result = getTable(name).get(get);
        List<Cell> cells = result.getColumnCells(Bytes.toBytes(columnFamilyName), Bytes.toBytes(columnName));
        for (Cell cell : cells) {
            byte[] value = CellUtil.cloneValue(cell);
            System.out.println(Bytes.toString(value));
        }
    }

    public void selectByRow(String name, String rowKey) throws IOException {
        Get get = new Get(Bytes.toBytes(rowKey));
        Result result = getTable(name).get(get);
        for (Cell cell : result.rawCells()) {
            System.out.println(new String(CellUtil.getCellKeyAsString(cell)));
            System.out.println(new String(CellUtil.cloneFamily(cell)));
            System.out.println(new String(CellUtil.cloneQualifier(cell)));
            System.out.println(new String(CellUtil.cloneValue(cell)));
            System.out.println(cell.getTimestamp());
        }
    }

    public void turncateTable(String name) throws IOException {
        getAdmin();
        tableName = TableName.valueOf(name);
        admin.disableTable(tableName);
        admin.truncateTable(tableName, false);
    }

    public static long timestampStringConvert2Long(String timestamp) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
        LocalDateTime localDateTime = LocalDateTime.parse(timestamp, formatter);
        return LocalDateTime.from(localDateTime).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    /**
     * 根据行键删除指定表中的整行数据
     *
     * @param name   表名
     * @param rowKey 要删除的行的唯一标识键
     */
    public void deleteByRow(String name, String rowKey) throws IOException {
        // 创建一个Delete对象,指定要删除的行键
        Delete delete = new Delete(Bytes.toBytes(rowKey));
        // 获取指定的表并执行删除操作
        getTable(name).delete(delete);
    }

    /**
     * 根据行键和列限定符删除指定表中的特定列数据
     *
     * @param name             表名
     * @param rowKey           要删除数据的行的唯一标识键
     * @param columnFamilyName 列族名称
     * @param columnName       列名称
     */
    public void deleteByColumn(String name, String rowKey, String columnFamilyName, String columnName) throws IOException {
        // 创建一个Delete对象,指定要删除的行键
        Delete delete = new Delete(Bytes.toBytes(rowKey));
        // 添加要删除的列,包括列族和列名
        delete.addColumn(Bytes.toBytes(columnFamilyName), Bytes.toBytes(columnName));
        // 获取指定的表并执行删除操作
        getTable(name).delete(delete);
    }

    /**
     * 根据行键、列族和列名以及时间戳删除指定表中的特定版本的数据
     *
     * @param name             表名
     * @param rowKey           要删除数据的行的唯一标识键
     * @param columnFamilyName 列族名称
     * @param columnName       列名称
     * @param timestamp        数据的时间戳,用于指定要删除的具体版本
     */
    public void deleteByTime(String name, String rowKey, String columnFamilyName, String columnName, String timestamp) throws IOException {
        // 创建一个Delete对象,指定要删除的行键
        Delete delete = new Delete(Bytes.toBytes(rowKey));
        // 添加要删除的列及其时间戳,时间戳需要转换为长整型
        delete.addColumns(Bytes.toBytes(columnFamilyName), Bytes.toBytes(columnName), timestampStringConvert2Long(timestamp));
        // 获取指定的表并执行删除操作
        getTable(name).delete(delete);
    }

    /**
     * 删除指定的表
     *
     * @param name 表名
     */
    public void deleteTable(String name) throws IOException {
        getAdmin();

        // 检查表是否存在
        if (admin.tableExists(TableName.valueOf(name))) {
            // 禁用表,确保在删除之前表处于禁用状态
            admin.disableTable(TableName.valueOf(name));
            // 删除表
            admin.deleteTable(TableName.valueOf(name));
            System.out.println("表 " + name + " 已删除。");
        } else {
            System.out.println("表 " + name + " 不存在。");
        }
    }
}

Delete Demo

package io.github.charlie;

import java.io.IOException;

/**
 * @Date: 2025/3/16
 * @Description: HBaseTest
 */
public class HBaseTest {
    public static void main(String[] args) throws IOException {
        // HBase连接测试
        // HBaseConnection hBaseConnection = new HBaseConnection();

        // 操作测试
        HBaseCRUD hBaseCRUD = new HBaseCRUD();

        String name = "kpl";
        String rowKey = "0001";
        long timestamp = 5L;
        String columnFamilyName = "role";
        String attr_name = "name";
        String attr_type = "type";
        String attr_duty = "duty";
        String value_name = "Hanxin";
        String value_type = "Warrior";
        String value_duty = "Wild";
        String[] columnFamily = {"role", "attack", "defence"};

//        hBaseCRUD.createTable(name, columnFamily);
//
//        hBaseCRUD.insertAndModify(name, rowKey, columnFamilyName, attr_name, timestamp, value_name);
//        hBaseCRUD.insertAndModify(name, rowKey, columnFamilyName, attr_type, timestamp, value_type);
//        hBaseCRUD.insertAndModify(name, rowKey, columnFamilyName, attr_duty, timestamp, value_duty);

//        hBaseCRUD.selectByGet(name, rowKey, columnFamilyName, attr_name);
//        hBaseCRUD.selectByRow(name, rowKey);

//        hBaseCRUD.turncateTable(name);

        // 删除数据
        // hBaseCRUD.deleteByRow(name, rowKey);
        // hBaseCRUD.deleteByColumn(name, rowKey, columnFamilyName, attr_name);
        // hBaseCRUD.deleteByTime(name, rowKey, columnFamilyName, attr_name, String.valueOf(timestamp));

        // 删除表
        // hBaseCRUD.deleteTable(name);

    }
}

Inster New Data

package io.github.charlie;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Date: 2025/3/16
 * @Description: TODO
 */
public class HBaseTest1 {
    public static void main(String[] args) throws IOException {
        // 操作测试
        HBaseCRUD hBaseCRUD = new HBaseCRUD();

        String tableName = "kpl";

        long timestamp = 5L;

        String[] columnFamily = {"role", "attack", "defence"};

        String roleColumnFamilyName = "role";
        String roleAttrName = "name";
        String roleAttrType = "type";
        String roleAttrDuty = "duty";

        String attackColumnFamilyName = "attack";
        String attackAttrSkill = "skill";
        String attackAttrDamage = "damage";

        String defenceColumnFamilyName = "defence";
        String defenceAttrArmor = "armor";
        String defenceAttrResistance = "resistance";

        hBaseCRUD.createTable(tableName, columnFamily);

        hBaseCRUD.insertAndModify(tableName, "0001", roleColumnFamilyName, roleAttrName, timestamp, "Hanxin");
        hBaseCRUD.insertAndModify(tableName, "0001", roleColumnFamilyName, roleAttrType, timestamp, "Warrior");
        hBaseCRUD.insertAndModify(tableName, "0001", roleColumnFamilyName, roleAttrDuty, timestamp, "Wild");
        hBaseCRUD.insertAndModify(tableName, "0001", attackColumnFamilyName, attackAttrSkill, timestamp, "Slash");
        hBaseCRUD.insertAndModify(tableName, "0001", attackColumnFamilyName, attackAttrDamage, timestamp, "150");
        hBaseCRUD.insertAndModify(tableName, "0001", defenceColumnFamilyName, defenceAttrArmor, timestamp, "100");
        hBaseCRUD.insertAndModify(tableName, "0001", defenceColumnFamilyName, defenceAttrResistance, timestamp, "80");

        hBaseCRUD.insertAndModify(tableName, "0002", roleColumnFamilyName, roleAttrName, timestamp, "Libai");
        hBaseCRUD.insertAndModify(tableName, "0002", roleColumnFamilyName, roleAttrType, timestamp, "Warrior");
        hBaseCRUD.insertAndModify(tableName, "0002", roleColumnFamilyName, roleAttrDuty, timestamp, "Wild");
        hBaseCRUD.insertAndModify(tableName, "0002", attackColumnFamilyName, attackAttrSkill, timestamp, "Fireball");
        hBaseCRUD.insertAndModify(tableName, "0002", attackColumnFamilyName, attackAttrDamage, timestamp, "120");
        hBaseCRUD.insertAndModify(tableName, "0002", defenceColumnFamilyName, defenceAttrArmor, timestamp, "80");
        hBaseCRUD.insertAndModify(tableName, "0002", defenceColumnFamilyName, defenceAttrResistance, timestamp, "60");

        hBaseCRUD.insertAndModify(tableName, "0003", roleColumnFamilyName, roleAttrName, timestamp, "Zhuangzhou");
        hBaseCRUD.insertAndModify(tableName, "0003", roleColumnFamilyName, roleAttrType, timestamp, "Support");
        hBaseCRUD.insertAndModify(tableName, "0003", roleColumnFamilyName, roleAttrDuty, timestamp, "Controller");
        hBaseCRUD.insertAndModify(tableName, "0003", attackColumnFamilyName, attackAttrSkill, timestamp, "Heal");
        hBaseCRUD.insertAndModify(tableName, "0003", attackColumnFamilyName, attackAttrDamage, timestamp, "50");
        hBaseCRUD.insertAndModify(tableName, "0003", defenceColumnFamilyName, defenceAttrArmor, timestamp, "60");
        hBaseCRUD.insertAndModify(tableName, "0003", defenceColumnFamilyName, defenceAttrResistance, timestamp, "90");
    }
}

Problems

连接过程中发现 IP 可以连接,但是换成 HostName 就不能了

这里我换了个方式,通过 SSH,直接在 ubuntu 里面运行

之后按 ssh 连上 Ubuntu,换成 SSH

OK 了,问题不大,主要是操作

posted @ 2025-04-08 18:41  Charlie_Byte  阅读(240)  评论(0)    收藏  举报