本文使用SpringBoot框架搭建项目,集成Phoenix,创建RESTful接口获取Hbase数据。
完整代码已放在GitHub上:phoenix-service
在上篇文章HBase创建表以及使用Phoenix操作查询Hbase中,我们已经创建好了hbase表test1:
下面我们开始创建接口获取hbase数据。
项目结构:
开发环境准备:
1.从集群拷贝以下文件:core-site.xml、hbase-site.xml、hdfs-site.xml文件放到resources文件夹下
2.在C:\Windows\System32\drivers\etc文件夹下的hosts文件中加入集群的hostname和IP
1.在pom.xml文件中加入依赖:
<dependency>
<groupId>org.apache.phoenix</groupId>
<artifactId>phoenix-core</artifactId>
<version>5.0.0-HBase-2.0</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.8.4</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.73</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
2.application.yml
spring:
datasource:
driver-class-name: org.apache.phoenix.jdbc.PhoenixDriver
url: jdbc:phoenix:hadoop01,hadoop02,hadoop03:2181
mybatis:
mapper-locations: classpath*:/mapper/**/*.xml
configuration:
map-underscore-to-camel-case: true #开启驼峰命名
3.domain层
import lombok.Data;
@Data
public class User {
private String ROW;
private String id;
private String name;
private String sex;
private String age;
}
4.controller层
@RestController
public class PhoenixController {
@Autowired
private PhoenixServiceImpl phoenixServiceImpl;
//查询所有用户
@RequestMapping("/alluser")
public Object showAllInfo() {
List<User> user= phoenixServiceImpl.getAllUserInfo();
if(user != null){
return user;
}
else{
return "查询为空!";
}
}
//根据id查询用户
@RequestMapping("/user/{id}")
public Object showInfo(@PathVariable("id") String id) {
User user= phoenixServiceImpl.getUserInfo(id);
if(user != null){
return user;
}
else{
return "查询为空!";
}
}
}
5.dao层
@Mapper
public interface PhoenixMapper {
List<User> getAllInfo();//查询所有用户
User getInfo(String id);//根据id查询用户
}
6.service层
@Service
public class PhoenixServiceImpl {
@Autowired
private PhoenixMapper phoenixMapper;
//查询所有用户
public List<User> getAllUserInfo() {
return phoenixMapper.getAllInfo();
}
//根据id查询用户
public User getUserInfo(String id) {
return phoenixMapper.getInfo(id);
}
}
7.mapper/PhoenixMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.bj.phoenix.dao.PhoenixMapper">
//查询所有用户
<select id="getAllInfo" resultType="com.bj.phoenix.domain.User" >
select *
from "test1"
</select>
//根据id查询用户
<select id="getInfo" resultType="com.bj.phoenix.domain.User" parameterType="String">
select *
from "test1" where "id" = #{id}
</select>
</mapper>
8.运行PhoenixApplication
运行结果:
至此,成功通过接口获取hbase数据。
附加
java POST方式调用请求此接口获取数据代码:
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
@Slf4j
public class PhoenixRequestTest {
private static final int SUCCESS_CODE = 200;
public static String sendPost(String url, String map) throws Exception{
//我们可以使用一个Builder来设置UA字段,然后再创建HttpClient对象
HttpClientBuilder builder = HttpClients.custom();
//对照UA字串的标准格式理解一下每部分的意思
builder.setUserAgent("Mozilla/5.0(Windows;U;Windows NT 5.1;en-US;rv:0.9.4)");
CloseableHttpClient httpClient = builder.build();
//配置超时时间
RequestConfig requestConfig = RequestConfig.custom().
setConnectTimeout(30000).setConnectionRequestTimeout(30000)
.setSocketTimeout(30000).setRedirectsEnabled(true).build();
HttpPost httpPost = new HttpPost(url);//前面字符串为数据服务的地址
httpPost.setHeader("Content-Type","application/json");
//httpPost.setHeader("Authorization","Bearer\n"+ JWT);
//设置超时时间
httpPost.setConfig(requestConfig);
try {
StringEntity stringEntity = new StringEntity(JSON.toJSONString(map));
//设置post求情参数
httpPost.setEntity(stringEntity);
HttpResponse httpResponse = httpClient.execute(httpPost);
String strResult = "";
if(httpResponse != null){
System.out.println(httpResponse.getStatusLine().getStatusCode());
if (httpResponse.getStatusLine().getStatusCode() == SUCCESS_CODE) {
strResult = EntityUtils.toString(httpResponse.getEntity());
} else{
log.warn("sendPost请求异常,Error Response: {}",httpResponse.getStatusLine().toString());
throw new RuntimeException("sendPost请求异常,Error Response: " + httpResponse.getStatusLine().toString());
}
}
return strResult;
} finally {
try {
if(httpClient != null){
httpClient.close(); //释放资源
}
} catch (IOException e) {
log.error("sendPost连接关闭异常",e);
throw new IOException("sendPost连接关闭异常",e);
}
}
}
public static void main(String[] args) throws Exception {
String result = sendPost("http://localhost:8080/user/1", "1");
log.info(result);
}
}
运行结果: