Feat: [后台]:完成mysql备份到es接口开发

This commit is contained in:
fanxb 2019-11-12 00:43:32 +08:00
parent b51616fbe3
commit 2fe026b69a
3 changed files with 116 additions and 0 deletions

View File

@ -0,0 +1,36 @@
package com.fanxb.bookmark.business.bookmark.controller;
import com.fanxb.bookmark.business.bookmark.service.BookmarkBackupService;
import com.fanxb.bookmark.common.entity.Result;
import com.fanxb.bookmark.common.util.ThreadPoolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created with IntelliJ IDEA
* Created By Fxb
* Date: 2019/11/12
* Time: 0:35
*
* @author fanxb
*/
@RestController
@RequestMapping("/bookmarkBackup")
public class BookmarkBackupController {
private BookmarkBackupService backupService;
@Autowired
public BookmarkBackupController(BookmarkBackupService backupService) {
this.backupService = backupService;
}
@PostMapping("/mysqlToEs")
public Result backupToEs() {
//异步执行同步任务
ThreadPoolUtil.execute(() -> backupService.backupToEs());
return Result.success(null);
}
}

View File

@ -0,0 +1,25 @@
package com.fanxb.bookmark.business.bookmark.dao;
import com.fanxb.bookmark.common.entity.Bookmark;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* Created with IntelliJ IDEA
* Created By Fxb
* Date: 2019/11/12
* Time: 0:24
*/
public interface BookmarkBackupDao {
/**
* 分页获取所有的书签
* @param size 大小
* @param startIndex 开始下标
* @return
*/
@Select("select * from bookmark order by bookmarkId limit ${startIndex},${size}")
List<Bookmark> getBookmarkListPage(@Param("size") int size,@Param("startIndex") int startIndex);
}

View File

@ -0,0 +1,55 @@
package com.fanxb.bookmark.business.bookmark.service;
import com.fanxb.bookmark.business.bookmark.dao.BookmarkBackupDao;
import com.fanxb.bookmark.business.bookmark.dao.BookmarkDao;
import com.fanxb.bookmark.common.constant.EsConstant;
import com.fanxb.bookmark.common.entity.Bookmark;
import com.fanxb.bookmark.common.entity.EsEntity;
import com.fanxb.bookmark.common.util.EsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
* Created with IntelliJ IDEA
* Created By Fxb
* Date: 2019/11/12
* Time: 0:22
*
* @author fanxb
*/
@Service
public class BookmarkBackupService {
@Autowired
private BookmarkDao bookmarkDao;
@Autowired
private BookmarkBackupDao bookmarkBackupDao;
@Autowired
private EsUtil esUtil;
/**
* 一次同步BACKUP_SIZE条到es中
*/
private static final int BACKUP_SIZE = 500;
/**
* 功能描述: 将mysql数据同步到es中
*
* @author fanxb
* @date 2019/11/12 0:22
*/
public void backupToEs() {
int start = 0;
List<Bookmark> list;
while ((list = bookmarkBackupDao.getBookmarkListPage(BACKUP_SIZE, start)).size() != 0) {
List<EsEntity> batchList = new ArrayList<>(list.size());
list.forEach(item -> batchList.add(new EsEntity<>(item.getBookmarkId().toString(), item)));
esUtil.insertBatch(EsConstant.BOOKMARK_INDEX, batchList);
start += BACKUP_SIZE;
}
}
}