hbase学习——创建springboot+hbase项目

  1. 在IDEA中创建项目
    项目类型: Spring Initializr

项目名: HbaseTest

包名: com.example.demo

Java版本: 8

依赖: Spring Web, Spring Boot DevTools

  1. 添加Maven依赖 (pom.xml)
    xml
4.0.0 org.springframework.boot spring-boot-starter-parent 2.7.18
<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>
3. 复制HBase配置文件 将虚拟机中的配置文件复制到 src/main/resources/:

/usr/local/hbase/conf/hbase-site.xml

/usr/local/hadoop/etc/hadoop/core-site.xml

  1. 创建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前端项目

  1. 创建Vue3项目
    bash
    npm create vue@latest frontend
    cd frontend
    npm install
  2. 安装依赖
    bash
    npm install axios
  3. 创建HBase操作组件
    vue
  1. 修改App.vue
    vue
posted @ 2025-09-22 21:40  vivi_vimi  阅读(24)  评论(0)    收藏  举报