主页:​写程序的小王叔叔的博客​​欢迎来访

支持:点赞BBoss-ES操作数据及抽ES数据存到List数组_elasticsearchBBoss-ES操作数据及抽ES数据存到List数组_SpringBoot_02​收藏BBoss-ES操作数据及抽ES数据存到List数组_微服务_03关注BBoss-ES操作数据及抽ES数据存到List数组_ELK_04

一、BBoss-ES操作数据 

BBoss-ES操作数据及抽ES数据存到List数组_ELK_05

1、pom.xml

<dependency>
<groupId>com.bbossgroups.plugins</groupId>
<artifactId>bboss-elasticsearch-spring-boot-starter</artifactId>
<version>6.0.0</version>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>

<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>

2、基本配置

elasticsearch:    
host: 10.1*.**.**
port: 9200
bboss:
default:
name: default
elasticPassword: elastic
elasticUser: elastic
elasticsearch:
dateFormat: yyyy.MM.dd
discoverHost: false
rest:
hostNames: 1*.**.**.**:9200
scrollBlockedWaitTimeout: 0
scrollThreadCount: 200
scrollThreadQueue: 200
showTemplate: true
sliceScrollBlockedWaitTimeout: 0
sliceScrollThreadCount: 100
sliceScrollThreadQueue: 100
timeZone: Asia/Shanghai
http:
connectionRequestTimeout: 5000
customHttpRequestRetryHandler: org.frameworkset.spi.remote.http.ConnectionResetHttpRequestRetryHandler
defaultMaxPerRoute: 200
hostnameVerifier: ''
keyPassword: ''
keystore: ''
maxHeaderCount: 200
maxLineLength: -1
maxTotal: 400
retryTime: 1
staleConnectionCheckEnabled: false
timeoutConnection: 5000
timeoutSocket: 5000
validateAfterInactivity: 2000
dslfile:
refreshInterval: -1

3、结构配置

3.1 配置ESSTartConfigurer

import org.frameworkset.elasticsearch.boot.BBossESStarter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

@Configuration
public class ESSTartConfigurer {
@Primary
@Bean(initMethod = "start")
@ConfigurationProperties("spring.elasticsearch.bboss.default")
public BBossESStarter bbossESStarter(){
return new BBossESStarter();

}
}

3.2配置EsQueryToolsConfig

import com.alibaba.fastjson.JSONObject;
import org.frameworkset.elasticsearch.boot.BBossESStarter;
import org.frameworkset.elasticsearch.client.ClientInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class EsQueryToolsConfig {

@Autowired
private BBossESStarter bbossESStarterDefault;

/**
* 用sql查询
* @param sql
* @return
*/
public String queryForSQL(String sql){
ClientInterface clientUtil = bbossESStarterDefault.getRestClient();
JSONObject jsonObject = new JSONObject();
jsonObject.put("sql",sql);
String rtnJson = clientUtil.executeHttp("/_sql/", jsonObject.toJSONString(), ClientInterface.HTTP_POST);
return rtnJson;
}

/**
* 用原生的 es的dsl查询
* @param indexName
* @param dsl
* @return
*/
public String queryForDSL(String indexName,String dsl) {
ClientInterface clientUtil = bbossESStarterDefault.getRestClient();
String rtnJson = clientUtil.executeHttp("/" + indexName + "/_search", dsl, ClientInterface.HTTP_POST);
return rtnJson;
}

}

4、CRUD

4.1 - C

在业务接口 **impl 中,实现基本业务逻辑。

代码块代表当前业务逻辑,具体问题具体分析。

@Service
public class TeacherResourceServiceImpl implements TeacherResourceService {

@Autowired
private TeacherResourceDao teacherResourceDao;

@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;

@Override
public PageInfo<TeacherResourceVO> findTeacherResourceFromES(T*****TO t***TO) throws Exception{
List t*******OsList = new ArrayList();
PageInfo<T******O> teacherResPages = new PageInfo<>();
if (elasticsearchRestTemplate.indexExists("s*******n")){
//聚合获取学校id-教师id的相关信息
String teacherResJson = teacherResourceDao.findTeacherResourceList(t*******TO) ;
JSONObject buildTeacherObj = JSONObject.parseObject(teacherResJson);
JSONArray buildTeacherArr = buildTeacherObj.getJSONObject("aggregations").getJSONObject("b*******_id").getJSONArray("buckets");
for (int i = 0; i < buildTeacherArr.size(); i++) {
TeacherResourceVO teacherResourceVO = new TeacherResourceVO();
JSONObject teacherObj = buildTeacherArr.getJSONObject(i);
if (null != teacherObj) {
String teacherId = teacherObj.getString("key");
String teacherNameJson = teacherResourceDao.getUserNameFromES(teacherId);
JSONObject teacherNameObj = JSONObject.parseObject(teacherNameJson);
String teacherName = teacherNameObj.getJSONObject("hits").getJSONArray("hits").getJSONObject(0).getJSONObject("_source").getString("user_name");
JSONObject schoolNameObj = JSONObject.parseObject(schoolNameJson);
String schoolName = schoolNameObj.getJSONObject("hits").getJSONArray("hits").getJSONObject(0).getJSONObject("_source").getString("organization_name");
teacherResourceVO.setTeacherId(teacherId);
teacherResourceVO.setTeacherName(teacherName);
teacherResourceVO.setRecourceAccount(Integer.parseInt(teacherResCount));
teacherResourceVOsList.add(teacherResourceVO);
}
}
Integer totalNum = teacherResourceVOsList.size();
int pageNum =teacherResourceDTO.getPageNum();
int pageSize = teacherResourceDTO.getPageSize();
Integer totalPage = 0;
if (totalNum > 0) {
totalPage = totalNum % pageSize == 0 ? totalNum / pageSize : totalNum / pageSize + 1;
}
if (pageNum > totalPage) {
pageNum = totalPage;
}
int startPoint = (pageNum - 1) * pageSize;
int endPoint = startPoint + pageSize;
if (totalNum <= endPoint) {
endPoint = totalNum;
}
List PageList = new ArrayList();
PageList = t******OsList.subList(startPoint, endPoint);
teacherResPages.setPageNum(t*****TO.getPageNum());
teacherResPages.setPageSize(t*******O.getPageSize());
teacherResPages.setTotal(t******sList.size());
teacherResPages.setPages(totalPage );
teacherResPages.setList(PageList);
return teacherResPages;
}
return teacherResPages;
}
}

