pom.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.aliuxy</groupId>
    <artifactId>elastic-api</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>elastic-api</name>
    <description>for elastic-api</description>
    <properties>
        <java.version>1.8</java.version>
        <elasticsearch.version>7.6.1</elasticsearch.version>
        <mybatis-plus.version>3.3.0</mybatis-plus.version>
        <HikariCP.version>3.1.0</HikariCP.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.76</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
		 <!--    手动打的canal依赖包 放在maven的com.xpand下就可以  在百度盘里 -->
        <dependency>
            <groupId>com.xpand</groupId>
            <artifactId>starter-canal</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <!--    HikariCP依赖    -->
        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
            <version>${HikariCP.version}</version>
        </dependency>
        <!--   mybatis-plus   -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>${mybatis-plus.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

yml配置:

spring.elasticsearch.rest.uris=http://10.10.0.17:9200

索引类:

@Data
@Document(indexName = "member_index")
@NoArgsConstructor
@AllArgsConstructor
public class Member {
    @Id
    private Integer id;
    private String name;
    private Integer age;
}

ElasticsearchRestTemplate使用:

	@Test//1 索引创建
    void testCreateIndex() {
        boolean isCreate = restTemplate.indexOps(IndexCoordinates.of("member_index")).create();
        System.out.println(isCreate);
    }
    -------------------------------------------------------------
    @Test//2 获取索引
    void testExist()  {
        boolean exists = restTemplate.indexOps(IndexCoordinates.of("member_index")).exists();
        System.out.println(exists);
    }
    ---------------------------------------------------------------
    @Test//3 删除索引
    void testDelete() {
        boolean delete = restTemplate.indexOps(IndexCoordinates.of("member_index")).delete();
        System.out.println(delete);
    }

	// 插入数据
   @Override
    public void addEsContent(String id) {
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getId, id));
        Member member = new Member();
        member.setId(user.getId());
        member.setName(user.getUsername());
        member.setAge(1);
        System.out.println(member);

        IndexQuery indexQuery = new IndexQueryBuilder()
                .withId(member.getId().toString())
                .withObject(member)
                .build();
        // 插入es数据
        String index = elasticsearchRestTemplate.index(indexQuery, IndexCoordinates.of("member_index"));
        System.out.println(index);
    }

canal监听类

package com.aliuxy.listener;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.aliuxy.pojo.User;
import com.aliuxy.service.ElasticsearchService;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.InsertListenPoint;
import com.xpand.starter.canal.annotation.ListenPoint;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.List;

/**
 *@author liuxingying
 *@description 监听类
 *@since 2021/6/2
 */
@CanalEventListener
public class CanalListener {

    @Resource
    private ElasticsearchService elasticsearchService;

    // 增加监听
    @InsertListenPoint
    public void addListener(CanalEntry.EntryType entryType,CanalEntry.RowData rowData){
        rowData.getAfterColumnsList().forEach(x->{
            if (x.getName().equals("id")){
                elasticsearchService.addEsContent(x.getValue());
            }
//            System.out.println("监听的字段:"+x.getName()+"------"+"监听的值:"+x.getValue());
        });
    }


    // 增加监听
//    @ListenPoint(schema = "es",table = {"user"})
//    public void adUpdate(CanalEntry.EntryType entryType,CanalEntry.RowData rowData){
//        rowData.getAfterColumnsList().forEach(x->{
//            System.out.println("监听的字段:"+x.getName()+"------"+"监听的值:"+x.getValue());
//        });
//    }
}

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