如题,需求如下:
现要求通过调用接口将hive表中的数据回传,但首先要通过token验证,而且token是一天后过期,refresh_token30天过期,而若想要获取最新token值,则需要调用刷新token的接口并将刷新后的token和refresh_token存入数据库以便后续直接从数据库获取。
流程分析:首先,因为接口需要token验证,所以需要先获取token,而获取token则通过授权码auth_code调用另一接口获取,接口返回token和refresh_token,然后调用刷新token的接口,将refresh_token传入,即可刷新token,并将两token存入数据库。最后使用最新token调用接口将hive表里的数据回传。
1.首先创建工具类:HIVE,HTTP以及MYSQL工具类。(示例均为通用工具类)
代码如下:(因数据库连接每天就连接三次,所以这里没创建连接池)
HttpUtils:
public class HttpUtils {
public static String postJson(String url, Object userEntity,String token) throws ParseException, IOException {
RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(globalConfig).build();
CloseableHttpResponse response = null;
HttpEntity responseEntity=null;
String res = "";
try {
HttpPost httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(3600000) //服务器响应超时时间
.setConnectTimeout(60000) //连接服务器超时时间
.build();
httpPost.setConfig(requestConfig);
StringEntity entity = new StringEntity(JSON.toJSONString(userEntity), "utf-8");//也可以直接使用JSONObject
httpPost.setEntity(entity);
httpPost.setHeader("Content-Type", "application/json;charset=utf8");
httpPost.setHeader("Authorization",token);
// 由客户端执行(发送)请求
response = httpClient.execute(httpPost);
// 从响应模型中获取响应实体
responseEntity = response.getEntity();
if (responseEntity != null) {
res= EntityUtils.toString(responseEntity);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 释放资源
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return res;
}
public static String get(String url, String token) throws ParseException, IOException {
RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.IGNORE_COOKIES).build();
CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(globalConfig).build();
CloseableHttpResponse response = null;
HttpEntity responseEntity=null;
String res = "";
try {
HttpGet httpget = new HttpGet(url);
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(2000) //服务器响应超时时间
.setConnectTimeout(2000) //连接服务器超时时间
.build();
httpget.setConfig(requestConfig);
httpget.setHeader("Content-Type", "application/json;charset=utf8");
httpget.setHeader("Authorization",token);
// 由客户端执行(发送)请求
response = httpClient.execute(httpget);
// 从响应模型中获取响应实体
responseEntity = response.getEntity();
if (responseEntity != null) {
res=EntityUtils.toString(responseEntity);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 释放资源
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return res;
}
}
HiveUtils:
public class DBUtil {
public static String url = "jdbc:hive2://IP:端口/数据库";
public static String username = "";
public static String password = "";
public static String pro_url = "jdbc:hive2://IP:端口/数据库";
public static String pro_username = "";
public static String pro_password = "";
public static String driverClassName = "org.apache.hive.jdbc.HiveDriver";
/**
* 获取源数据库连接
* @return
*/
public static Connection getSourceConn() {
Log log = LogFactory.getLog(DBUtil.class);
Connection conn=null;
try {
conn = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
log.error("连接创建失败, 请检查[url]:" + url + ", [user]:" + username + ", [password]:" + password + ")");
}
return conn;
}
/**
* 获取目标数据库连接
* @return
*/
public static Connection getTargetConn(String ispro) {
Log log = LogFactory.getLog(DBUtil.class);
Connection conn=null;
String curr_url;
String curr_username;
String curr_password;
if("pro".equals(ispro)){
curr_url=pro_url;
curr_username=pro_username;
curr_password=pro_password;