BBoss-ES操作数据及抽ES数据存到List数组_微服务_06在dao层 引入文件【见上】:

@Autowired
private EsQueryToolsConfig esQueryToolsConfig;
/***
* 获取学校教师资源数量
* @param teacherResourceDTO
* @return
* @throws Exception
*/
public String findTeacherResourceList(T*******O *****O) throws Exception{
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//基础sql
String select_sql = "select base_user_id , count(*) as teacherResCount from s*******n ";
String where_sql = "where 1=1 ";
String groupBy_sql = "group by base_user_id ";
String order_sql = " order by teacherResCount desc limit 10";
if(StringUtils.isNotBlank(t********O.getSchoolId())){
where_sql = where_sql.concat(" and b******_id = '" + t*******O.getSchoolId() + "' ");
}
if (StringUtils.isNotBlank(te*****O.getBeginDate())) {
long beginTime = simpleDateFormat.parse(t*****O.getBeginDate() + LoginConstant.START_TIME).getTime();
where_sql = where_sql.concat( " and create_time >= '" + beginTime + "' ");
}
if (StringUtils.isNotBlank(t******O.getEndDate())) {
long endTime = simpleDateFormat.parse(te********O.getEndDate() + LoginConstant.END_TIME).getTime();
where_sql = where_sql.concat( " and create_time < '" + endTime + "' ");
}
String sql = select_sql + where_sql + groupBy_sql ;
String retJson = this.esQueryToolsConfig.queryForSQL( sql );
System.out.println(sql);
return retJson;
}

二、抽ES数据存到List数组

//自定义 学科名称数组
ArrayList arraySubList = new ArrayList();
ArrayList arraySubIdList = new ArrayList();
if (elasticsearchRestTemplate.indexExists("s*******************b")) {
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withIndices("s************b")
.withTypes("_***************")
.withQuery(boolQuery)
.build();
//统计学生的发布课程信息
List<St***************O> s*************Os = elasticsearchRestTemplate.queryForList(searchQuery, S*************O.class);
//根据学生发布课程编号获取学段,学科基本信息
for (int i = 0; i < stuIssueD**********s.size(); i++) {
String strCourseId = stu************s.get(i).getCourse_base_id();
//课程统计
BoolQueryBuilder queryCourseInfo = QueryBuilders.boolQuery();
queryCourseInfo.must(QueryBuilders.termQuery("co******id", strCourseId));
SearchQuery queryCourse = new NativeSearchQueryBuilder()
.withIndices("s****************o")
.withTypes("_d************************")
.withQuery(queryCourseInfo)
.build();
List<Co******O> co*************List = elasticsearchRestTemplate.queryForList(queryCourse, C***********O.class);
for (int j = 0; j < co**********ist.size(); j++) {
BoolQueryBuilder querySubInfo = QueryBuilders.boolQuery();
//学段信息
if (StringUtils.isNotBlank(subjectId)) {
querySubInfo.must(QueryBuilders.termQuery("s******de", subjectId));
} else {
querySubInfo.must(QueryBuilders.termQuery("s***********de", courseVOFromESList.get(j).getBusiness_subject_id()));
}
//学科信息
if (StringUtils.isNotBlank(stageId)) {
querySubInfo.must(QueryBuilders.termQuery("s******de", stageId));
} else {
querySubInfo.must(QueryBuilders.termQuery("s*******de", courseVOFromESList.get(j).getBu*******_id()));
}
SearchQuery querySubjectInfo = new NativeSearchQueryBuilder()
.withIndices("st*********t")
.withTypes("_***********c")
.withQuery(querySubInfo)
.build();
List<Su***********O> subList = elasticsearchRestTemplate.queryForList(querySubjectInfo, Su*********O.class);
//动态添加 - list数组
//动态添加 - list数组
arraySubList.add(subList.get(0).getSubject_name());
arraySubIdList.add(subList.get(0).getSubject_code());
}
}
List<St**********O> s**************List = homeworkService.getQuestionAndknowledgeTotal(s****************O);
for (int i = 0; i < st************List.size(); i++) {
//学生学习的课程数量
st**************List.get(i).setResourceAccount(s************s.size());
stud**************List.get(i).setSubjectId(subjectId);
//学科名称
subList.stream().map(S*******::getSubject_name).collect(Collectors.toList());
// studentSubResourceVOList.get(i).setSubjectName(subName.toString());
}

return studentSubResourceVOs;
}

转载声明:本文为博主原创文章,未经博主允许不得转载

⚠️注意 ~

💯本期内容就结束了,如果内容有误,麻烦大家评论区指出!

如有疑问❓可以在评论区留言💬或私信留言💬,尽我最大能力🏃‍♀️帮大家解决👨‍🏫!

如果我的文章有帮助到您,欢迎点赞+关注✔️鼓励博主🏃,您的鼓励是我分享的动力🏃🏃🏃~