赞
踩
前面几篇分析了elk存取日志相关的操作,今天我们持续分享SpringBoot集成ElasticSearch存取数据实战,在项目开发过程中直接使用。而使用SpringBoot整合Elasticsearch,一般都是使用 SpringData 进行封装的,然后再dao层接口继承ElasticsearchRepository 类,该类实现了很多的方法,比如常用的CRUD方法。
1、pom文件引入相关jar:
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- <version>2.1.6.RELEASE</version>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
- </dependency>
2、yml配置es:
- spring
- data:
- elasticsearch:
- cluster-name: nd-es-1 #集群名。(默认值: elasticsearch)
- #cluster-nodes: es-1.cs-ys2.nandao.io:9300
- cluster-nodes: 127.0.0.1:9300 #集群节点地址列表,用逗号分隔。如果没有指定,就启动一个客户端节点。
- repositories:
- enabled: true #开启 Elasticsearch 仓库。(默认值:true。)
注意: 9300 是 Java 客户端的端口。9200 是支持 Restful HTTP 的接口。
3、客户端取数据配置:
3.1、接收数据的VO实体:
-
- import java.io.Serializable;
- import java.util.Date;
- import lombok.Data;
- import lombok.ToString;
- import org.springframework.data.annotation.Id;
- import org.springframework.data.elasticsearch.annotations.Document;
- import org.springframework.data.elasticsearch.annotations.Field;
- import org.springframework.data.elasticsearch.annotations.FieldType;
-
- /**
- * @author nandao 2020/9/27
- */
- @Data
- @Document(indexName = "userInfoindex", type = "userInfo")
- @ToString
- public class UserInfoEsDto implements Serializable {
-
- private static final long serialVersionUID = 9881158963867439519L;
-
- /**
- * es id
- */
- @Id
- private String id;
-
- //关联id
- @Field(type = FieldType.Long)
- private Long jid;
- /**
- * 用户ID
- */
- @Field(type = FieldType.Long)
- private Long userId;
-
- /**
- * 用户名称
- */
- @Field(type = FieldType.Text)
- private String name;
-
- /**
- * 内容
- */
- @ToString.Exclude
- @Field(type = FieldType.Text)
- private String content;
-
- /**
- * 时间
- */
- @Field(type = FieldType.Long)
- private Date publishTime;
-
-
- }
3.2、mapper层接口:
- import com.nandao.service.dto.UserInfoEsDto;
- import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
-
-
- public interface UserInfoRepository extends ElasticsearchRepository<UserInfoEsDto,String> {
-
- }
3.3、业务层保持和查询核心伪代码:
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.data.domain.Page;
- import org.springframework.data.domain.PageRequest;
- import org.springframework.data.domain.Pageable;
- import org.springframework.data.domain.Sort;
- import org.springframework.data.domain.Sort.Direction;
- import org.springframework.data.redis.core.StringRedisTemplate;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.util.CollectionUtils;
-
- @Resource
- private UserInfoRepository userInfoRepository;
-
- @Override
- public R syncUserInfoByIds(SyncArticleReq req) {
- List<UsserInfoEsDto> list = .......
- if (list != null && !list.isEmpty()) {
- userInfoRepository.saveAll(list);
- }
- return R.ok();
- }
-
-
- @Override
- public R listUser(PageParamUtil req,String acceptLanguage) {
- Sort sort = Sort.by(Direction.DESC, "publishTime");
-
- Pageable pageable = PageRequest.of(ReqUtil.checkPageNum(req.getPageNum()), 12, sort);
-
- Page<UsserInfoEsDto> page = userInfoRepository.findAll(pageable);
-
- int total = page.getTotalPages();
- List<UsserInfoEsDto> list = page.getContent();
- //单个保存和删除接口如下
- // userInfoRepository.save(userInfoEsDto);
- //userInfoRepository.deleteById(String.valueOf(req.getId()));
- ........
- }
-
到此实战分享结束,推荐参考一篇文章,可以从另外一个角度测试一下:参考
下篇继续分享es原理,敬请期待!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。