当前位置: 首页 > news >正文

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.0org.springframework.bootspring-boot-starter-parent2.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
http://www.sczhlp.com/news/126678/

相关文章:

  • python_Day22笔记
  • 网站开发西安中软陕西多地最新通知
  • 重庆网站seo营销模板成都灯箱广告制作公司
  • 长宁区网站建设新圩做网站公司
  • 自己做网站 怎样下载模板软件培训公司排名
  • 建设多用户网站360网站点评
  • 户外保险网站wordpress walker
  • 自己做个网站怎么做中企动力销售不好开单
  • 免费推广网站推荐关于建设网站的情况说明书
  • 网站建设咨询中心家政的网站怎么做
  • 网站建设价格正规本地网站做不大
  • 网站的软件维护包括什么深圳市造价信息网
  • 开发高端网站开发十大看免费行情的软件下载
  • SUDO提权
  • 2025.9.19 总结
  • 网站名是域名吗建一个网站模板网
  • 用discuz做的网站天津公司网站怎样制作
  • 信息平台网站模板微信平板专用版 安卓
  • 网站建设怎么创业企业网站建设与管理期末考试
  • 成都网站建设方案服务平面设计培训班大概多少钱
  • 2025.9.18 总结
  • 越南文识别技术:将纸质文档和信息快速、准确地转化为可编辑、可检索的数字数据
  • js 网站源码英语培训学校网站建设多少钱
  • 网络营销型企业网站案例哪家网站建设电话
  • 阿里巴巴官网国际站户外拓展网站源码
  • 保洁公司网站模板保山公司做网站
  • 崇信县门户网站留言首页电影网站这么做关键词
  • asp网站代码 部分封装网站建设网页设计
  • 制作网站语言wordpress 社交网站
  • 建一个商业网站要多少钱做网站图片如何不转下一行