executor 服务添加ES依赖,添加对小板造型数据入库处理

This commit is contained in:
yangsb
2024-04-11 16:49:13 +08:00
parent d540216721
commit 35fee5454d
13 changed files with 582 additions and 53 deletions
@@ -6,6 +6,7 @@ import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import javax.validation.constraints.NotBlank;
import java.time.LocalDateTime;
import java.util.Date;
import static com.cf.imes.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
import static com.cf.imes.framework.common.util.date.DateUtils.TIME_ZONE_DEFAULT;
@@ -19,11 +20,11 @@ public class ESDocument {
public String id;
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
@JsonFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND, timezone = TIME_ZONE_DEFAULT)
private Date createTime;
private String createTime;
private Long creator;
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
@JsonFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND, timezone = TIME_ZONE_DEFAULT)
private Date updateTime;
private String updateTime;
private Long updater;
private Long organId;
@@ -56,7 +56,7 @@ public interface ESDocumentService {
* @param documents 要增加的对象集合
* @return 批量操作的结果
*/
<T> BulkResponse bulkCreate(String idxName, List<T> documents) throws Exception;
<T> BulkResponse bulkCreate(String idxName, List<?extends ESDocument> documents) throws Exception;
/**
@@ -15,6 +15,11 @@ import com.cf.imes.framework.security.core.util.SecurityFrameworkUtils;
import java.io.IOException;
import java.io.StringReader;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Date;
import java.util.List;
import java.util.Map;
@@ -23,7 +28,7 @@ import java.util.function.BiConsumer;
/**
* @author there
*/
public class ESDocumentServiceImpl implements ESDocumentService{
public class ESDocumentServiceImpl implements ESDocumentService {
//同步客户端
private final ElasticsearchClient elasticsearchClient;
@@ -32,20 +37,24 @@ public class ESDocumentServiceImpl implements ESDocumentService{
private Snowflake snowflake = IdUtil.getSnowflake();
public ESDocumentServiceImpl (ElasticsearchClient elasticsearchClient, ElasticsearchAsyncClient elasticsearchAsyncClient) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_DATE_TIME;
ZoneId utc = ZoneId.of("UTC");
public ESDocumentServiceImpl(ElasticsearchClient elasticsearchClient, ElasticsearchAsyncClient elasticsearchAsyncClient) {
this.elasticsearchClient = elasticsearchClient;
this.elasticsearchAsyncClient = elasticsearchAsyncClient;
}
@Override
public <T> IndexResponse createByFluentDSL(String idxName, String idxId, ESDocument document) throws Exception {
LocalDateTime now = LocalDateTime.now();
LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
document.setCreator(loginUser.getId());
document.setUpdater(loginUser.getId());
document.setOrganId(loginUser.getOrganId());
document.setCreateTime(new Date());
document.setUpdateTime(new Date());
if(StrUtil.isBlank(document.getId())) {
document.setCreateTime((now.atZone(utc).format(dateTimeFormatter)));
document.setUpdateTime((now.atZone(utc).format(dateTimeFormatter)));
if (StrUtil.isBlank(document.getId())) {
document.setId(snowflake.nextIdStr());
}
return elasticsearchClient.index(idx -> idx
@@ -56,21 +65,23 @@ public class ESDocumentServiceImpl implements ESDocumentService{
/**
* BuilderPattern 方式创建文档
* @param idxName 索引名
* @param idxId 索引id
*
* @param idxName 索引
* @param idxId 索引id
* @param document 文档对象
*/
@Override
public <T> IndexResponse createByBuilderPattern(String idxName, String idxId, ESDocument document) throws Exception {
LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
LocalDateTime now = LocalDateTime.now();
document.setCreator(loginUser.getId());
document.setUpdater(loginUser.getId());
document.setOrganId(loginUser.getOrganId());
document.setCreateTime(new Date());
document.setUpdateTime(new Date());
document.setCreateTime((now.atZone(utc).format(dateTimeFormatter)));
document.setUpdateTime((now.atZone(utc).format(dateTimeFormatter)));
IndexRequest.Builder<Object> indexReqBuilder = new IndexRequest.Builder<>();
indexReqBuilder.index(idxName);
if(StrUtil.isBlank(idxId)) {
if (StrUtil.isBlank(idxId)) {
idxId = snowflake.nextIdStr();
}
indexReqBuilder.id(idxId);
@@ -80,13 +91,14 @@ public class ESDocumentServiceImpl implements ESDocumentService{
/**
* json方式创建文档
* @param idxName 索引名
* @param idxId 索引id
*
* @param idxName 索引
* @param idxId 索引id
* @param jsonContent json字符串
*/
@Override
public IndexResponse createByJson(String idxName, String idxId, String jsonContent) throws Exception {
if(StrUtil.isBlank(idxId)) {
if (StrUtil.isBlank(idxId)) {
idxId = snowflake.nextIdStr();
}
String finalIdxId = idxId;
@@ -98,11 +110,12 @@ public class ESDocumentServiceImpl implements ESDocumentService{
}
/**
* 异步方式创建文档
* @param idxName 索引名
* @param idxId 索引id
* 异步方式创建文档
*
* @param idxName 索引
* @param idxId 索引id
* @param document 文档
* @param action 操作
* @param action 操作
*/
@Override
public <T> void createAsync(String idxName, String idxId, T document, BiConsumer<IndexResponse, Throwable> action) {
@@ -115,54 +128,56 @@ public class ESDocumentServiceImpl implements ESDocumentService{
/**
* 批量方式创建文档
* @param idxName 索引名
*
* @param idxName 索引名
* @param documents 要增加的对象集合
*/
@Override
public <T> BulkResponse bulkCreate(String idxName, List<T> documents) throws Exception {
public <T> BulkResponse bulkCreate(String idxName, List<? extends ESDocument> documents) throws Exception {
BulkRequest.Builder br = new BulkRequest.Builder();
LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
Date date = new Date();
documents.forEach(document ->{
ESDocument esDocument = (ESDocument) document;
if(StrUtil.isBlank(esDocument.getId())) {
LocalDateTime now = LocalDateTime.now();
documents.forEach(esDocument -> {
if (StrUtil.isBlank(esDocument.getId())) {
esDocument.setId(snowflake.nextIdStr());
}
esDocument.setCreator(loginUser.getId());
esDocument.setCreateTime(date);
esDocument.setCreateTime(now.atZone(utc).format(dateTimeFormatter));
esDocument.setUpdater(loginUser.getId());
esDocument.setUpdateTime(date);
esDocument.setUpdateTime(now.atZone(utc).format(dateTimeFormatter));
br.operations(op -> op.index(idx -> idx
.index(idxName)
.id(esDocument.getId().toString())
.document(esDocument)));
});
return elasticsearchClient.bulk(br.build());
return elasticsearchClient.bulk(br.build());
}
/**
*
* @param idxName 索引名称
* @param docId 文档id
* @param tClass 返回的类型
* @param map 修改内容的map
* Map<String, Object> map = new HashMap<>();
* map.put("age", 35);
* 把年龄改成35
* @param docId 文档id
* @param tClass 返回的类型
* @param map 修改内容的map
* Map<String, Object> map = new HashMap<>();
* map.put("age", 35);
* 把年龄改成35
*/
@Override
public <T> Result updateById(String idxName, String docId, Class<T> tClass, Map<String,Object> map) throws IOException {
public <T> Result updateById(String idxName, String docId, Class<T> tClass, Map<String, Object> map) throws IOException {
UpdateResponse<T> response = elasticsearchClient.update(e -> e.index(idxName).id(docId).doc(map), tClass);
return response.result();
}
/**
* 文档id查询信息
*
* @param idxName 索引名
* @param docId 文档id
* @param docId 文档id
*/
@Override
public <T> T getById(String idxName, String docId,Class<T> tClass) throws IOException {
public <T> T getById(String idxName, String docId, Class<T> tClass) throws IOException {
GetResponse<T> response = elasticsearchClient.get(g -> g
.index(idxName)
.id(docId),
@@ -172,8 +187,9 @@ public class ESDocumentServiceImpl implements ESDocumentService{
/**
* 根据索引名称和文档id查询ObjectNode
*
* @param idxName 索引名
* @param docId 文档id
* @param docId 文档id
*/
@Override
public JSONObject getObjectNodeById(String idxName, String docId) throws IOException {
@@ -187,8 +203,9 @@ public class ESDocumentServiceImpl implements ESDocumentService{
/**
* 单条输出
*
* @param idxName 索引名
* @param docId 文档id
* @param docId 文档id
*/
@Override
public Boolean deleteById(String idxName, String docId) throws IOException {
@@ -200,8 +217,9 @@ public class ESDocumentServiceImpl implements ESDocumentService{
/**
* 批量删除
*
* @param idxName 索引名
* @param docIds 要删除的文档id集合
* @param docIds 要删除的文档id集合
*/
@Override
public BulkResponse bulkDeleteByIds(String idxName, List<String> docIds) throws Exception {