FullSyncPostToEs.java 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package com.yupi.yuso.job.once;
  2. import com.yupi.yuso.esdao.PostEsDao;
  3. import com.yupi.yuso.model.dto.post.PostEsDTO;
  4. import com.yupi.yuso.model.entity.Post;
  5. import com.yupi.yuso.service.PostService;
  6. import java.util.List;
  7. import java.util.stream.Collectors;
  8. import javax.annotation.Resource;
  9. import lombok.extern.slf4j.Slf4j;
  10. import org.apache.commons.collections4.CollectionUtils;
  11. import org.springframework.boot.CommandLineRunner;
  12. /**
  13. * 全量同步帖子到 es
  14. *
  15. * @author <a href="https://github.com/liyupi">程序员鱼皮</a>
  16. * @from <a href="https://yupi.icu">编程导航知识星球</a>
  17. */
  18. // todo 取消注释开启任务
  19. //@Component
  20. @Slf4j
  21. public class FullSyncPostToEs implements CommandLineRunner {
  22. @Resource
  23. private PostService postService;
  24. @Resource
  25. private PostEsDao postEsDao;
  26. @Override
  27. public void run(String... args) {
  28. List<Post> postList = postService.list();
  29. if (CollectionUtils.isEmpty(postList)) {
  30. return;
  31. }
  32. List<PostEsDTO> postEsDTOList = postList.stream().map(PostEsDTO::objToDto).collect(Collectors.toList());
  33. final int pageSize = 500;
  34. int total = postEsDTOList.size();
  35. log.info("FullSyncPostToEs start, total {}", total);
  36. for (int i = 0; i < total; i += pageSize) {
  37. int end = Math.min(i + pageSize, total);
  38. log.info("sync from {} to {}", i, end);
  39. postEsDao.saveAll(postEsDTOList.subList(i, end));
  40. }
  41. log.info("FullSyncPostToEs end, total {}", total);
  42. }
  43. }