大数据开发利器:Hadoop(6)-HBase第三讲 Java 开发基础

本文详细介绍HBase 0.96及1.2.3版本的数据增删改查操作,包括表的创建、数据的插入与查询等核心功能,并提供完整的Java代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本节介绍HBase版本号0.96和1.2.3的JAVA基本开发。讲解数据的增删改查。

1. 前言

本节主要从两个部分讲解JAVA的开发,分别是0.96版本和1.2.3版本。
以下为学生成绩表的逻辑视图:

namegradscore
EnglishMathComputer
ZhangsanB808595
LisiC657488

将通过两个版本HBase讲解增删改查。

2. HBase-0.96版本

2.1 启动服务和创建表

首先启动服务,让后用命令行增加一个studentInfo的表。

start-dfs.sh
start-yarn.sh
zkServer.sh start
start-hbase.sh
create 'studentInfo', 'grad', 'score'
list

2.2 新建连接和关闭连接

注意,每次进行操作之前,都需要新建连接;操作完成之后,需要关闭连接
打开Eclipse,创建新项目、包以及类。并将hbase的lib目录下的所有jar文件导入到项目中,同时将conf目录下的log4j.properties复制到当前项目的src目录下(没有这个文件无法查看HBase运行情况)。

// 创建HBase连接
Configuration conf = HBaseConfiguration.create();
// 设置Zookeeper端口和名称
conf.set("hbase.zookeeper.quorum", "localhost");
conf.set("hbase.zookeeper.property.clientPort", "2181");
// 加载配置
HConnection conn = HConnectionManager.createConnection(conf);
// 连接HBase表
HTableInterface table = conn.getTable("studentInfo");
# 关闭连接
table.close();
conn.close();

2.3 插入数据

// 插入数据
Put put = new Put(Bytes.toBytes("Zhangsan"));
put.add(Bytes.toBytes("grad"), Bytes.toBytes(""), Bytes.toBytes("B"));
table.put(put);

2.4 get方式查询数据

get方式查询目前测试了两种方式,查询指定行键和查询单元格。

① 查询指定行键方式
// 获取行键
Get get = new Get(Bytes.toBytes("Zhangsan"));
Result rr = table.get(get);
List<Cell> cells = rr.listCells();
for (Cell cell : cells) {
    String rowkey = new String(CellUtil.cloneRow(cell));
    String family = new String(CellUtil.cloneFamily(cell));
    String column = new String(CellUtil.cloneQualifier(cell));
    String value = new String(CellUtil.cloneValue(cell));
    System.out.println("rowkey : " + rowkey + " family : " + family + " column :" + column + " value : " + value);
}
② 查询单元格方式
// 获取行键
Get get = new Get(Bytes.toBytes("Zhangsan"));
// 列族名
String failmy = "score";
// 行限定符
String qulifier = "English";
Result rr = table.get(get);
// 如果不做判断,会抛出异常
if (get.addColumn(Bytes.toBytes(failmy), Bytes.toBytes(qualifier)) != null) {
    String value = new String(rr.getValue(Bytes.toBytes(failmy), Bytes.toBytes(qualifier)));
    System.out.println(value);
} 

2.5 Scan方式查询

这种方式比较简单,但不适合数据量大的场合,scan方式是输出全部数据。shell方式下可以加关键字limit限制输出量

Scan scan = new Scan();
ResultScanner ss = table.getScanner(scan);
for (Result rr : ss) {
    List<Cell> cells = rr.listCells();
    for (Cell cell : cells) {
        String rowkey = new String(CellUtil.cloneRow(cell));
        String family = new String(CellUtil.cloneFamily(cell));
        String column = new String(CellUtil.cloneQualifier(cell));
        String value = new String(CellUtil.cloneValue(cell));
        System.out.println("rowkey : " + rowkey + " family : " + family + " column :" + column + " value : " + value);
    }
}

2.6 删除数据

