场景
为了简化项目的部署,需要改变原来数据插入方式:
原来的方式:数据发送到kafka,再进入logstatsh,最后到es中
现在的方式:数据直接插入到es中
引入bboss依赖
<!-- BBOSS --> <dependency> <groupId>com.bbossgroups.plugins</groupId> <artifactId>bboss-elasticsearch-spring-boot-starter</artifactId> <version>${bboss.version}</version> <exclusions> <exclusion> <artifactId>slf4j-log4j12</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> </dependency>
代码演示
提醒一下,本项目采用的springboot框架哦
package com.xxl.job.executor.test; import com.xxl.job.executor.utils.CommonUtils; import org.frameworkset.elasticsearch.boot.BBossESStarter; import org.frameworkset.elasticsearch.client.ClientInterface; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @SpringBootTest public class XxlJobExecutorExampleBootApplicationTests { @Autowired private BBossESStarter bbossESStarter; @Test public void test() { String itemName = "process-index"; List<Map<String,Object>> captureDataList = new ArrayList<Map<String,Object>>(); Map<String,Object> metaInfoMap = new HashMap<String, Object>(); metaInfoMap.put("@timestamp", System.currentTimeMillis()); metaInfoMap.put("sign", "esc"); metaInfoMap.put("deptId", 100L); metaInfoMap.put("dbName", "dert"); metaInfoMap.put("itemName", itemName); captureDataList.add(metaInfoMap); ClientInterface clientUtil = bbossESStarter.getRestClient(); clientUtil.addDocuments(CommonUtils.getTimeBaseIndexName(itemName),"_doc",captureDataList); } }
package com.xxl.job.executor.utils; import java.time.LocalDate; import java.time.YearMonth; import java.util.List; public class CommonUtils { /** * 获取带年月后缀的索引名 * @param index * @return */ public static String getTimeBaseIndexName( String indexName ){ LocalDate now = LocalDate.now(); YearMonth yearMonth = YearMonth.from(now); StringBuilder sb = new StringBuilder(); sb.append(indexName); sb.append("_"); sb.append(yearMonth.toString().replace("-", ".")); return sb.toString(); } }