+ * @author hejianfeng
+ * @date 2019/10/5
+ * @param
+ * @return
+ *
+ * 修改记录
+ * 版本号 修订日期 修改人 bug编号 修改内容
+ * 1.0.0 2019/10/5 hejianfeng 新建
+ *
+ */
+ static class ClearTask extends TimerTask {
+ private String key;
+
+ public ClearTask(String key) {
+ this.key = key;
+ }
+
+ @Override
+ public void run() {
+ CacheUtil.remove(key);
+ }
+
+ }
+
+ //==================缓存的增删改查
+
+ /**
+ *
+ * 添加缓存
+ *
+ * @author hejianfeng
+ * @date 2019/10/5
+ * @param
+ * @param
+ * @return void
+ *
+ * 修改记录
+ * 版本号 修订日期 修改人 bug编号 修改内容
+ * 1.0.0 2019/10/5 hejianfeng 新建
+ *
+ */
+ public boolean put(String key,Object object) {
+ if (checkCapacity()) {
+ /* String res = HttpUtils.doGet(uyun_alert_url, null);
+ JSONObject jsonObject=JSONObject.parseObject(res);
+ JSONArray recordsArray = jsonObject.getJSONArray("records");
+ Map alertMap=recordsArray.stream().collect(Collectors.toMap(item ->((JSONObject)item).getString("name"), Function.identity(), (item, item1) -> item));
+*/
+
+ //默认缓存时间
+ timer.schedule(new ClearTask(key), DEFAULT_TIMEOUT);
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ *
+ * 添加缓存
+ *
+ * @author hejianfeng
+ * @date 2019/10/5
+ * @param key
+ * @param object
+ * @param time_out :缓存过期时间:单位秒
+ * @return void
+ *
+ * 修改记录
+ * 版本号 修订日期 修改人 bug编号 修改内容
+ * 1.0.0 2019/10/5 hejianfeng 新建
+ *
+ */
+ public static boolean put(String key, Object object, int time_out) {
+ if (checkCapacity()) {
+ map.put(key, object);
+ //默认缓存时间
+ timer.schedule(new ClearTask(key), time_out * SECOND_TIME);
+ }
+ return false;
+ }
+
+
+ /**
+ *
+ * 判断容量大小
+ *
+ * @author hejianfeng
+ * @date 2019/10/5
+ * @param
+ * @return boolean
+ *
+ * 修改记录
+ * 版本号 修订日期 修改人 bug编号 修改内容
+ * 1.0.0 2019/10/5 hejianfeng 新建
+ *
+ */
+ public static boolean checkCapacity() {
+ return map.size() < MAX_CAPACITY;
+ }
+
+ /**
+ *
+ * 批量增加缓存
+ *
+ * @author hejianfeng
+ * @date 2019/10/5
+ * @param m
+ * @param time_out
+ * @return void
+ *
+ * 修改记录
+ * 版本号 修订日期 修改人 bug编号 修改内容
+ * 1.0.0 2019/10/5 hejianfeng 新建
+ *
+ */
+ public static boolean put(Map m, int time_out) {
+ if (map.size() + m.size() <= MAX_CAPACITY) {
+ map.putAll(map);
+ for (String key : m.keySet()) {
+ timer.schedule(new ClearTask(key), time_out * SECOND_TIME);
+ }
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ *
+ * 删除缓存
+ *
+ * @author hejianfeng
+ * @date 2019/10/5
+ * @param key
+ * @return void
+ *
+ * 修改记录
+ * 版本号 修订日期 修改人 bug编号 修改内容
+ * 1.0.0 2019/10/5 hejianfeng 新建
+ *
+ */
+ public static void remove(String key) {
+ map.remove(key);
+ }
+
+ /**
+ *
+ * 清除所有缓存
+ *
+ * @author hejianfeng
+ * @date 2019/10/5
+ * @param
+ * @return void
+ *
+ * 修改记录
+ * 版本号 修订日期 修改人 bug编号 修改内容
+ * 1.0.0 2019/10/5 hejianfeng 新建
+ *
+ */
+ public void clearAll() {
+ if (map.size() > 0) {
+ map.clear();
+ }
+ timer.cancel();
+ }
+
+ /**
+ *
+ * 获取缓存
+ *
+ * @author hejianfeng
+ * @date 2019/10/5
+ * @param key
+ * @return java.lang.Object
+ *
+ * 修改记录
+ * 版本号 修订日期 修改人 bug编号 修改内容
+ * 1.0.0 2019/10/5 hejianfeng 新建
+ *
+ */
+ public static Object get(String key) {
+ return map.get(key);
+ }
+
+ /**
+ *
+ * 是否包含某个缓存
+ *
+ * @author hejianfeng
+ * @date 2019/10/5
+ * @param key
+ * @return boolean
+ *
+ * 修改记录
+ * 版本号 修订日期 修改人 bug编号 修改内容
+ * 1.0.0 2019/10/5 hejianfeng 新建
+ *
+ */
+ public static boolean isContain(String key) {
+ return map.contains(key);
+ }
+
+
+ public static void main(String[] args) {
+ /* Map mp=new HashMap<>();
+ mp.put("应用",0);
+ mp.put("中间件",0);
+ mp.put("数据库",0);
+ mp.put("服务器",0);
+ mp.put("网络设备",0);
+ mp.put("全流程",0);
+ mp.put("云管",0);
+ mp.put("alertTotal",0);
+ mp.put("安全事件",0);
+ map.put("alertInfo", mp);
+ CacheUtil.put("alertInfo",mp,20);*/
+
+
+
+ Object alertInfo = CacheUtil.get("alertInfo");
+ System.out.println(alertInfo);
+
+ }
+
+
+
+}
diff --git a/spring-boot-demo-alwaysuse/src/main/java/com/example/awaysuse/util/CommonUtils.java b/spring-boot-demo-alwaysuse/src/main/java/com/example/awaysuse/util/CommonUtils.java
new file mode 100644
index 000000000..01cdd0611
--- /dev/null
+++ b/spring-boot-demo-alwaysuse/src/main/java/com/example/awaysuse/util/CommonUtils.java
@@ -0,0 +1,54 @@
+package com.example.awaysuse.util;
+
+import java.math.BigDecimal;
+
+/**
+ * Created by clt on 2020/3/12.
+ * 数据的基本算法
+ */
+public class CommonUtils {
+ public static Boolean isNull(Object str) {
+ if(null==str||str.toString().trim().equals("")||str.toString().trim().equals("null")){
+ return true;
+ }
+ return false;
+ }
+ public static Double calcRate(Integer coActualNum, Integer coTdNum) {
+ if (coActualNum != null && coTdNum != null && coTdNum != 0 ) {
+ return 1.0 * coActualNum / coTdNum;
+ } else if (coTdNum == null || coTdNum == 0) {
+ return 0.0;
+ } else {
+ return 0.0;
+ }
+ }
+
+ public static String isNullOrStr(Object c_siteopf_id) {
+ if (c_siteopf_id==null){
+ return "";
+ }
+ return c_siteopf_id.toString().trim();
+ }
+ public static Integer isNullOrInter(Object c_siteopf_id) {
+ if (c_siteopf_id==null){
+ return 0;
+ }
+ return Integer.valueOf(c_siteopf_id.toString().trim());
+ }
+
+ public static boolean isEquals(String c_snet_id,String s) {
+ if(c_snet_id==null){
+ return false;
+ }
+ if (c_snet_id.equals(s)){
+ return true;
+ }else {
+ return false;
+ }
+ }
+ public static double add(double v1, double v2) {
+ BigDecimal b1=new BigDecimal(Double.toString(v1));
+ BigDecimal b2 = new BigDecimal(Double.toString(v2));
+ return b1.add(b2).doubleValue();
+ }
+}
diff --git a/spring-boot-demo-alwaysuse/src/main/java/com/example/awaysuse/util/CompareTime.java b/spring-boot-demo-alwaysuse/src/main/java/com/example/awaysuse/util/CompareTime.java
new file mode 100644
index 000000000..bfe3a972e
--- /dev/null
+++ b/spring-boot-demo-alwaysuse/src/main/java/com/example/awaysuse/util/CompareTime.java
@@ -0,0 +1,56 @@
+package com.example.awaysuse.util;
+
+import com.example.awaysuse.model.Person;
+import org.apache.commons.lang3.builder.ToStringExclude;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.*;
+import java.util.stream.Collectors;
+
+public class CompareTime {
+ private static SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
+
+ /**
+ * 判断当前时间是否在某个时间段内
+ * begin 开始时间字符串 String begin="09:00:00";
+ * end 结束时间字符串 String end="12:00:00";
+ */
+ public static boolean compareTime(String begin, String end) {
+ boolean result = false;
+ //将时间字符串转化成时间
+
+ try {
+ //转换成时间格式
+ Date beginTime = df.parse(begin);
+ Date endTime = df.parse(end);
+ //取出当前时间的时分秒编码再解码
+ Date date = df.parse(df.format(new Date()));
+ //通过日历形式开始比较
+ Calendar b = Calendar.getInstance();
+ b.setTime(beginTime);
+ Calendar e = Calendar.getInstance();
+ e.setTime(endTime);
+ Calendar d = Calendar.getInstance();
+ d.setTime(date);
+ //当前时间晚于开始时间,早于结束时间则表明在指定的时间段内
+ if (d.after(b) && d.before(e)) {
+ result = true;
+ }
+ } catch (ParseException e1) {
+ e1.printStackTrace();
+
+ }
+ return result;
+ }
+
+ public static void main(String[] args) {
+ /* boolean b = compareTime("09:00:00", "18:00:00");
+ System.out.println(b);*/
+
+
+ }
+
+
+
+}
diff --git a/spring-boot-demo-alwaysuse/src/main/java/com/example/awaysuse/util/DataCalcUtils.java b/spring-boot-demo-alwaysuse/src/main/java/com/example/awaysuse/util/DataCalcUtils.java
new file mode 100644
index 000000000..e7f92ac63
--- /dev/null
+++ b/spring-boot-demo-alwaysuse/src/main/java/com/example/awaysuse/util/DataCalcUtils.java
@@ -0,0 +1,115 @@
+package com.example.awaysuse.util;
+
+import java.math.BigDecimal;
+
+/**
+ * @Description 一些常用的数学计算
+ */
+public class DataCalcUtils {
+
+ /**
+ * 默认精确到2位小数
+ */
+ private static final int DEF_DIV_SCALE = 2;
+
+ private DataCalcUtils() {
+ }
+
+ /**
+ * 提供精确的加法运算。
+ *
+ * @param v1
+ * 被加数
+ * @param v2
+ * 加数
+ * @return 两个参数的和
+ */
+ public static double add(double v1, double v2) {
+ BigDecimal b1 = new BigDecimal(Double.toString(v1));
+ BigDecimal b2 = new BigDecimal(Double.toString(v2));
+ return b1.add(b2).doubleValue();
+ }
+
+ /**
+ * 提供精确的减法运算。
+ *
+ * @param v1
+ * 被减数
+ * @param v2
+ * 减数
+ * @return 两个参数的差
+ */
+ public static double sub(double v1, double v2) {
+ BigDecimal b1 = new BigDecimal(Double.toString(v1));
+ BigDecimal b2 = new BigDecimal(Double.toString(v2));
+ return b1.subtract(b2).doubleValue();
+ }
+
+ /**
+ * 提供精确的乘法运算。
+ *
+ * @param v1
+ * 被乘数
+ * @param v2
+ * 乘数
+ * @return 两个参数的积
+ */
+ public static double mul(double v1, double v2) {
+ BigDecimal b1 = new BigDecimal(Double.toString(v1));
+ BigDecimal b2 = new BigDecimal(Double.toString(v2));
+ return b1.multiply(b2).doubleValue();
+ }
+
+ /**
+ * 提供(相对)精确的除法运算,当发生除不尽的情况时,精确到 小数点以后10位,以后的数字四舍五入。
+ *
+ * @param v1
+ * 被除数
+ * @param v2
+ * 除数
+ * @return 两个参数的商
+ */
+ public static double div(double v1, double v2) {
+ return div(v1, v2, DEF_DIV_SCALE);
+ }
+
+ /**
+ * 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指 定精度,以后的数字四舍五入。
+ *
+ * @param v1
+ * 被除数
+ * @param v2
+ * 除数
+ * @param scale
+ * 表示表示需要精确到小数点以后几位。
+ * @return 两个参数的商
+ */
+ public static double div(double v1, double v2, int scale) {
+ if (scale < 0) {
+ throw new IllegalArgumentException(
+ "The scale must be a positive integer or zero");
+ }
+ BigDecimal b1 = new BigDecimal(Double.toString(v1));
+ BigDecimal b2 = new BigDecimal(Double.toString(v2));
+ return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
+ }
+
+ /**
+ * 提供精确的小数位四舍五入处理。
+ *
+ * @param v
+ * 需要四舍五入的数字
+ * @param scale
+ * 小数点后保留几位
+ * @return 四舍五入后的结果
+ */
+ public static double round(double v, int scale) {
+ if (scale < 0) {
+ throw new IllegalArgumentException(
+ "The scale must be a positive integer or zero");
+ }
+ BigDecimal b = new BigDecimal(Double.toString(v));
+ BigDecimal one = new BigDecimal("1");
+ return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
+ }
+}
diff --git a/spring-boot-demo-alwaysuse/src/main/java/com/example/awaysuse/util/DemoResourceSync.java b/spring-boot-demo-alwaysuse/src/main/java/com/example/awaysuse/util/DemoResourceSync.java
new file mode 100644
index 000000000..de8e21589
--- /dev/null
+++ b/spring-boot-demo-alwaysuse/src/main/java/com/example/awaysuse/util/DemoResourceSync.java
@@ -0,0 +1,164 @@
+package com.example.awaysuse.util;
+
+
+import com.alibaba.fastjson.JSONObject;
+
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+
+public class DemoResourceSync {
+
+ public static void main(String[] args) {
+ DemoResourceSync dr = new DemoResourceSync();
+ dr.run();
+ }
+
+ public void run() {
+ ResThread resThread = new ResThread();
+ for (int i = 0; i < 10; i++) { //10个线程去跑
+ new Thread(resThread, "线程" + i).start();
+ }
+
+ PerfThread perfThread = new PerfThread();
+ for (int i = 0; i < 20; i++) { //20个线程去跑
+ new Thread(perfThread, "线程" + i).start();
+ }
+
+ StateThread stateThread = new StateThread();
+ for (int i = 0; i < 20; i++) { //20个线程去跑
+ new Thread(stateThread, "线程" + i).start();
+ }
+ }
+
+ /**
+ * 同步资源
+ * @author Administrator
+ *
+ */
+ private class ResThread implements Runnable {
+
+ public ResThread() {
+ }
+
+ @Override
+ public void run() {
+ synchronized (this) {
+ SimpleDateFormat sdf = new SimpleDateFormat(
+ "yyyy-MM-dd HH:mm:ss");
+ String datestr = sdf.format(new Date());
+ String str = "[";
+ for (int i = 0; i < 900; i++) {
+ str += "{\"classCode\":\"Switch\",\"domainId\":\"rootDomain\",\"sourceId\":\"192.168."
+ + Thread.currentThread().getId()
+ + "."
+ + i
+ + "\",\"updateTime\":\""
+ + datestr
+ + "\",\"values\":{\"name\":\"交换机"
+ + Thread.currentThread().getId()
+ + i
+ + "\",\"ipAddr\":\"192.168."
+ + Thread.currentThread().getId()
+ + "."
+ + i
+ + "\"}},";
+ }
+ str = str.substring(0, str.length() - 1) + "]";
+ System.out.println("resThread:"+str);
+ String url = "http://127.0.0.1:8890/api/v2/cmdb/cis/save-batch";
+ try {
+ JSONObject jsonObject = HttpClientUtils.httpPost(url, str);
+ String result = jsonObject.getString("result");
+ System.out.println("resThreadResult"
+ + Thread.currentThread().getId() + ":" + result);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ }
+
+ /**
+ * 同步性能
+ * @author Administrator
+ *
+ */
+ private class PerfThread implements Runnable {
+
+ public PerfThread() {
+ }
+
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ Calendar calendar = Calendar.getInstance();
+
+ @Override
+ public void run() {
+ synchronized (this) {
+ String str = "[";
+ for (int i = 500; i > 0; i--) {
+ String datestr = sdf.format(new Date((calendar
+ .getTimeInMillis() - (1000 * Thread.currentThread()
+ .getId() * (i + 1)))));
+ str += "{\"ciId\":\"3a98052b-794c-4531-9182-611625c102a4\",\"groupCode\":\"ram-use\","
+ + "\"sampleTime\":\""
+ + datestr
+ + "\",\"indicators\":{\"mem_used\":500,\"mem_usage\":97}},";
+ }
+ str = str.substring(0, str.length() - 1) + "]";
+ System.out.println("perfThread:"+str);
+ String url = "http://127.0.0.1:8890/api/v2/pmdb/perf-groups/create";
+ try {
+ JSONObject jsonObject = HttpClientUtils.httpPost(url, str);
+ String result = jsonObject.getString("result");
+ System.out.println("perfThreadResult"
+ + Thread.currentThread().getId() + ":" + result);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ }
+
+ /**
+ * 同步状态
+ * @author Administrator
+ *
+ */
+ private class StateThread implements Runnable {
+
+ public StateThread() {
+ }
+
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ Calendar calendar = Calendar.getInstance();
+
+ @Override
+ public void run() {
+ synchronized (this) {
+ String str = "[";
+ for (int i = 500; i > 0; i--) {
+ String datestr = sdf.format(new Date((calendar
+ .getTimeInMillis() - (1000 * Thread.currentThread()
+ .getId() * (i + 1)))));
+ str += "{\"ciId\":\"3a98052b-794c-4531-9182-611625c102a4\",\"typeCode\":\"available_status\","
+ + "\"sampleTime\":\""
+ + datestr
+ + "\",\"value\":\"1\",\"descr\":\"可用\"},";
+ }
+ str = str.substring(0, str.length() - 1) + "]";
+ System.out.println("stateThread:"+str);
+ String url = "http://127.0.0.1:8890/api/v2/pmdb/states/create";
+ try {
+ JSONObject jsonObject = HttpClientUtils.httpPost(url, str);
+ String result = jsonObject.getString("result");
+ System.out.println("stateThreadResult"
+ + Thread.currentThread().getId() + ":" + result);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ }
+}
+
diff --git a/spring-boot-demo-alwaysuse/src/main/java/com/example/awaysuse/util/ESUtils.java b/spring-boot-demo-alwaysuse/src/main/java/com/example/awaysuse/util/ESUtils.java
new file mode 100644
index 000000000..7155f45d8
--- /dev/null
+++ b/spring-boot-demo-alwaysuse/src/main/java/com/example/awaysuse/util/ESUtils.java
@@ -0,0 +1,662 @@
+package com.example.awaysuse.util;
+
+
+import com.example.awaysuse.model.Doc;
+import com.example.awaysuse.model.PageData;
+import lombok.extern.slf4j.Slf4j;
+import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
+import org.elasticsearch.action.bulk.BulkRequest;
+import org.elasticsearch.action.bulk.BulkResponse;
+import org.elasticsearch.action.delete.DeleteRequest;
+import org.elasticsearch.action.delete.DeleteResponse;
+import org.elasticsearch.action.get.GetRequest;
+import org.elasticsearch.action.get.GetResponse;
+import org.elasticsearch.action.index.IndexRequest;
+import org.elasticsearch.action.index.IndexResponse;
+import org.elasticsearch.action.search.ClearScrollRequest;
+import org.elasticsearch.action.search.SearchRequest;
+import org.elasticsearch.action.search.SearchResponse;
+import org.elasticsearch.action.search.SearchScrollRequest;
+import org.elasticsearch.action.support.master.AcknowledgedResponse;
+import org.elasticsearch.action.update.UpdateRequest;
+import org.elasticsearch.action.update.UpdateResponse;
+import org.elasticsearch.client.RequestOptions;
+import org.elasticsearch.client.RestHighLevelClient;
+import org.elasticsearch.client.core.CountRequest;
+import org.elasticsearch.client.core.CountResponse;
+import org.elasticsearch.client.indices.CreateIndexRequest;
+import org.elasticsearch.client.indices.CreateIndexResponse;
+import org.elasticsearch.client.indices.GetIndexRequest;
+import org.elasticsearch.common.settings.Settings;
+import org.elasticsearch.common.unit.TimeValue;
+import org.elasticsearch.common.xcontent.XContentType;
+import org.elasticsearch.index.query.BoolQueryBuilder;
+import org.elasticsearch.index.query.QueryBuilder;
+import org.elasticsearch.rest.RestStatus;
+import org.elasticsearch.search.Scroll;
+import org.elasticsearch.search.SearchHit;
+import org.elasticsearch.search.SearchHits;
+import org.elasticsearch.search.aggregations.AggregationBuilder;
+import org.elasticsearch.search.aggregations.AggregationBuilders;
+import org.elasticsearch.search.aggregations.bucket.terms.Terms;
+import org.elasticsearch.search.aggregations.metrics.Sum;
+import org.elasticsearch.search.aggregations.metrics.TopHits;
+import org.elasticsearch.search.builder.SearchSourceBuilder;
+import org.elasticsearch.search.sort.SortOrder;
+import org.springframework.stereotype.Component;
+import org.springframework.util.CollectionUtils;
+import org.springframework.util.StringUtils;
+
+import javax.annotation.Resource;
+import java.util.*;
+
+/**
+ * @author fengh
+ * @Date: 2020/8/18 10:17
+ * @Description: elasticsearch工具类
+ */
+@Slf4j
+@Component
+public class ESUtils {
+
+ @Resource(name = "restHighLevelClient")
+ private RestHighLevelClient client;
+
+ /**
+ * 检查索引是否存在
+ *
+ * @param index 索引名称
+ * @return boolean
+ */
+ public boolean existIndex(String index) {
+ try {
+ GetIndexRequest request = new GetIndexRequest(index);
+ return client.indices().exists(request, RequestOptions.DEFAULT);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return false;
+ }
+
+ /**
+ * 创建索引
+ *
+ * @param index 索引名称
+ * @return boolean
+ */
+ public boolean createIndex(String index) {
+ return createIndex(index, null);
+ }
+
+ /**
+ * 创建索引
+ *
+ * @param index 索引名称
+ * @param source 配置
+ * @return boolean
+ */
+ public boolean createIndex(String index, String source) {
+ try {
+ CreateIndexRequest createIndexRequest = new CreateIndexRequest(index);
+ //指定字段个数最大10000
+ createIndexRequest.settings(Settings.builder().put("index.mapping.total_fields.limit", 10000));
+ if (!StringUtils.isEmpty(source)) {
+ createIndexRequest.mapping(source, XContentType.JSON);
+ }
+ CreateIndexResponse response = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
+ return response.isAcknowledged();
+ } catch (Exception e) {
+ log.error("创建索引{}异常", index, e);
+ }
+ return false;
+ }
+
+ /**
+ * 删除索引
+ *
+ * @param index 索引名称
+ * @return boolean
+ */
+ public boolean deleteIndex(String index) {
+ try {
+ DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(index);
+ AcknowledgedResponse acknowledgedResponse = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
+ return acknowledgedResponse.isAcknowledged();
+ } catch (Exception e) {
+ log.error("删除索引{}异常", index, e);
+ }
+ return false;
+ }
+
+ /**
+ * 新增(更新)文档:自动生成文档id
+ *
+ * @param index 索引
+ * @param map 保存的数据
+ * @return string
+ */
+ public String save(String index, Map map) {
+ return save(index, map, null);
+ }
+
+ /**
+ * 新增(更新)文档:自定义文档id
+ *
+ * @param index 索引
+ * @param docId 文档id
+ * @param map 保存的数据
+ * @return string
+ */
+ public String save(String index, Map map, String docId) {
+ try {
+ //文档id为空则新增
+ if(StringUtils.isEmpty(docId)){
+ IndexRequest indexRequest = new IndexRequest().index(index).source(map);
+ IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
+ return indexResponse.getId();
+ }
+ UpdateRequest updateRequest = new UpdateRequest().index(index).id(docId).doc(map).docAsUpsert(true);
+ UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT);
+ return updateResponse.getId();
+ } catch (Exception e) {
+ log.error("索引{}插入(更新)数据异常:{}", index, e);
+ }
+ return null;
+ }
+
+ /**
+ * 批量插入(更新)文档: 自动生成id
+ *
+ * @param index 索引
+ * @param list 保存的数据
+ * @return boolean
+ */
+ public boolean bulkSave(String index, List