springboot对solr进行定时重新创建索引实现增量和全量索引实现

第一步:创建定时器建立任务:
1.主入口添加定时器注解:
package nmrdb;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.scheduling.annotation.EnableScheduling;

/**

  • @Author: ShuaiFu
  • @Description:
  • @Date: Created in 下午 6:20 2019/4/25 0025
    */
    @SpringBootApplication
    @EnableScheduling
    public class WtmnlpApplication extends SpringBootServletInitializer {
    public static void main(String[] args) {
    SpringApplication.run(WtmnlpApplication.class, args);
    }
    }
    2、定时器接口实现:
    package nmrdb.config;

import nmrdb.service.SolrService;
import nmrdb.service.TeskHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**

  • @Author: ShuaiFu

  • @Description:

  • @Date: Created in 下午 10:46 2019/7/30 0030
    */
    @Component
    public class Scheduler {
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
    @Autowired
    private TeskHistoryService teskHistoryService;
    @Autowired
    private SolrService solrService;

    //每隔2秒执行一次
    // long time = 10006060*24;
    @Scheduled(fixedRate = 1000 * 60 * 2)
    // @Scheduled(cron = “0 00 00 ? * *”)
    public void updateSqlTasks() {
    int rows = teskHistoryService.CallBackNmrSourceById();
    System.out.println(“此次更新数据量为:” + rows + " 时间:" + dateFormat.format(new Date()));
    }

    //每天3:05执行
    // @Scheduled(cron = “0 00 00 ? * *”)
    @Scheduled(fixedRate = 1000 * 60 * 3)
    public void updateSolrIndex() {
    boolean delta = true; //进行全量索引
    // boolean delta = false; //进行增量量索引
    boolean confirm = solrService.updateSolrIndex(delta);
    if (confirm){
    System.out.println(“定时进行solr全量索引重建成功! 时间:” + dateFormat.format(new Date()));
    }else {
    System.out.println(“定时进行solr全量索引重建失败!时间:” + dateFormat.format(new Date()));
    }
    }

}
第二步:进行solr缩印的实现:
package nmrdb.service.impl;

import nmrdb.service.SolrService;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;

/**

  • @Author: ShuaiFu

  • @Description:

  • @Date: Created in 下午 3:06 2019/7/31 0031
    */
    @Service
    public class SolrServiceImpl implements SolrService {

    @Autowired
    private HttpSolrClient httpSolrClient;

    @Override
    public boolean updateSolrIndex(boolean delta) {
    boolean confirm = true;

     SolrQuery query = new SolrQuery();
     query.setRequestHandler("/dataimport");
     String command = delta ? "full-import" : "delta-import";
     String clean = delta ? "true" : "false";
     String optimize = delta ? "true" : "false";
    
     query.setParam("command", command)
             .setParam("clean", clean)
             .setParam("commit", "true")
    

// .setParam(“entity”, “”)
.setParam(“optimize”, optimize);
try {
httpSolrClient.query(query);
httpSolrClient.commit();
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
confirm = false;
}
return confirm;
}
}
第三步;springboot资源配置:

1 服务器

server:
port: 9003
servlet:
context-path: /nmrdb

################### DataSource Configuration ##########################
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.0.101:3306/nmrdb
username: root
password: root
druid:
#连接超时时间,初始连接数,最小,最大连接数
max-wait: 60000
initial-size: 20
min-idle: 50
max-active: 200
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
# 配置一个连接在池中最小生存的时间,单位是毫秒
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
validation-query: SELECT 1 FROM DUAL
test-while-idle: true
test-on-borrow: false
test-on-return: false
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20

redis

redis:
###Redis数据库索引(默认为0)
database: 0
host: 127.0.0.1
port: 6379
password:
jedis:
pool:
###连接池最大连接数(使用负值表示没有限制)
max-active: 8
###连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1
###连接池中的最大空闲连接
max-idle: 5
###连接池中的最小空闲连接
min-idle: 0
###连接超时时间(毫秒)
timeout: 10000
jpa:
database: mysql
show-sql: true

hibernate:

ddl-auto: update

2 solr的配置

solr:
url: http://192.168.0.101:8080/solr/nmrCore
maxRetries: 1
connectionTimeout: 1000
第四步:查看结果:
此次更新数据量为:0 时间:2019-07-31 15:32:43
定时进行solr全量索引重建成功! 时间:2019-07-31 15:33:43
此次更新数据量为:0 时间:2019-07-31 15:34:43
此次更新数据量为:0 时间:2019-07-31 15:36:43
定时进行solr全量索引重建成功! 时间:2019-07-31 15:36:43
此次更新数据量为:0 时间:2019-07-31 15:38:43
定时进行solr全量索引重建成功! 时间:2019-07-31 15:39:43
此次更新数据量为:0 时间:2019-07-31 15:40:43

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值