当前位置:   article > 正文

SpringBoot整合Elasticsearch,使用定时任务添加数据到仓库_spring boot es添加数据

spring boot es添加数据
添加依赖
<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
  • 1
  • 2
  • 3
  • 4
自动配置属性(如果在本机,不需要配置)
spring.elasticsearch.rest.uris=http://localhost:9200
  • 1
添加实体类
@Document(indexName = "znsd",type = "student")
public class Student {
    private Integer id;
    private String name;
    private String aihao;
    private String sex;

    public Student(Integer id, String name, String aihao, String sex) {
        this.id = id;
        this.name = name;
        this.aihao = aihao;
        this.sex = sex;
    }

    public Student() {
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAihao() {
        return aihao;
    }

    public void setAihao(String aihao) {
        this.aihao = aihao;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", aihao='" + aihao + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
手动自定义的查询
public interface StudentRepository extends ElasticsearchRepository<Student,Integer> {

    //根据书名搜索书籍的方法(类似jpa中的方法命名规则)

	//查询名字
    List<Student> getListByName(String name);
	//查询性别
    List<Student> getListBySex(String sex);
	//查询爱好
    List<Student> getListByAihao(String aihao);
    //查询所有
    List<Student> getAllBy();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
增加数据到仓库
@Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Test
    void contextLoads() {
        Student student1=new Student(1,"中","打篮球","女");
        IndexQuery indexQuery =new IndexQueryBuilder()
                .withId(student1.getId().toString())
                .withObject(student1)
                .build();
        elasticsearchRestTemplate.index(indexQuery);



        Student student2=new Student(2,"中国人","打乒乓球","男");
        IndexQuery indexQuery2 =new IndexQueryBuilder()
                .withId(student2.getId().toString())
                .withObject(student2)
                .build();
        elasticsearchRestTemplate.index(indexQuery2);



        elasticsearchRestTemplate.queryForObject(GetQuery.getById("2"),Student.class);
        Student student3=new Student(3,"中国话","打高尔夫","男");
        IndexQuery indexQuery3 =new IndexQueryBuilder()
                .withId(student3.getId().toString())
                .withObject(student3)
                .build();
        elasticsearchRestTemplate.index(indexQuery3);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
模糊搜索
@Test
    void test3(){
        List<Student> student=studentRepository.getListByName("中");
        for (Student student1:student) {
            System.out.println(student1);
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
搜索结果
Student{id=1, name='中', aihao='打篮球', sex='女'}
Student{id=3, name='中国话', aihao='打高尔夫', sex='男'}
Student{id=2, name='中国人', aihao='打乒乓球', sex='男'}
  • 1
  • 2
  • 3
分页查询
@Test
    void test5(){
        //分页参数
        int page = 0;
        int size = 1;
        // 构建查询条件
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        // 查询id倒序查询
        builder.withSort(SortBuilders.fieldSort("id").order(SortOrder.DESC));
        //模糊查询条件
        builder.withQuery(QueryBuilders.matchPhraseQuery("name", "中"));
        //分页
        builder.withPageable(PageRequest.of(page, size));
        //查询
        Page<Student> page123 =studentRepository.search(builder.build());
        //总记录数
        System.out.println(page123.getTotalElements());
        //当前页数
        System.out.println(page123.getTotalPages());
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
其实这些东西都是要从数据查询出来放入仓库中,所有要使用定时任务,每天定时同步一下。
添加依赖
<!--quartz-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-quartz</artifactId>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
在项目启动入口添加@EnableScheduling注解
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class DemoApplication {

     public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
@Component
public class QuartzDemo {
    @Scheduled(cron = "0 0/1 * * * ?") // 每分钟执行一次
    public static void work() throws Exception {
        System.out.println("执行调度任务:"+new Date());
    }


    @Scheduled(fixedRate = 5000)//每5秒执行一次
    public static void play() throws Exception {
        System.out.println("执行Quartz定时器任务:"+new Date());
    }

    @Scheduled(cron = "0/2 * * * * ?") //每2秒执行一次
    public static void doSomething() throws Exception {
        System.out.println("每2秒执行一个的定时任务:"+new Date());
    }

    @Scheduled(cron = "0 0 0/1 * * ? ") // 每一小时执行一次
    public static void goWork() throws Exception {
        System.out.println("每一小时执行一次的定时任务:"+new Date());
    }

    @Scheduled(cron = "0 37 17 * * ?") // 到17点37执行
    public static void date() throws Exception {
        System.out.println("到了17点37执行:"+new Date());
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
使用jdbc将数据库的数据查询出来,使用定时任务添加到仓库中(略)
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/936282
推荐阅读
相关标签
  

闽ICP备14008679号