hbase学习——创建springboot+hbase项目
- 在IDEA中创建项目
项目类型: Spring Initializr
项目名: HbaseTest
包名: com.example.demo
Java版本: 8
依赖: Spring Web, Spring Boot DevTools
- 添加Maven依赖 (pom.xml)
xml
<groupId>com.example</groupId>
<artifactId>HbaseTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>HbaseTest</name>
<properties>
<java.version>1.8</java.version>
<hbase.version>2.4.18</hbase.version>
<hadoop.version>3.3.6</hadoop.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- HBase Client -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>${hbase.version}</version>
</dependency>
<!-- Hadoop Common -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
/usr/local/hbase/conf/hbase-site.xml
/usr/local/hadoop/etc/hadoop/core-site.xml
- 创建HBase配置类
java
// src/main/java/com/example/demo/config/HBaseConfig.java
package com.example.demo.config;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import java.io.IOException;
@Configuration
public class HBaseConfig {
@Bean
public org.apache.hadoop.conf.Configuration configuration() {
org.apache.hadoop.conf.Configuration config = HBaseConfiguration.create();
// 配置文件已放在resources目录,会自动加载
return config;
}
@Bean
public Connection connection() throws IOException {
return ConnectionFactory.createConnection(configuration());
}
}
5. 创建实体类
java
// src/main/java/com/example/demo/entity/User.java
package com.example.demo.entity;
public class User {
private String rowKey;
private String name;
private String age;
private String email;
// 构造方法、getter、setter
public User() {}
public User(String rowKey, String name, String age, String email) {
this.rowKey = rowKey;
this.name = name;
this.age = age;
this.email = email;
}
// getter和setter方法...
}
6. 创建Service层
java
// src/main/java/com/example/demo/service/HBaseService.java
package com.example.demo.service;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Service
public class HBaseService {
@Autowired
private Connection connection;
// 创建表
public void createTable(String tableName, String... columnFamilies) throws IOException {
try (Admin admin = connection.getAdmin()) {
TableName tn = TableName.valueOf(tableName);
if (admin.tableExists(tn)) {
System.out.println("Table already exists: " + tableName);
return;
}
TableDescriptorBuilder tableBuilder = TableDescriptorBuilder.newBuilder(tn);
for (String cf : columnFamilies) {
ColumnFamilyDescriptorBuilder cfBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(cf));
tableBuilder.setColumnFamily(cfBuilder.build());
}
admin.createTable(tableBuilder.build());
System.out.println("Table created: " + tableName);
}
}
// 插入数据
public void putData(String tableName, String rowKey, String family, String qualifier, String value) throws IOException {
try (Table table = connection.getTable(TableName.valueOf(tableName))) {
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(value));
table.put(put);
}
}
// 查询数据
public String getData(String tableName, String rowKey, String family, String qualifier) throws IOException {
try (Table table = connection.getTable(TableName.valueOf(tableName))) {
Get get = new Get(Bytes.toBytes(rowKey));
Result result = table.get(get);
byte[] value = result.getValue(Bytes.toBytes(family), Bytes.toBytes(qualifier));
return value != null ? Bytes.toString(value) : null;
}
}
// 扫描表
public List<String> scanTable(String tableName) throws IOException {
List<String> results = new ArrayList<>();
try (Table table = connection.getTable(TableName.valueOf(tableName))) {
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
results.add(Bytes.toString(result.getRow()) + ": " + result.toString());
}
}
return results;
}
// 删除数据
public void deleteData(String tableName, String rowKey) throws IOException {
try (Table table = connection.getTable(TableName.valueOf(tableName))) {
Delete delete = new Delete(Bytes.toBytes(rowKey));
table.delete(delete);
}
}
}
7. 创建Controller层
java
// src/main/java/com/example/demo/controller/HBaseController.java
package com.example.demo.controller;
import com.example.demo.service.HBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.List;
@RestController
@RequestMapping("/api/hbase")
@CrossOrigin(origins = "http://localhost:3000") // Vue3前端地址
public class HBaseController {
@Autowired
private HBaseService hBaseService;
@PostMapping("/create-table")
public String createTable(@RequestParam String tableName) throws IOException {
hBaseService.createTable(tableName, "info");
return "Table created: " + tableName;
}
@PostMapping("/add-data")
public String addData(@RequestParam String tableName,
@RequestParam String rowKey,
@RequestParam String family,
@RequestParam String qualifier,
@RequestParam String value) throws IOException {
hBaseService.putData(tableName, rowKey, family, qualifier, value);
return "Data added successfully";
}
@GetMapping("/get-data")
public String getData(@RequestParam String tableName,
@RequestParam String rowKey,
@RequestParam String family,
@RequestParam String qualifier) throws IOException {
return hBaseService.getData(tableName, rowKey, family, qualifier);
}
@GetMapping("/scan-table")
public List<String> scanTable(@RequestParam String tableName) throws IOException {
return hBaseService.scanTable(tableName);
}
@DeleteMapping("/delete-data")
public String deleteData(@RequestParam String tableName,
@RequestParam String rowKey) throws IOException {
hBaseService.deleteData(tableName, rowKey);
return "Data deleted successfully";
}
}
8. 修改应用配置
java
// src/main/java/com/example/demo/HbaseTestApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HbaseTestApplication {
public static void main(String[] args) {
SpringApplication.run(HbaseTestApplication.class, args);
}
}
9. 应用配置文件
properties
src/main/resources/application.properties
server.port=8080
spring.application.name=HbaseTest
二、创建Vue3前端项目
- 创建Vue3项目
bash
npm create vue@latest frontend
cd frontend
npm install - 安装依赖
bash
npm install axios - 创建HBase操作组件
vue
HBase 操作测试
<div class="operation-section">
<h2>创建表</h2>
<input v-model="createTableName" placeholder="表名" />
<button @click="createTable">创建表</button>
</div>
<div class="operation-section">
<h2>添加数据</h2>
<input v-model="addData.table" placeholder="表名" />
<input v-model="addData.rowKey" placeholder="行键" />
<input v-model="addData.family" placeholder="列族" />
<input v-model="addData.qualifier" placeholder="列名" />
<input v-model="addData.value" placeholder="值" />
<button @click="addDataToTable">添加数据</button>
</div>
<div class="operation-section">
<h2>查询数据</h2>
<input v-model="queryData.table" placeholder="表名" />
<input v-model="queryData.rowKey" placeholder="行键" />
<input v-model="queryData.family" placeholder="列族" />
<input v-model="queryData.qualifier" placeholder="列名" />
<button @click="getData">查询数据</button>
<p v-if="queryResult">结果: {{ queryResult }}</p>
</div>
<div class="operation-section">
<h2>扫描表</h2>
<input v-model="scanTableName" placeholder="表名" />
<button @click="scanTable">扫描表</button>
<div v-if="scanResults.length">
<h3>扫描结果:</h3>
<ul>
<li v-for="(result, index) in scanResults" :key="index">{{ result }}</li>
</ul>
</div>
</div>
<div class="operation-section">
<h2>删除数据</h2>
<input v-model="deleteData.table" placeholder="表名" />
<input v-model="deleteData.rowKey" placeholder="行键" />
<button @click="deleteDataFromTable">删除数据</button>
</div>
- 修改App.vue
vue

浙公网安备 33010602011771号