From bccf9487a738af6104369d54356c5e395d415317 Mon Sep 17 00:00:00 2001 From: liqiangqiang Date: Tue, 19 Mar 2019 14:02:54 +0800 Subject: [PATCH] =?UTF-8?q?Spring=20Boot=20=E9=9B=86=E6=88=90=20HBase?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- springboot-hbase/pom.xml | 52 +++++++++++++++++++ .../org/spring/springboot/Application.java | 20 +++++++ .../controller/CityRestController.java | 31 +++++++++++ .../spring/springboot/dao/CityRowMapper.java | 24 +++++++++ .../org/spring/springboot/domain/City.java | 48 +++++++++++++++++ .../springboot/service/CityService.java | 19 +++++++ .../service/impl/CityServiceImpl.java | 47 +++++++++++++++++ .../src/main/resources/application.properties | 4 ++ 8 files changed, 245 insertions(+) create mode 100755 springboot-hbase/pom.xml create mode 100644 springboot-hbase/src/main/java/org/spring/springboot/Application.java create mode 100644 springboot-hbase/src/main/java/org/spring/springboot/controller/CityRestController.java create mode 100644 springboot-hbase/src/main/java/org/spring/springboot/dao/CityRowMapper.java create mode 100644 springboot-hbase/src/main/java/org/spring/springboot/domain/City.java create mode 100644 springboot-hbase/src/main/java/org/spring/springboot/service/CityService.java create mode 100644 springboot-hbase/src/main/java/org/spring/springboot/service/impl/CityServiceImpl.java create mode 100644 springboot-hbase/src/main/resources/application.properties diff --git a/springboot-hbase/pom.xml b/springboot-hbase/pom.xml new file mode 100755 index 0000000..00aca67 --- /dev/null +++ b/springboot-hbase/pom.xml @@ -0,0 +1,52 @@ + + + 4.0.0 + + springboot + springboot-hbase + 0.0.1-SNAPSHOT + springboot-hbase :: 集成 HBase + + + + org.springframework.boot + spring-boot-starter-parent + 1.5.6.RELEASE + + + + 1.0.0.RELEASE + 5.1.39 + + + + + + + org.springframework.boot + spring-boot-starter-web + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + com.spring4all + spring-boot-starter-hbase + ${hbase-spring-boot} + + + + + junit + junit + 4.12 + + + diff --git a/springboot-hbase/src/main/java/org/spring/springboot/Application.java b/springboot-hbase/src/main/java/org/spring/springboot/Application.java new file mode 100644 index 0000000..5070937 --- /dev/null +++ b/springboot-hbase/src/main/java/org/spring/springboot/Application.java @@ -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); + } +} diff --git a/springboot-hbase/src/main/java/org/spring/springboot/controller/CityRestController.java b/springboot-hbase/src/main/java/org/spring/springboot/controller/CityRestController.java new file mode 100644 index 0000000..06470b7 --- /dev/null +++ b/springboot-hbase/src/main/java/org/spring/springboot/controller/CityRestController.java @@ -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"); + } +} diff --git a/springboot-hbase/src/main/java/org/spring/springboot/dao/CityRowMapper.java b/springboot-hbase/src/main/java/org/spring/springboot/dao/CityRowMapper.java new file mode 100644 index 0000000..9db7db9 --- /dev/null +++ b/springboot-hbase/src/main/java/org/spring/springboot/dao/CityRowMapper.java @@ -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 { + + 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; + } +} diff --git a/springboot-hbase/src/main/java/org/spring/springboot/domain/City.java b/springboot-hbase/src/main/java/org/spring/springboot/domain/City.java new file mode 100644 index 0000000..afe589e --- /dev/null +++ b/springboot-hbase/src/main/java/org/spring/springboot/domain/City.java @@ -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; + } +} diff --git a/springboot-hbase/src/main/java/org/spring/springboot/service/CityService.java b/springboot-hbase/src/main/java/org/spring/springboot/service/CityService.java new file mode 100644 index 0000000..cb183e5 --- /dev/null +++ b/springboot-hbase/src/main/java/org/spring/springboot/service/CityService.java @@ -0,0 +1,19 @@ +package org.spring.springboot.service; + +import org.spring.springboot.domain.City; + +import java.util.List; + +/** + * 城市业务逻辑接口类 + *

+ * Created by bysocket on 07/02/2017. + */ +public interface CityService { + + List query(String startRow, String stopRow); + + public City query(String row); + + void saveOrUpdate(); +} diff --git a/springboot-hbase/src/main/java/org/spring/springboot/service/impl/CityServiceImpl.java b/springboot-hbase/src/main/java/org/spring/springboot/service/impl/CityServiceImpl.java new file mode 100644 index 0000000..72b7bf7 --- /dev/null +++ b/springboot-hbase/src/main/java/org/spring/springboot/service/impl/CityServiceImpl.java @@ -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; + +/** + * 城市业务逻辑实现类 + *

+ * Created by bysocket on 07/02/2017. + */ +@Service +public class CityServiceImpl implements CityService { + + @Autowired private HbaseTemplate hbaseTemplate; + + public List query(String startRow, String stopRow) { + Scan scan = new Scan(Bytes.toBytes(startRow), Bytes.toBytes(stopRow)); + scan.setCaching(5000); + List 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 saveOrUpdates = new ArrayList(); + 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); + } +} diff --git a/springboot-hbase/src/main/resources/application.properties b/springboot-hbase/src/main/resources/application.properties new file mode 100644 index 0000000..03ccb62 --- /dev/null +++ b/springboot-hbase/src/main/resources/application.properties @@ -0,0 +1,4 @@ +## HBase 配置 +spring.data.hbase.quorum=xxx +spring.data.hbase.rootDir=xxx +spring.data.hbase.nodeParent=xxx