SpringBoot 集成 HBase

一.导入依赖



	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) {
        List list = studentService.selectList(startRow, stopRow);
        return SaResult.data(list);
    }
}

4.创建service

//==============接口
public interface StudentService {
    List selectList(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 List selectList(String startRow, String stopRow) {
        Scan scan = new Scan(Bytes.toBytes(startRow), Bytes.toBytes(stopRow));
        scan.setCaching(5000);
        List dtos = 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进去的数据,是则成功

你可能感兴趣的:(SpringBoot,相关,大数据,相关,hbase,spring,boot,java)