spring boot java SpringBoot 集成 HBase - beans

📅 2026-11-30 👤 拼多多助力营销技巧 📂 2026拼多多助力 👁️ 120 次阅读

一.导入依赖

org.springframework.data

spring-data-hadoop

2.5.0.RELEASE

com.sun.xml.bind

jaxb-impl

org.apache.hbase

hbase-client

1.3.2

org.slf4j

slf4j-log4j12

org.mortbay.jetty

servlet-api-2.5

org.mortbay.jetty

servlet-api-2.5-6.1.14

com.google.protobuf

protobuf-java

2.5.0

二.配置hbase

1.配置yml文件

#HBase连接的zookeeper配置

hbase:

zookeeper:

quorum: 127.0.0.1

property:

clientPort: 2181

2.配置HbaseConfig连接以及HbaseTemplate

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.HBaseConfiguration;

import org.apache.hadoop.hbase.client.Connection;

import org.apache.hadoop.hbase.client.ConnectionFactory;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.context.annotation.Bean;

import org.springframework.data.hadoop.hbase.HbaseTemplate;

import java.io.IOException;

/**

*HBase配置

*/

@org.springframework.context.annotation.Configuration

public class HbaseConfig {

@Value("${hbase.zookeeper.quorum}")

private String quorum;

@Value("${hbase.zookeeper.property.clientPort}")

private String clientPort;

@Bean

public Configuration configuration() {

Configuration configuration = HBaseConfiguration.create();

configuration.set("hbase.zookeeper.quorum", quorum);

configuration.set("hbase.zookeeper.property.clientPort", clientPort);

return configuration;

}

@Bean(name = "hbaseTemplate")

public HbaseTemplate hbaseTemplate() {

return new HbaseTemplate(configuration());

}

@Bean

public Connection getConnection() throws IOException { 拼多多帮助力入口

return ConnectionFactory.createConnection(configuration());

}

}

三.使用hbase

1.在hbase shell下新增以下数据

//创建命名空间

create_namespace 'user'

//创建表

create 'user:sac','student','course1','course2','course3'

//第一条

put 'user:sac','2023001','student:S_Name','zhangsan'

put 'user:sac','2023001','student:S_Sex','male'

put 'user:sac','2023001','student:S_Age','23'

//第二条

put 'user:sac' ,'2023002','student:S_Name','lisi'

put 'user:sac' ,'2023002','student:S_Sex','female'

put 'user:sac' ,'2023002','student:S_Age','22'

//第三条

put 'user:sac' ,'2023003','student:S_Name','wanger'

put 'user:sac' ,'2023003','student:S_Sex','male'

put 'user:sac' ,'2023003','student:S_Age','24'

2.创建实体类

@Data

public class Student {

private String name;

private String sex;

private String age;

}

3.创建controller

import cn.dev33.satoken.util.SaResult;

import lombok.RequiredArgsConstructor;

import org.apache.ibatis.annotations.Param;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequiredArgsConstructor

@RestController

@RequestMapping("/hbase/student")

public class StudentController {

private final StudentService studentService;

@RequestMapping("/list")

public SaResult list(@Param("startRow") String startRow, @Param("stopRow") String stopRow) {

Listlist = studentService.selectList(startRow, stopRow);

return SaResult.data(list);

}

}

4.创建service

//==============接口

public interface StudentService {

ListselectList(String startRow, String stopRow);

}

//==============实现类

import org.apache.hadoop.hbase.client.Mutation;

import org.apache.hadoop.hbase.client.Put;

import org.apache.hadoop.hbase.client.Scan;

import org.apache.hadoop.hbase.util.Bytes;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.hadoop.hbase.HbaseTemplate;

import org.springframework.stereotype.Service;

import java.util.ArrayList;

import java.util.List;

@Service

public class StudentServiceImpl implements StudentService {

@Autowired

private HbaseTemplate hbaseTemplate;

/**

* 根据开始、结束行

*

* @param startRow

* @param stopRow

* @return

*/

@Override

public ListselectList(String startRow, String stopRow) {

Scan scan = new Scan(Bytes.toBytes(startRow), Bytes.toBytes(stopRow));

scan.setCaching(5000);

Listdtos = this.hbaseTemplate.find("user:sac", scan, new CityRowMapper());

return dtos;

}

}

5.写mapper

import org.apache.hadoop.hbase.client.Result;

import org.apache.hadoop.hbase.util.Bytes;

import org.springframework.data.hadoop.hbase.RowMapper;

import java.sql.SQLException;

/**

* 实现 RowMapper 进行列簇转换

*/

public class StudentRowMapper implements RowMapper{

private static byte[] COLUMN_FAMILY = "student".getBytes();

private static byte[] NAME = "S_Name".getBytes();

private static byte[] SEX = "S_Sex".getBytes();

private static byte[] AGE = "S_Age".getBytes();

@Override

public Student mapRow(Result result, int i) throws SQLException {

String name = Bytes.toString(result.getValue(COLUMN_FAMILY, NAME));

String sex = Bytes.toString(result.getValue(COLUMN_FAMILY, SEX));

String age = Bytes.toString(result.getValue(COLUMN_FAMILY, AGE));

Student dto = new Student();

dto.setName(name);

dto.setSex(sex);

dto.setAge(age);

return dto;

}

}

6.调用接口查看结果是否为put进去的数据,是则成功

精彩链接

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