es整合项目
发布日期:2021-05-10 15:46:13 浏览次数:31 分类:精选文章

本文共 3626 字,大约阅读时间需要 12 分钟。

数据库到Elasticsearch的同步配置及Spring Boot项目搭建

一、数据库到Elasticsearch的同步配置

在实际项目中,通常需要将数据库中的数据实时同步到Elasticsearch中,以便后续的搜索、分析等操作。以下是实现这一过程的详细步骤:

1. 数据库准备

在数据库中选择需要同步的数据资源,确保数据库的连接权限和驱动程序已正确配置。

2. 配置Logstash的同步配置文件

sync_tanle.cfg文件中,添加以下配置:

input {  jdbc {    jdbc_connection_string => "jdbc:mysql://192.168.230.1:3306/ddd?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT+8"    jdbc_user => "root"    jdbc_password => "123123"    jdbc_driver_library => "/opt/logstash-7.7.0/config/sync/mysql-connector-java-8.0.13.jar"    jdbc_driver_class => "com.mysql.cj.jdbc.Driver"    jdbc_paging_enabled => true    jdbc_page_size => "50000"    jdbc_default_timezone => "Asia/Shanghai"    statement => "SELECT p.id, p.name, p.brand_name, p.product_category_name, p.publish_status, p.new_status, p.sub_title, ps.sku_code, ps.price, ps.pic, ps.sale, ps.sp_data FROM pms_product p LEFT JOIN pms_sku_stock ps ON p.id = ps.product_id GROUP BY p.name"    schedule => "* * * * *"    use_column_value => true    tracking_column => "create_time"    tracking_column_type => "timestamp"    last_run_metadata_path => "area_logstash_capital_bill_last_id"    clean_run => false  }}filter {  date {    match => [ "create_time", "yyyy-MM-dd HH:mm:ss" ]    timezone => "Asia/Shanghai"  }}output {  elasticsearch {    hosts => ["192.168.230.134:9200"]    index => "jq_product"    document_id => "%{id}"    template_overwrite => true  }  stdout {    codec => "json_lines"  }}

3. 执行同步命令

在Logstash的bin目录下,运行以下命令:

./logstash -f /opt/logstash-7.7.0/config/sync/sync_tanle.cfg

注意:在重新执行同步时,请先查询进程并终止之前的Logstash进程。

4. 检查同步效果

可以通过检查Elasticsearch中的索引状态和数据量来确认同步是否成功。


二、在IDEA中创建Spring Boot项目服务

本节将介绍如何在IDEA中创建一个Spring Boot项目,并配置其与Elasticsearch的集成。

1. 创建项目

在IDEA中,选择“File” > “New Project”,填写项目信息,选择“Spring Boot”项目类型,并点击“Next”。

2. 配置项目依赖

pom.xml中添加必要的依赖:

org.springframework.boot
spring-boot-starter-data-elasticsearch

3. 配置应用程序属性

application.properties中添加以下配置:

spring:  application:    name: jq-search  elasticsearch:    rest:      uris: http://192.168.230.134:9200  mybatis-plus:    configuration:      log-impl: org.apache.ibatis.logging.stdout.StdOutImplserver:  port: 5053eureka:  client:    service-url:      defaultZone: http://localhost:5051/eureka

4. 启动类配置

在主启动类中添加@Exclude注解,避免默认数据源配置:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})public class JqSearchApplication {    public static void main(String[] args) {        SpringApplication.run(JqSearchApplication.class, args);    }}

5. 创建索引类

定义用于Elasticsearch的索引类:

@Data@Document(indexName = "jq_product")public class SearchModel {    private Long id;    @Field(type = FieldType.Text, analyzer = "ik_max_word")    private String name;    @Field(type = FieldType.Text)    private String brand_name;    // ... 其他字段}

6. 创建控制器类

实现与Elasticsearch的交互:

@RestController@RequestMapping("search")public class SearchController {    @Autowired    private ElasticsearchRestTemplate elasticsearchRestTemplate;    @GetMapping()    public Object search(String name) {        Pageable pageable = PageRequest.of(0, 999);        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();        NativeSearchQuery query = builder.withQuery(QueryBuilders.queryStringQuery(name))                                      .withPageable(pageable)                                      .build();        SearchHits
search = elasticsearchRestTemplate.search(query, SearchModel.class); return search.get(); }}

7. 测试

通过浏览器访问http://localhost:5053/search进行测试。


通过以上步骤,可以实现数据库数据的实时同步到Elasticsearch,以及在IDEA中创建一个与Elasticsearch交互的Spring Boot项目。

上一篇:使用ElasticsearchRestTemplate.search
下一篇:linux开始安装jdk 和tomcat

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2025年04月30日 16时10分14秒