Skip to content

Commit bccf948

Browse files
author
liqiangqiang
committed
Spring Boot 集成 HBase
1 parent 7e3e9fc commit bccf948

File tree

8 files changed

+245
-0
lines changed

8 files changed

+245
-0
lines changed

springboot-hbase/pom.xml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>springboot</groupId>
7+
<artifactId>springboot-hbase</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<name>springboot-hbase :: 集成 HBase</name>
10+
11+
<!-- Spring Boot 启动父依赖 -->
12+
<parent>
13+
<groupId>org.springframework.boot</groupId>
14+
<artifactId>spring-boot-starter-parent</artifactId>
15+
<version>1.5.6.RELEASE</version>
16+
</parent>
17+
18+
<properties>
19+
<hbase-spring-boot>1.0.0.RELEASE</hbase-spring-boot>
20+
<mysql-connector>5.1.39</mysql-connector>
21+
</properties>
22+
23+
<dependencies>
24+
25+
<!-- Spring Boot Web 依赖 -->
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-web</artifactId>
29+
</dependency>
30+
31+
<!-- Spring Boot Test 依赖 -->
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-test</artifactId>
35+
<scope>test</scope>
36+
</dependency>
37+
38+
<!-- Spring Boot HBase 依赖 -->
39+
<dependency>
40+
<groupId>com.spring4all</groupId>
41+
<artifactId>spring-boot-starter-hbase</artifactId>
42+
<version>${hbase-spring-boot}</version>
43+
</dependency>
44+
45+
<!-- Junit -->
46+
<dependency>
47+
<groupId>junit</groupId>
48+
<artifactId>junit</artifactId>
49+
<version>4.12</version>
50+
</dependency>
51+
</dependencies>
52+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.spring.springboot;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
* Spring Boot 应用启动类
8+
*
9+
* Created by bysocket on 16/4/26.
10+
*/
11+
// Spring Boot 应用的标识
12+
@SpringBootApplication
13+
public class Application {
14+
15+
public static void main(String[] args) {
16+
// 程序启动入口
17+
// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
18+
SpringApplication.run(Application.class,args);
19+
}
20+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.spring.springboot.controller;
2+
3+
import org.spring.springboot.domain.City;
4+
import org.spring.springboot.service.CityService;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RequestMethod;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
/**
11+
* Created by bysocket on 07/02/2017.
12+
*/
13+
@RestController
14+
public class CityRestController {
15+
16+
@Autowired
17+
private CityService cityService;
18+
19+
@RequestMapping(value = "/api/city/save", method = RequestMethod.GET)
20+
public City save() {
21+
cityService.saveOrUpdate();
22+
City city = new City();
23+
city.setAge(1);
24+
return city;
25+
}
26+
27+
@RequestMapping(value = "/api/city/get", method = RequestMethod.GET)
28+
public City getCity() {
29+
return cityService.query("135xxxxxx");
30+
}
31+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.spring.springboot.dao;
2+
3+
import com.spring4all.spring.boot.starter.hbase.api.RowMapper;
4+
import org.apache.hadoop.hbase.client.Result;
5+
import org.apache.hadoop.hbase.util.Bytes;
6+
import org.spring.springboot.domain.City;
7+
8+
public class CityRowMapper implements RowMapper<City> {
9+
10+
private static byte[] COLUMN_FAMILY = "f".getBytes();
11+
private static byte[] NAME = "name".getBytes();
12+
private static byte[] AGE = "age".getBytes();
13+
14+
@Override
15+
public City mapRow(Result result, int rowNum) throws Exception {
16+
String name = Bytes.toString(result.getValue(COLUMN_FAMILY, NAME));
17+
int age = Bytes.toInt(result.getValue(COLUMN_FAMILY, AGE));
18+
19+
City dto = new City();
20+
dto.setCityName(name);
21+
dto.setAge(age);
22+
return dto;
23+
}
24+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.spring.springboot.domain;
2+
3+
/**
4+
* 城市实体类
5+
*
6+
* Created by bysocket on 07/02/2017.
7+
*/
8+
public class City {
9+
10+
/**
11+
* 城市编号
12+
*/
13+
private Long id;
14+
15+
/**
16+
* 省份年龄
17+
*/
18+
private Integer age;
19+
20+
/**
21+
* 城市名称
22+
*/
23+
private String cityName;
24+
25+
public Long getId() {
26+
return id;
27+
}
28+
29+
public void setId(Long id) {
30+
this.id = id;
31+
}
32+
33+
public Integer getAge() {
34+
return age;
35+
}
36+
37+
public void setAge(Integer age) {
38+
this.age = age;
39+
}
40+
41+
public String getCityName() {
42+
return cityName;
43+
}
44+
45+
public void setCityName(String cityName) {
46+
this.cityName = cityName;
47+
}
48+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.spring.springboot.service;
2+
3+
import org.spring.springboot.domain.City;
4+
5+
import java.util.List;
6+
7+
/**
8+
* 城市业务逻辑接口类
9+
* <p>
10+
* Created by bysocket on 07/02/2017.
11+
*/
12+
public interface CityService {
13+
14+
List<City> query(String startRow, String stopRow);
15+
16+
public City query(String row);
17+
18+
void saveOrUpdate();
19+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.spring.springboot.service.impl;
2+
3+
import com.spring4all.spring.boot.starter.hbase.api.HbaseTemplate;
4+
import org.apache.hadoop.hbase.client.Mutation;
5+
import org.apache.hadoop.hbase.client.Put;
6+
import org.apache.hadoop.hbase.client.Scan;
7+
import org.apache.hadoop.hbase.util.Bytes;
8+
import org.spring.springboot.dao.CityRowMapper;
9+
import org.spring.springboot.domain.City;
10+
import org.spring.springboot.service.CityService;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.stereotype.Service;
13+
14+
import java.util.ArrayList;
15+
import java.util.List;
16+
17+
/**
18+
* 城市业务逻辑实现类
19+
* <p>
20+
* Created by bysocket on 07/02/2017.
21+
*/
22+
@Service
23+
public class CityServiceImpl implements CityService {
24+
25+
@Autowired private HbaseTemplate hbaseTemplate;
26+
27+
public List<City> query(String startRow, String stopRow) {
28+
Scan scan = new Scan(Bytes.toBytes(startRow), Bytes.toBytes(stopRow));
29+
scan.setCaching(5000);
30+
List<City> dtos = this.hbaseTemplate.find("people_table", scan, new CityRowMapper());
31+
return dtos;
32+
}
33+
34+
public City query(String row) {
35+
City dto = this.hbaseTemplate.get("people_table", row, new CityRowMapper());
36+
return dto;
37+
}
38+
39+
public void saveOrUpdate() {
40+
List<Mutation> saveOrUpdates = new ArrayList<Mutation>();
41+
Put put = new Put(Bytes.toBytes("135xxxxxx"));
42+
put.addColumn(Bytes.toBytes("people"), Bytes.toBytes("name"), Bytes.toBytes("test"));
43+
saveOrUpdates.add(put);
44+
45+
this.hbaseTemplate.saveOrUpdates("people_table", saveOrUpdates);
46+
}
47+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## HBase 配置
2+
spring.data.hbase.quorum=xxx
3+
spring.data.hbase.rootDir=xxx
4+
spring.data.hbase.nodeParent=xxx

0 commit comments

Comments
 (0)