这里有三种方式,删除指定行、删除列族以及删除指定单元格。
可以新建一行进行测试。

① 删除指定行
Delete delete = new Delete(Bytes.toBytes("Zhangsan"));
table.delete(delete);
② 删除列族
Delete delete = new Delete(Bytes.toBytes("Zhangsan"));
String family = "Score";
delete.deleteFamily(Bytes.toBytes(family));
table.delete(delete);
③ 删除指定单元格
Delete delete = new Delete(Bytes.toBytes("Zhangsan"));
String family = "Score";
String qualifier = "Math";
delete.deleteColumns(Bytes.toBytes(family), Bytes.toBytes(qualifier));
table.delete(delete);

2.7 完全Java代码

package edu.hbase.study;

import java.io.IOException;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
/**
 * 0.96 版本Hbase,1.2.3也可以运行,只是提示某些方法已经被弃用
 * @author 凝心如定
 *
 */
public class EduHbase {
    public static Configuration conf;
    public static HConnection conn;
    public static HTableInterface table;

    // 建立连接
    public static void init() throws IOException {
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "localhost");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        conn = HConnectionManager.createConnection(conf);
        table = conn.getTable("studentInfo");
    }

    // 关闭连接
    public static void close() throws IOException {
        table.close();
        conn.close();
    }

    // 插入数据
    public static void put(String[] raws, String[][][] cells)
            throws IOException {

        for (int family = 0; family < 2; family++)
            for (int qualifier = 0; qualifier < 4; qualifier++) {
                Put put = new Put(Bytes.toBytes(raws[family]));
                put.add(Bytes.toBytes(cells[family][qualifier][0]),
                        Bytes.toBytes(cells[family][qualifier][1]),
                        Bytes.toBytes(cells[family][qualifier][2]));
                table.put(put);
            }
    }

    // get方式查询数据
    public static void get(String raw, String... args) throws IOException {
        Get get = new Get(Bytes.toBytes(raw));

        if (args.length == 0) {
            Result rr = table.get(get);
            List<Cell> cells = rr.listCells();
            for (Cell cell : cells) {
                String rowkey = new String(CellUtil.cloneRow(cell));
                String family = new String(CellUtil.cloneFamily(cell));
                String column = new String(CellUtil.cloneQualifier(cell));
                String value = new String(CellUtil.cloneValue(cell));
                System.out.println("rowkey : " + rowkey + " family : " + family
                        + " column :" + column + " value : " + value);
            }
        } else if (args.length == 2) {
            String failmy = args[0];
            String qualifier = args[1];
            Result rr = table.get(get);
            if (get.addColumn(Bytes.toBytes(failmy), Bytes.toBytes(qualifier)) != null) {
                String value = new String(rr.getValue(Bytes.toBytes(failmy),
                        Bytes.toBytes(qualifier)));
                System.out.println(value);
            } else {
                System.out.println("无该数据");
            }
        } else {
            System.err.println("参数输入错误");
        }

    }

    public static void scan() throws IOException {
        Scan scan = new Scan();
        ResultScanner ss = table.getScanner(scan);
        for (Result rr : ss) {
            List<Cell> cells = rr.listCells();
            for (Cell cell : cells) {
                String rowkey = new String(CellUtil.cloneRow(cell));
                String family = new String(CellUtil.cloneFamily(cell));
                String column = new String(CellUtil.cloneQualifier(cell));
                String value = new String(CellUtil.cloneValue(cell));
                System.out.println("rowkey : " + rowkey + " family : " + family
                        + " column :" + column + " value : " + value);
            }
        }

    }

    public static void delete(String raw, String... args) throws IOException {
        Delete delete = new Delete(Bytes.toBytes(raw));
        if (args.length == 2) {
            String family = args[0];
            String qualifier = args[1];
            delete.deleteColumns(Bytes.toBytes(family),
                    Bytes.toBytes(qualifier));
        } else if (args.length == 1) {
            String family = args[0];
            delete.deleteFamily(Bytes.toBytes(family));
        } else if (args.length > 2) {
            System.err.println("参数输入个数错误");
        }
        table.delete(delete);
    }

    public static void main(String[] args) throws IOException {
        String[] raws = new String[] { "Zhangsan", "Lisi" };
        String[][][] cells = {
                { { "grad", "", "B" }, { "score", "English", "80" },
                        { "score", "Math", "85" },
                        { "score", "Computer", "95" } },
                { { "grad", "", "C" }, { "score", "English", "65" },
                        { "score", "Math", "74" },
                        { "score", "Computer", "88" } } };
        init();
        // 插入数据
        // put(raws, cells);
        /*
         * get方法查询 
         * // 查询一个特定行 
         * get("Zhangsan"); 
         * // 查询一个特定列的值 
         * get("Zhangsan", "grad", ""); 
         * // 查询参数输入错误 
         * get("Zhangsan", "grad", "", "e");
         */
        /*
         * // Scan 方法查询 scan();
         */

        /* 删除数据 
         * delete("wanger"); 
         * // 删除指定列族 
         * delete("wanger", "grad");
         * // 删除指定单元格 
         * delete("wanger", "score", "English"); 
         * // 输入参数错误
         * delete("wanger", "score", "English", "111");
         */
        close();
    }

}

