Merge branch 'master' into secondStage
This commit is contained in:
+98
-14
@@ -1,5 +1,6 @@
|
||||
package com.jero.common.es;
|
||||
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -32,6 +33,11 @@ public class JeroElasticsearchTemplate {
|
||||
// ElasticSearch 最大可返回条目数
|
||||
public static final int ES_MAX_SIZE = 10000;
|
||||
|
||||
@Value("${jero.elasticsearch.username}")
|
||||
private String username;
|
||||
@Value("${jero.elasticsearch.password}")
|
||||
private String password;
|
||||
|
||||
public JeroElasticsearchTemplate(@Value("${jero.elasticsearch.cluster-nodes}") String baseUrl, @Value("${jero.elasticsearch.check-enabled}") boolean checkEnabled) {
|
||||
log.debug("JeroElasticsearchTemplate BaseURL:" + baseUrl);
|
||||
if (StringUtils.isNotEmpty(baseUrl)) {
|
||||
@@ -40,7 +46,11 @@ public class JeroElasticsearchTemplate {
|
||||
if (checkEnabled) {
|
||||
try {
|
||||
this.getElasticsearchVersion();
|
||||
RestUtil.get(this.getBaseUrl().toString());
|
||||
String basicAuth = this.getBasicAuth();
|
||||
HttpRequest.get(this.getBaseUrl().toString())
|
||||
.header("Authorization", basicAuth)
|
||||
.execute()
|
||||
.body();
|
||||
log.info("ElasticSearch 服务连接成功");
|
||||
log.info("ElasticSearch version: " + this.version);
|
||||
} catch (Exception e) {
|
||||
@@ -56,8 +66,12 @@ public class JeroElasticsearchTemplate {
|
||||
*/
|
||||
private void getElasticsearchVersion() {
|
||||
if (this.version == null) {
|
||||
String url = this.getBaseUrl().toString();
|
||||
JSONObject result = RestUtil.get(url);
|
||||
String basicAuth = this.getBasicAuth();
|
||||
String response = HttpRequest.get(this.getBaseUrl().toString())
|
||||
.header("Authorization", basicAuth)
|
||||
.execute()
|
||||
.body();
|
||||
JSONObject result = JSONObject.parseObject(response);
|
||||
if (result != null) {
|
||||
JSONObject v = result.getJSONObject("version");
|
||||
this.version = v.getString("number");
|
||||
@@ -65,6 +79,13 @@ public class JeroElasticsearchTemplate {
|
||||
}
|
||||
}
|
||||
|
||||
public String getBasicAuth(){
|
||||
String authString = username + ":" + password;
|
||||
String encode = Base64.getEncoder().encodeToString(authString.getBytes());
|
||||
String authHeader = "Basic " + encode;
|
||||
return authHeader;
|
||||
}
|
||||
|
||||
public StringBuilder getBaseUrl(String indexName, String typeName) {
|
||||
typeName = typeName.trim().toLowerCase();
|
||||
return this.getBaseUrl(indexName).append("/").append(typeName);
|
||||
@@ -79,6 +100,59 @@ public class JeroElasticsearchTemplate {
|
||||
return new StringBuilder("http://").append(this.baseUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* cat 查询ElasticSearch系统数据,返回json
|
||||
*/
|
||||
public JSONArray _cat(String urlAfter) {
|
||||
String url = this.getBaseUrl().append("/_cat").append(urlAfter).append("?").append(FORMAT_JSON).toString();
|
||||
String basicAuth = this.getBasicAuth();
|
||||
String response = HttpRequest.get(url)
|
||||
.header("Authorization", basicAuth)
|
||||
.execute()
|
||||
.body();
|
||||
JSONArray jsonArray = JSONArray.parseArray(response);
|
||||
return jsonArray;
|
||||
}
|
||||
|
||||
public JSONObject put(String url){
|
||||
String basicAuth = this.getBasicAuth();
|
||||
String response = HttpRequest.put(url)
|
||||
.header("Authorization", basicAuth)
|
||||
.execute()
|
||||
.body();
|
||||
JSONObject jsonObject = JSONObject.parseObject(response);
|
||||
return jsonObject;
|
||||
}
|
||||
public JSONObject put(String url, String params){
|
||||
String basicAuth = this.getBasicAuth();
|
||||
String response = HttpRequest.put(url)
|
||||
.body(params)
|
||||
.header("Authorization", basicAuth)
|
||||
.execute()
|
||||
.body();
|
||||
JSONObject jsonObject = JSONObject.parseObject(response);
|
||||
return jsonObject;
|
||||
}
|
||||
public JSONObject post(String url, JSONObject params){
|
||||
String basicAuth = this.getBasicAuth();
|
||||
String response = HttpRequest.post(url)
|
||||
.body(params.toJSONString())
|
||||
.header("Authorization", basicAuth)
|
||||
.execute()
|
||||
.body();
|
||||
JSONObject jsonObject = JSONObject.parseObject(response);
|
||||
return jsonObject;
|
||||
}
|
||||
public JSONObject delete(String url){
|
||||
String basicAuth = this.getBasicAuth();
|
||||
String response = HttpRequest.delete(url)
|
||||
.header("Authorization", basicAuth)
|
||||
.execute()
|
||||
.body();
|
||||
JSONObject jsonObject = JSONObject.parseObject(response);
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
/**
|
||||
* cat 查询ElasticSearch系统数据,返回json
|
||||
*/
|
||||
@@ -107,7 +181,7 @@ public class JeroElasticsearchTemplate {
|
||||
if (!StringUtils.isEmpty(indexName)) {
|
||||
urlAfter.append("/").append(indexName.trim().toLowerCase());
|
||||
}
|
||||
return _cat(urlAfter.toString(), JSONArray.class).getBody();
|
||||
return _cat(urlAfter.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -139,7 +213,12 @@ public class JeroElasticsearchTemplate {
|
||||
public JSONObject getDataById(String indexName, String typeName, String dataId) {
|
||||
String url = this.getBaseUrl(indexName, typeName).append("/").append(dataId).toString();
|
||||
log.info("url:" + url);
|
||||
JSONObject result = RestUtil.get(url);
|
||||
String basicAuth = this.getBasicAuth();
|
||||
String response = HttpRequest.get(this.getBaseUrl().toString())
|
||||
.header("Authorization", basicAuth)
|
||||
.execute()
|
||||
.body();
|
||||
JSONObject result = JSONObject.parseObject(response);
|
||||
boolean found = result.getBoolean("found");
|
||||
if (found) {
|
||||
return result.getJSONObject("_source");
|
||||
@@ -157,7 +236,7 @@ public class JeroElasticsearchTemplate {
|
||||
String url = this.getBaseUrl(indexName).toString();
|
||||
|
||||
try {
|
||||
return RestUtil.put(url).getBoolean("acknowledged");
|
||||
return this.put(url).getBoolean("acknowledged");
|
||||
} catch (org.springframework.web.client.HttpClientErrorException ex) {
|
||||
if (HttpStatus.BAD_REQUEST == ex.getStatusCode()) {
|
||||
log.warn("索引创建失败:" + indexName + " 已存在,无需再创建");
|
||||
@@ -176,7 +255,7 @@ public class JeroElasticsearchTemplate {
|
||||
public boolean removeIndex(String indexName) {
|
||||
String url = this.getBaseUrl(indexName).toString();
|
||||
try {
|
||||
return RestUtil.delete(url).getBoolean("acknowledged");
|
||||
return this.delete(url).getBoolean("acknowledged");
|
||||
} catch (org.springframework.web.client.HttpClientErrorException ex) {
|
||||
if (HttpStatus.NOT_FOUND == ex.getStatusCode()) {
|
||||
log.warn("索引删除失败:" + indexName + " 不存在,无需删除");
|
||||
@@ -204,7 +283,13 @@ public class JeroElasticsearchTemplate {
|
||||
}
|
||||
log.info("getIndexMapping-url:" + url);
|
||||
try {
|
||||
return RestUtil.get(url);
|
||||
String basicAuth = this.getBasicAuth();
|
||||
String response = HttpRequest.get(this.getBaseUrl().toString())
|
||||
.header("Authorization", basicAuth)
|
||||
.execute()
|
||||
.body();
|
||||
JSONObject result = JSONObject.parseObject(response);
|
||||
return result;
|
||||
} catch (org.springframework.web.client.HttpClientErrorException e) {
|
||||
String message = e.getMessage();
|
||||
if (message != null && message.contains("404 Not Found")) {
|
||||
@@ -291,7 +376,7 @@ public class JeroElasticsearchTemplate {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
String result = RestUtil.put(url, data).getString("result");
|
||||
String result = this.put(url, data.toJSONString()).getString("result");
|
||||
return "created".equals(result) || "updated".equals(result);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage() + "\n-- url: " + url + "\n-- data: " + data.toJSONString());
|
||||
@@ -327,8 +412,7 @@ public class JeroElasticsearchTemplate {
|
||||
bodySB.append(data.toJSONString()).append("\n");
|
||||
}
|
||||
log.info("+-+-+-: bodySB.toString(): " + bodySB.toString());
|
||||
HttpHeaders headers = RestUtil.getHeaderApplicationJson();
|
||||
RestUtil.request(url, HttpMethod.PUT, headers, null, bodySB, JSONObject.class);
|
||||
this.put(url,bodySB.toString());
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -340,7 +424,7 @@ public class JeroElasticsearchTemplate {
|
||||
public boolean delete(String indexName, String typeName, String dataId) {
|
||||
String url = this.getBaseUrl(indexName, typeName).append("/").append(dataId).toString();
|
||||
try {
|
||||
return "deleted".equals(RestUtil.delete(url).getString("result"));
|
||||
return "deleted".equals(this.delete(url).getString("result"));
|
||||
} catch (org.springframework.web.client.HttpClientErrorException ex) {
|
||||
if (HttpStatus.NOT_FOUND == ex.getStatusCode()) {
|
||||
return false;
|
||||
@@ -362,7 +446,7 @@ public class JeroElasticsearchTemplate {
|
||||
String url = this.getBaseUrl(indexName, typeName).append("/_search").toString();
|
||||
|
||||
log.info("url:" + url + " ,search: " + queryObject.toJSONString());
|
||||
JSONObject res = RestUtil.post(url, queryObject);
|
||||
JSONObject res = this.post(url, queryObject);
|
||||
log.info("url:" + url + " ,return res: \n" + res.toJSONString());
|
||||
return res;
|
||||
}
|
||||
@@ -486,7 +570,7 @@ public class JeroElasticsearchTemplate {
|
||||
String url = this.getBaseUrl(indexName, typeName).append("/_delete_by_query").toString();
|
||||
|
||||
log.info("url:" + url + " ,delete: " + queryObject.toJSONString());
|
||||
JSONObject res = RestUtil.post(url, queryObject);
|
||||
JSONObject res = this.post(url, queryObject);
|
||||
log.info("url:" + url + " ,return res: \n" + res.toJSONString());
|
||||
return res;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user