目录列表
Hbase伪分布式环境部署见:Hbase环境部署
前置准备
启动hdfs和hbase并验证环境正常
启动服务
通过shell
命令启动hdfs文件系统和hbase服务,jps
下出现如下图所示的进程即为正确。
start-all.sh # 启动HDFS文件系统 (datanode, namenode, secondary namenode)
start-hbase.sh # 启动HBase服务 (HMaster, HRegionServer, HQuorumPeer)
jps # 查看当前运行的任务进程
验证运行环境
进入hbase shell
终端或进入hbase
提供的web页面(HBase
默认端口:16030),验证环境正常。
HBase web页面如下图所示
搭建HBase客户端
使用IDEA创建Maven项目
修改pom.xml
配置文件
为项目添加hbase-lient
的依赖(刷新完成不飘红即为引入成功)
添加log4j
输出日志的依赖(刷新完成不飘红即为引入成功)
使用Java API操作HBase
Java连接Hbase
// 静态代码块
static {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "192.168.159.139"); //这里替换成自己Hbase的IP地址
try {
//创建HBase连接对象
connection = ConnectionFactory.createConnection(conf);
} catch (IOException ex) {
ex.printStackTrace();
}
}
创建命名空间
public static void createNameSpace(String nameSpaceName) throws IOException {
if (nameSpaceName == null || nameSpaceName.isEmpty()) {
logger.error("namespace 不能为空!");
return;
}
Admin admin = connection.getAdmin();
NamespaceDescriptor.Builder builder = NamespaceDescriptor.create(nameSpaceName);
NamespaceDescriptor namespaceDescriptor = builder.build();
try {
admin.createNamespace(namespaceDescriptor);
logger.info("namespace: {} 创建成功", nameSpaceName);
} catch (NamespaceExistException e) {
logger.error("namespace 名字已存在!");
}
}
运行截图:
Hbase
中前后对比:
表的创建和删除
// 表的创建和删除
public static void createTable() {
TableName tableName = TableName.valueOf("stu");
try (Admin admin = connection.getAdmin()) {
// 检查表是否存在
if (admin.tableExists(tableName)) {
admin.disableTable(tableName);
admin.deleteTable(tableName);
logger.info(tableName.toString() + " exists, deleted it.");
}
// 创建表描述符
TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(tableName)
.setColumnFamily(ColumnFamilyDescriptorBuilder.of("StuInfo"))
.setColumnFamily(ColumnFamilyDescriptorBuilder.of("Grades"))
.build();
// 创建表
admin.createTable(tableDescriptor);
logger.info("Table " + tableName + " created successfully.");
} catch (IOException e) {
logger.error("Error: " + e.getMessage());
e.printStackTrace();
}
}
运行截图:
Hbase
中前后对比:
插入数据
// 数据插入
public static void addData() throws IOException {
Table table = connection.getTable(TableName.valueOf("stu"));
Put put1 = new Put(Bytes.toBytes("row1"));
put1.addColumn(Bytes.toBytes("StuInfo"), Bytes.toBytes("name"), Bytes.toBytes("Jack"));
put1.addColumn(Bytes.toBytes("StuInfo"), Bytes.toBytes("age"), Bytes.toBytes("18"));
put1.addColumn(Bytes.toBytes("Grades"), Bytes.toBytes("math"), Bytes.toBytes("90"));
put1.addColumn(Bytes.toBytes("Grades"), Bytes.toBytes("english"), Bytes.toBytes("85"));
table.put(put1);
Put put2 = new Put(Bytes.toBytes("row2"));
put2.addColumn(Bytes.toBytes("StuInfo"), Bytes.toBytes("name"), Bytes.toBytes("Alice"));
put2.addColumn(Bytes.toBytes("StuInfo"), Bytes.toBytes("age"), Bytes.toBytes("19"));
put2.addColumn(Bytes.toBytes("Grades"), Bytes.toBytes("math"), Bytes.toBytes("88"));
put2.addColumn(Bytes.toBytes("Grades"), Bytes.toBytes("english"), Bytes.toBytes("92"));
table.put(put2);
Put put3 = new Put(Bytes.toBytes("row3"));
put3.addColumn(Bytes.toBytes("StuInfo"), Bytes.toBytes("name"), Bytes.toBytes("Bob"));
put3.addColumn(Bytes.toBytes("StuInfo"), Bytes.toBytes("age"), Bytes.toBytes("20"));
put3.addColumn(Bytes.toBytes("Grades"), Bytes.toBytes("math"), Bytes.toBytes("95"));
put3.addColumn(Bytes.toBytes("Grades"), Bytes.toBytes("english"), Bytes.toBytes("80"));
table.put(put3);
Put put4 = new Put(Bytes.toBytes("row4"));
put4.addColumn(Bytes.toBytes("StuInfo"), Bytes.toBytes("name"), Bytes.toBytes("Emma"));
put4.addColumn(Bytes.toBytes("StuInfo"), Bytes.toBytes("age"), Bytes.toBytes("21"));
put4.addColumn(Bytes.toBytes("Grades"), Bytes.toBytes("math"), Bytes.toBytes("82"));
put4.addColumn(Bytes.toBytes("Grades"), Bytes.toBytes("english"), Bytes.toBytes("90"));
table.put(put4);
Put put5 = new Put(Bytes.toBytes("row5"));
put5.addColumn(Bytes.toBytes("StuInfo"), Bytes.toBytes("name"), Bytes.toBytes("David"));
put5.addColumn(Bytes.toBytes("StuInfo"), Bytes.toBytes("age"), Bytes.toBytes("22"));
put5.addColumn(Bytes.toBytes("Grades"), Bytes.toBytes("math"), Bytes.toBytes("91"));
put5.addColumn(Bytes.toBytes("Grades"), Bytes.toBytes("english"), Bytes.toBytes("88"));
table.put(put5);
logger.info("SUCCESS");
table.close();
}
运行截图:
Hbase
表中的变化:
数据查询
行键查询
// 数据获取(通过行键查询)
public static void getData() throws IOException {
Table table = connection.getTable(TableName.valueOf("stu"));
Get get = new Get(Bytes.toBytes("row1"));
Result result = table.get(get);
for (Cell cell : result.rawCells()) {
System.out.println(CellUtil.getCellKeyAsString(cell));
System.out.println(new String(CellUtil.cloneQualifier(cell)));
System.out.println(new String(CellUtil.cloneValue(cell)));
System.out.println(cell.getTimestamp());
System.out.println("----------------------------");
}
table.close();
}
运行截图:
全部查询
// 全部查询
public static void getDataAll() throws IOException {
Table table = connection.getTable(TableName.valueOf("stu"));
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
for (Cell cell : result.rawCells()) {
System.out.println(CellUtil.getCellKeyAsString(cell));
System.out.println(new String(CellUtil.cloneQualifier(cell)));
System.out.println(new String(CellUtil.cloneValue(cell)));
System.out.println(cell.getTimestamp());
System.out.println("----------------------------");
}
}
scanner.close();
table.close();
}
运行截图:
特定前缀列查询
// 使用ColumnPrefixFilter过滤器 查询特定列前缀的列
public static void getDataWithColumnPrefixFilter() throws IOException {
Table table = connection.getTable(TableName.valueOf("stu"));
Filter filter = new ColumnPrefixFilter(Bytes.toBytes("na"));
Scan scan = new Scan();
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
for (Cell cell : result.rawCells()) {
System.out.println(CellUtil.getCellKeyAsString(cell));
System.out.println(new String(CellUtil.cloneQualifier(cell)));
System.out.println(new String(CellUtil.cloneValue(cell)));
System.out.println(cell.getTimestamp());
System.out.println("----------------------------");
}
}
scanner.close();
table.close();
}
运行截图:
完整代码
package org.example;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.IOException;
public class HBaseDemo {
private static Connection connection;
private static final Logger logger = LogManager.getLogger(HBaseDemo.class);
static {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "192.168.159.139");
try {
connection = ConnectionFactory.createConnection(conf);
logger.info("Hbase 连接成功!");
} catch (IOException ex) {
logger.error(ex.getMessage(), ex);
}
}
//创建命名空间
public static void createNameSpace(String nameSpaceName) throws IOException {
if (nameSpaceName == null || nameSpaceName.isEmpty()) {
logger.error("namespace 不能为空!");
return;
}
Admin admin = connection.getAdmin();
NamespaceDescriptor.Builder builder = NamespaceDescriptor.create(nameSpaceName);
NamespaceDescriptor namespaceDescriptor = builder.build();
try {
admin.createNamespace(namespaceDescriptor);
logger.info("namespace: {} 创建成功", nameSpaceName);
} catch (NamespaceExistException e) {
logger.error("namespace 名字已存在!");
}
}
// 表的创建和删除
public static void createTable() {
TableName tableName = TableName.valueOf("stu");
try (Admin admin = connection.getAdmin()) {
// 检查表是否存在
if (admin.tableExists(tableName)) {
admin.disableTable(tableName);
admin.deleteTable(tableName);
logger.info(tableName.toString() + " exists, deleted it.");
}
// 创建表描述符
TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(tableName)
.setColumnFamily(ColumnFamilyDescriptorBuilder.of("StuInfo"))
.setColumnFamily(ColumnFamilyDescriptorBuilder.of("Grades"))
.build();
// 创建表
admin.createTable(tableDescriptor);
logger.info("Table " + tableName + " created successfully.");
} catch (IOException e) {
logger.error("Error: " + e.getMessage());
e.printStackTrace();
}
}
// 数据插入
public static void addData() throws IOException {
Table table = connection.getTable(TableName.valueOf("stu"));
Put put1 = new Put(Bytes.toBytes("row1"));
put1.addColumn(Bytes.toBytes("StuInfo"), Bytes.toBytes("name"), Bytes.toBytes("Jack"));
put1.addColumn(Bytes.toBytes("StuInfo"), Bytes.toBytes("age"), Bytes.toBytes("18"));
put1.addColumn(Bytes.toBytes("Grades"), Bytes.toBytes("math"), Bytes.toBytes("90"));
put1.addColumn(Bytes.toBytes("Grades"), Bytes.toBytes("english"), Bytes.toBytes("85"));
table.put(put1);
Put put2 = new Put(Bytes.toBytes("row2"));
put2.addColumn(Bytes.toBytes("StuInfo"), Bytes.toBytes("name"), Bytes.toBytes("Alice"));
put2.addColumn(Bytes.toBytes("StuInfo"), Bytes.toBytes("age"), Bytes.toBytes("19"));
put2.addColumn(Bytes.toBytes("Grades"), Bytes.toBytes("math"), Bytes.toBytes("88"));
put2.addColumn(Bytes.toBytes("Grades"), Bytes.toBytes("english"), Bytes.toBytes("92"));
table.put(put2);
Put put3 = new Put(Bytes.toBytes("row3"));
put3.addColumn(Bytes.toBytes("StuInfo"), Bytes.toBytes("name"), Bytes.toBytes("Bob"));
put3.addColumn(Bytes.toBytes("StuInfo"), Bytes.toBytes("age"), Bytes.toBytes("20"));
put3.addColumn(Bytes.toBytes("Grades"), Bytes.toBytes("math"), Bytes.toBytes("95"));
put3.addColumn(Bytes.toBytes("Grades"), Bytes.toBytes("english"), Bytes.toBytes("80"));
table.put(put3);
Put put4 = new Put(Bytes.toBytes("row4"));
put4.addColumn(Bytes.toBytes("StuInfo"), Bytes.toBytes("name"), Bytes.toBytes("Emma"));
put4.addColumn(Bytes.toBytes("StuInfo"), Bytes.toBytes("age"), Bytes.toBytes("21"));
put4.addColumn(Bytes.toBytes("Grades"), Bytes.toBytes("math"), Bytes.toBytes("82"));
put4.addColumn(Bytes.toBytes("Grades"), Bytes.toBytes("english"), Bytes.toBytes("90"));
table.put(put4);
Put put5 = new Put(Bytes.toBytes("row5"));
put5.addColumn(Bytes.toBytes("StuInfo"), Bytes.toBytes("name"), Bytes.toBytes("David"));
put5.addColumn(Bytes.toBytes("StuInfo"), Bytes.toBytes("age"), Bytes.toBytes("22"));
put5.addColumn(Bytes.toBytes("Grades"), Bytes.toBytes("math"), Bytes.toBytes("91"));
put5.addColumn(Bytes.toBytes("Grades"), Bytes.toBytes("english"), Bytes.toBytes("88"));
table.put(put5);
logger.info("SUCCESS");
table.close();
}
// 全部查询
public static void getDataAll() throws IOException {
Table table = connection.getTable(TableName.valueOf("stu"));
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
for (Cell cell : result.rawCells()) {
System.out.println(CellUtil.getCellKeyAsString(cell));
System.out.println(new String(CellUtil.cloneQualifier(cell)));
System.out.println(new String(CellUtil.cloneValue(cell)));
System.out.println(cell.getTimestamp());
System.out.println("----------------------------");
}
}
scanner.close();
table.close();
}
// 数据获取 - 行键
public static void getDataRow() throws IOException {
Table table = connection.getTable(TableName.valueOf("stu"));
Get get = new Get(Bytes.toBytes("row1"));
Result result = table.get(get);
for (Cell cell : result.rawCells()) {
System.out.println(CellUtil.getCellKeyAsString(cell));
System.out.println(new String(CellUtil.cloneQualifier(cell)));
System.out.println(new String(CellUtil.cloneValue(cell)));
System.out.println(cell.getTimestamp());
System.out.println("----------------------------");
}
table.close();
}
// 使用ColumnPrefixFilter过滤器 查询特定列前缀的列
public static void getDataWithColumnPrefixFilter() throws IOException {
Table table = connection.getTable(TableName.valueOf("stu"));
Filter filter = new ColumnPrefixFilter(Bytes.toBytes("na"));
Scan scan = new Scan();
scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
for (Cell cell : result.rawCells()) {
System.out.println(CellUtil.getCellKeyAsString(cell));
System.out.println(new String(CellUtil.cloneQualifier(cell)));
System.out.println(new String(CellUtil.cloneValue(cell)));
System.out.println(cell.getTimestamp());
System.out.println("----------------------------");
}
}
scanner.close();
table.close();
}
public static void main(String[] args) throws IOException {
// 创建命名空间
// createNameSpace("test");
// 表的创建和删除
// createTable();
// 数据插入
// addData();
// 数据获取
// getDataRow();
// 全部查询
// getDataAll();
// 使用PrefixFilter过滤器查询
// getDataWithColumnPrefixFilter();
}
}
@鲨鱼爱兜兜