3. HBase-1.2.3版本

创建一个Eclipse项目,将log4j.properties文件复制到src目录下(没有这个文件无法查看HBase运行情况)。这里项目名为hbase_study,包名为com.jdk.hbase。

cd /usr/local/hbase-1.2.3/conf
cp log4j.properties ~/workspace/hbase_study/src # 这里复制之后可以在控制台查看输出信息

3.1 新建连接和关闭连接

同样的,这里也需要开启Hadoop、yarn、Zookeeper以及HBase服务,这里不在过多赘述。

// 新建连接
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
configuration.set("hbase.zookeeper.quorum", "localhost");
configuration.set("hbase.zookeeper.property.clientPort", "2181");
Connection connection = ConnectionFactory.createConnection(connfiguration);
admin = connection.getAdmin();

// 关闭连接
if (admin != null) {
    admin.close();
}
if (null != connection) {
    connection.close();
}

3.2 创建表以及列族

注意,每次进行以下操作时都需要开启连接,完成操作时,都需要关闭连接

/* 
    hbase shell:
    create "studentInfo", "grad", "score"
*/
TableName tableName = TableName.valueOf("studentInfo");
if (admin.tableExists(tableName) {
    System.out.println("table exists");
} else {
    HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
    String[] colFamily = {"grad", "score"};
    for (String str : colFamily) {
        HColumnDescriptor hcoulColumnDescriptor = new HColumnDescriptor("grad");
        hTableDescriptor.addFamily(hcoulColumnDescriptor);
    }
    admin.createTable(hTableDescriptor);
}

3.3 添加数据

/*
    hbase shell
    put 'studentInfo', 'Zhangsan', 'score', 'English', '80'
*/
Table table = connection.getTable(TableName.valueOf("studentInfo"));
Put put = new Put(Bytes.toBytes("Zhangsan"));
put.addColumn(Bytes.toBytes("score"), Bytes.toBytes("English"), Bytes.toBytes("80"))
table.put(put);
table.close();

3.4 浏览数据

这里有举了两种例子:浏览指定单元格和浏览指定行键。

① 浏览指定单元格
/*
    hbase shell
    get 'studentInfo', 'Zhangsan', 'score'
*/
Get get = new Get(Bytes.toBytes("Zhangsan"));
Table table = connection.getTable(TableName.valueOf("studentInfo"));
get.addColumn(Bytes.toBytes("score"), Bytes.toBytes("English"));
// 格式化输出
Result result = table.get(get);
System.out.println(new String(result.getValue(colFamily.getBytes(), col == null ? null : col.getBytes())));
table.close();
② 浏览指定行键
/*
    hbase shell
    get 'studentInfo', 'Zhangsan'
*/
Get get = new Get(Bytes.toBytes("Zhangsan"));
Table table = connection.getTable(TableName.valueOf("studentInfo"));
Result rr = table.get(get);
List<Cell> cells = rr.listCells();
for (Cell cell : cells) {
    String rowkey = new String(CellUtil.cloneRow(cell));
    String family = new String(CellUtil.cloneFamily(cell));
    String column = new String(CellUtil.cloneQualifier(cell));
    String value = new String(CellUtil.cloneValue(cell));
    System.out.println("rowkey : " + rowkey + " family : " + family + " column :" + column + " value : " + value);
}
table.close();

3.5 删除数据

这里举三个例子,分别删除整行,列族和指定单元格。

① 删除整行
/*
    hbase shell
    delete 'studentInfo', 'Zhangsan'
*/
Table table = connection.getTable(TableName.valueOf("studentInfo"));
Delete del = new Delete(Bytes.toBytes("Zhangsan"));
table.delete(del);
talbe.close();
② 删除列族
/*
    hbase delete 'studentInfo', 'Zhangsan', 'score'
*/
Table table = connection.getTable(TableName.valueOf("studentInfo"));
Delete del = new Delete(Bytes.toBytes("Zhangsan"));
del.addFamily(Bytes.toBytes("score"));
table.delete(del);
table.close();
③ 删除指定单元格
/*
    hbase shell
    delete 'studentInfo', 'Zhangsan', 'score', 'English'
*/
Table table = connection.getTable(TableName.valueOf("studentInfo"));
Delete del = new Delete(Bytes.toBytes("Zhangsan");  del.addColumns(Bytes.toBytes("score"), Bytes.toBytes("English"));
table.close();

3.6 删除表

删除表之前需要先禁用表。

/*
    hbase shell
    disable 'studentInfo'
    drop 'studetnInfo'
*/
TableName tName = TableName.valueOf(Bytes.toBytes("studentInfo"));
if (admin.tableExists(tName)){
    admin.disableTable(tName);
    admin.deleteTable(tName);
} else{
    System.out.println(tableName + " not exists.");
}

3.7 完成JAVA代码

package edu.hbase.study;

import java.io.IOException;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
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.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
/**
 * 1.2.3 HBase下测试通过
 * @author 凝心如定
 */
public class EduHbase2 {
    public static Configuration configuration;
    public static Connection connection;
    public static Admin admin;

    // 建立连接
    public static void init() throws IOException {
        configuration = HBaseConfiguration.create();
        configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
        configuration.set("hbase.zookeeper.quorum", "localhost");
        configuration.set("hbase.zookeeper.property.clientPort", "2181");
        connection = ConnectionFactory.createConnection(configuration);
        admin = connection.getAdmin();
    }

    // 关闭连接
    public static void close() throws IOException {
        if (admin != null) {
            admin.close();
        }
        if (null != connection) {
            connection.close();
        }
    }

    // 创建表
    public static void createTable(String myTableName, String[] colFamily)
            throws IOException {
        init();
        TableName tableName = TableName.valueOf(myTableName);
        if (admin.tableExists(tableName)) {
            System.out.println("table exists");
        } else {
            HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
            for (String str : colFamily) {
                HColumnDescriptor hcoulColumnDescriptor = new HColumnDescriptor(
                        str);
                hTableDescriptor.addFamily(hcoulColumnDescriptor);
            }
            admin.createTable(hTableDescriptor);
        }
        close();
    }

    // 添加数据
    public static void insertData(String tableName, String rowkey,
            String colFamily, String col, String val) throws IOException {
        init();
        Table table = connection.getTable(TableName.valueOf(tableName));
        Put put = new Put(Bytes.toBytes(rowkey));
        put.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col),
                Bytes.toBytes(val));
        table.put(put);
        table.close();
        close();
    }

    // 浏览数据
    public static void getData(String tableName, String rowKey,
            String colFamily, String col) throws IOException {
        init();
        Table table = connection.getTable(TableName.valueOf(tableName));
        Get get = new Get(Bytes.toBytes(rowKey));
        get.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col));
        // 获取的result数据是结果集,还需要格式化输出
        Result result = table.get(get);
        System.out.println(new String(result.getValue(colFamily.getBytes(),
                col == null ? null : col.getBytes())));
        table.close();
        close();
    }

    public static void getData2(String tableName, String rowKey)
            throws IOException {
        init();
        Get get = new Get(Bytes.toBytes(rowKey));
        Table table = connection.getTable(TableName.valueOf(tableName));
        Result rr = table.get(get);
        List<Cell> cells = rr.listCells();
        for (Cell cell : cells) {
            String rowkey = new String(CellUtil.cloneRow(cell));
            String family = new String(CellUtil.cloneFamily(cell));
            String column = new String(CellUtil.cloneQualifier(cell));
            String value = new String(CellUtil.cloneValue(cell));
            System.out.println("rowkey : " + rowkey + " family : " + family
                    + " column :" + column + " value : " + value);
        }
        table.close();
        close();
    }

    // 删除数据
    public static void deleteData(String tableName, String...args)
            throws IOException {
        init();
        Table table = connection.getTable(TableName.valueOf(tableName));
        if (args.length == 1) {

            // 删除整行
            Delete del = new Delete(Bytes.toBytes(args[0]));
            table.delete(del);
        } else if(args.length == 2) {
            // 删除指定列族
            Delete del = new Delete(Bytes.toBytes(args[0]));
            del.addFamily(Bytes.toBytes(args[1]));
            table.delete(del);
        } else if (args.length == 3) {
            // 删除指定单元格
            Delete del = new Delete(Bytes.toBytes(args[1]));
            del.addColumns(Bytes.toBytes(args[1]), Bytes.toBytes(args[2]));
        }
        table.close();
        close();
    }

    public static void deleteTable(String tableName) throws IOException {
        init();
        TableName tName = TableName.valueOf(Bytes.toBytes(tableName));
        if (admin.tableExists(tName)){
            admin.disableTable(tName);
            admin.deleteTable(tName);
        } else{
            System.out.println(tableName + " not exists.");
        }
        close();
    }

    public static void main(String[] args) throws IOException {
        String[] raws = new String[] { "Zhangsan", "Lisi" };
        String[][][] cells = {
                { { "grad", "", "B" }, { "score", "English", "80" },
                        { "score", "Math", "85" },
                        { "score", "Computer", "95" } },
                { { "grad", "", "C" }, { "score", "English", "65" },
                        { "score", "Math", "74" },
                        { "score", "Computer", "88" } } };
        // 新建表
        // createTable("studentInfo", new String[] { "grad", "score" });
        // 插入数据
        // for (int family = 0; family < 2; family++)
        // for (int qualifier = 0; qualifier < 4; qualifier++) {
        // insertData("studentInfo", raws[family],
        // cells[family][qualifier][0],
        // cells[family][qualifier][1],
        // cells[family][qualifier][2]);
        // }
        // 浏览数据
//      getData("studentInfo", "Zhangsan", "grad", "");
//      getData2("studentInfo", "Zhangsan");
//       删除整行
//      insertData("studentInfo", "Wanger", "score", "English", "99");
//      deleteData("studentInfo", "Zhangsan");
//      deleteData("studentInfo", "wanger");
        deleteTable("studentInfo");
    }
}

4. 总结

本节主要介绍了HBase0.96和1.2.3版本下数据的增删改查。可以发现,两种版本除了代码有些差异之外,基本原理都是一致的。

参考内容

林子雨-大数据技术原理与应用
王滨-网易云课堂微专业

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值