mirror of
https://github.com/JeffLi1993/springboot-learning-example.git
synced 2026-03-14 06:03:52 +08:00
Spring Boot 集成 HBase
This commit is contained in:
52
springboot-hbase/pom.xml
Executable file
52
springboot-hbase/pom.xml
Executable file
@@ -0,0 +1,52 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>springboot</groupId>
|
||||
<artifactId>springboot-hbase</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>springboot-hbase :: 集成 HBase</name>
|
||||
|
||||
<!-- Spring Boot 启动父依赖 -->
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.5.6.RELEASE</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<hbase-spring-boot>1.0.0.RELEASE</hbase-spring-boot>
|
||||
<mysql-connector>5.1.39</mysql-connector>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- Spring Boot Web 依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Boot Test 依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Boot HBase 依赖 -->
|
||||
<dependency>
|
||||
<groupId>com.spring4all</groupId>
|
||||
<artifactId>spring-boot-starter-hbase</artifactId>
|
||||
<version>${hbase-spring-boot}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Junit -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.spring.springboot;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* Spring Boot 应用启动类
|
||||
*
|
||||
* Created by bysocket on 16/4/26.
|
||||
*/
|
||||
// Spring Boot 应用的标识
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// 程序启动入口
|
||||
// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
|
||||
SpringApplication.run(Application.class,args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package org.spring.springboot.controller;
|
||||
|
||||
import org.spring.springboot.domain.City;
|
||||
import org.spring.springboot.service.CityService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* Created by bysocket on 07/02/2017.
|
||||
*/
|
||||
@RestController
|
||||
public class CityRestController {
|
||||
|
||||
@Autowired
|
||||
private CityService cityService;
|
||||
|
||||
@RequestMapping(value = "/api/city/save", method = RequestMethod.GET)
|
||||
public City save() {
|
||||
cityService.saveOrUpdate();
|
||||
City city = new City();
|
||||
city.setAge(1);
|
||||
return city;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/api/city/get", method = RequestMethod.GET)
|
||||
public City getCity() {
|
||||
return cityService.query("135xxxxxx");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package org.spring.springboot.dao;
|
||||
|
||||
import com.spring4all.spring.boot.starter.hbase.api.RowMapper;
|
||||
import org.apache.hadoop.hbase.client.Result;
|
||||
import org.apache.hadoop.hbase.util.Bytes;
|
||||
import org.spring.springboot.domain.City;
|
||||
|
||||
public class CityRowMapper implements RowMapper<City> {
|
||||
|
||||
private static byte[] COLUMN_FAMILY = "f".getBytes();
|
||||
private static byte[] NAME = "name".getBytes();
|
||||
private static byte[] AGE = "age".getBytes();
|
||||
|
||||
@Override
|
||||
public City mapRow(Result result, int rowNum) throws Exception {
|
||||
String name = Bytes.toString(result.getValue(COLUMN_FAMILY, NAME));
|
||||
int age = Bytes.toInt(result.getValue(COLUMN_FAMILY, AGE));
|
||||
|
||||
City dto = new City();
|
||||
dto.setCityName(name);
|
||||
dto.setAge(age);
|
||||
return dto;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package org.spring.springboot.domain;
|
||||
|
||||
/**
|
||||
* 城市实体类
|
||||
*
|
||||
* Created by bysocket on 07/02/2017.
|
||||
*/
|
||||
public class City {
|
||||
|
||||
/**
|
||||
* 城市编号
|
||||
*/
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 省份年龄
|
||||
*/
|
||||
private Integer age;
|
||||
|
||||
/**
|
||||
* 城市名称
|
||||
*/
|
||||
private String cityName;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Integer getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(Integer age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getCityName() {
|
||||
return cityName;
|
||||
}
|
||||
|
||||
public void setCityName(String cityName) {
|
||||
this.cityName = cityName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.spring.springboot.service;
|
||||
|
||||
import org.spring.springboot.domain.City;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 城市业务逻辑接口类
|
||||
* <p>
|
||||
* Created by bysocket on 07/02/2017.
|
||||
*/
|
||||
public interface CityService {
|
||||
|
||||
List<City> query(String startRow, String stopRow);
|
||||
|
||||
public City query(String row);
|
||||
|
||||
void saveOrUpdate();
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package org.spring.springboot.service.impl;
|
||||
|
||||
import com.spring4all.spring.boot.starter.hbase.api.HbaseTemplate;
|
||||
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.spring.springboot.dao.CityRowMapper;
|
||||
import org.spring.springboot.domain.City;
|
||||
import org.spring.springboot.service.CityService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 城市业务逻辑实现类
|
||||
* <p>
|
||||
* Created by bysocket on 07/02/2017.
|
||||
*/
|
||||
@Service
|
||||
public class CityServiceImpl implements CityService {
|
||||
|
||||
@Autowired private HbaseTemplate hbaseTemplate;
|
||||
|
||||
public List<City> query(String startRow, String stopRow) {
|
||||
Scan scan = new Scan(Bytes.toBytes(startRow), Bytes.toBytes(stopRow));
|
||||
scan.setCaching(5000);
|
||||
List<City> dtos = this.hbaseTemplate.find("people_table", scan, new CityRowMapper());
|
||||
return dtos;
|
||||
}
|
||||
|
||||
public City query(String row) {
|
||||
City dto = this.hbaseTemplate.get("people_table", row, new CityRowMapper());
|
||||
return dto;
|
||||
}
|
||||
|
||||
public void saveOrUpdate() {
|
||||
List<Mutation> saveOrUpdates = new ArrayList<Mutation>();
|
||||
Put put = new Put(Bytes.toBytes("135xxxxxx"));
|
||||
put.addColumn(Bytes.toBytes("people"), Bytes.toBytes("name"), Bytes.toBytes("test"));
|
||||
saveOrUpdates.add(put);
|
||||
|
||||
this.hbaseTemplate.saveOrUpdates("people_table", saveOrUpdates);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
## HBase 配置
|
||||
spring.data.hbase.quorum=xxx
|
||||
spring.data.hbase.rootDir=xxx
|
||||
spring.data.hbase.nodeParent=xxx
|
||||
Reference in New Issue
Block a user