SpringBoot 数据存储实战拆解!

Elasticsearch是一个基于Apache Lucene开发的的搜索服务,提供了一个分布式多用户能力的全文搜索引擎,并基于RESTful web接口。支持结构化搜索、数据分析、复杂的语言处理、地理位置和对象间关联关系等功能。那么如何在SpringBoot项目中整合ElasticSearch来完成数据的存储呢?下面我们就来看看。

这里需要特别说明在高版本的ElasticSearch对于数据的处理方式与低版本的处理方式是不同的,并且,在Spring Boot中所支持的ElasticSearch客户端版本也是有所差异的。这里演示的是基于 Spring Boot 2.1.4.RELEASE 版本和ElasticSearch 6.8.0的版本处理方式。

依赖和配置

添加Elasticsearch的依赖到pom.xml文件中。

xml 复制代码
<parent>
    <artifactId>spring-boot-parent</artifactId>
    <groupId>org.springframework.boot</groupId>
    <version>2.1.4.RELEASE</version>
</parent>

```js
```
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

在配置文件文件中配置ElasticSearch的连接

ini 复制代码
spring.data.elasticsearch.cluster-name=my-cluster
spring.data.elasticsearch.cluster-nodes=192.168.1.202:9300

这里需要注意,在高版本的SpringBoot配置中已经不支持这样的配置操作,需要注意的实,在这个配置中如果是通过ELK调用则添加的端口是9200,如果是通过Java程序调用则配置的连接端口是9300。

具体实现

typescript 复制代码
@Document(indexName = "my_index", type = "my_type")
public class MyDocument {

    @Id
    private String id;
    private String name;
    private String description;

    // 省略构造函数、getter和setter
}

继承ElasticsearchRepository接口实现数据访问层代码

typescript 复制代码
public interface MyDocumentRepository 
extends ElasticsearchRepository<MyDocument, String> {
}

编写服务层代码

typescript 复制代码
@Service
public class MyDocumentService {

    @Autowired
    private MyDocumentRepository repository;

    public MyDocument save(MyDocument document) {
        return repository.save(document);
    }

    public Iterable<MyDocument> findAll() {
        Iterable<MyDocument> all = repository.findAll();
        return all;
    }

    public MyDocument findById(String id) {
        return repository.findById(id).orElse(null);
    }

    public void deleteById(String id) {
        repository.deleteById(id);
    }
}

编写Controller类进行测试

less 复制代码
@RestController
@RequestMapping("/documents")
public class MyDocumentController {

    @Autowired
    private MyDocumentService service;

    @PostMapping
    public MyDocument create(@RequestBody MyDocument document) {
        return service.save(document);
    }

    @GetMapping
    public Iterable<MyDocument> getAll() {
        Iterable<MyDocument> iterable = service.findAll();
        return iterable;
    }

    @GetMapping("/{id}")
    public MyDocument getById(@PathVariable String id) {
        return service.findById(id);
    }

    @DeleteMapping("/{id}")
    public void deleteById(@PathVariable String id) {
        service.deleteById(id);
    }
}

以上代码示例演示了如何使用Spring Boot整合Elasticsearch实现数据存储。

调用接口进行测试

获取列表

总结

在上面的例子中,通过SpringBoot 2.1.4与ElasticSearch6.8.0完成了整合,如果想要适配更高版本的ElasticSearch或者是Spring Boot ,在过程中一定注意对于版本兼容性的选择。

相关推荐
KD3 小时前
设计模式——责任链模式实战,优雅处理Kafka消息
后端·设计模式·kafka
没逻辑9 小时前
gocron - 分布式定时任务管理系统
后端
程序猿DD10 小时前
人工智能如何改变 Anthropic 的工作方式
java·后端
桦说编程10 小时前
Guava Forwarding系列类详解——装饰器模式实战
java·后端·设计模式
VX:Fegn089511 小时前
计算机毕业设计|基于springboot + vue敬老院管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
算法与双吉汉堡11 小时前
【短链接项目笔记】Day2 用户注册
java·redis·笔记·后端·spring
Victor35611 小时前
Netty(18)Netty的内存模型
后端
Victor35611 小时前
Netty(17)Netty如何处理大量的并发连接?
后端
码事漫谈11 小时前
C++共享内存小白入门指南
后端