推送数据
This commit is contained in:
@@ -64,6 +64,11 @@ public class SyncHubTableInitializer {
|
|||||||
jdbcTemplate.execute("ALTER TABLE sync_hub_book ALTER COLUMN doc_id TYPE VARCHAR(320)");
|
jdbcTemplate.execute("ALTER TABLE sync_hub_book ALTER COLUMN doc_id TYPE VARCHAR(320)");
|
||||||
backfillMatchKeys();
|
backfillMatchKeys();
|
||||||
jdbcTemplate.execute("CREATE UNIQUE INDEX IF NOT EXISTS uk_sync_hub_book_match ON sync_hub_book (holder, match_key)");
|
jdbcTemplate.execute("CREATE UNIQUE INDEX IF NOT EXISTS uk_sync_hub_book_match ON sync_hub_book (holder, match_key)");
|
||||||
|
jdbcTemplate.execute(
|
||||||
|
"CREATE TABLE IF NOT EXISTS sync_hub_cursor ("
|
||||||
|
+ "node_code VARCHAR(32) NOT NULL PRIMARY KEY, "
|
||||||
|
+ "last_resource_id BIGINT DEFAULT 0, "
|
||||||
|
+ "update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP)");
|
||||||
log.info("图书汇聚表已就绪");
|
log.info("图书汇聚表已就绪");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -60,6 +60,10 @@ public class SyncHubProperties {
|
|||||||
private String mysqlUser = "";
|
private String mysqlUser = "";
|
||||||
private String mysqlPassword = "";
|
private String mysqlPassword = "";
|
||||||
private String mysqlQuery = "";
|
private String mysqlQuery = "";
|
||||||
|
/** 纸质书写入这张表,陆工大测试库是 book_attr */
|
||||||
|
private String paperTable = "book_attr";
|
||||||
|
/** 电子书写入这张表,陆工大测试库是 e_book */
|
||||||
|
private String ebookTable = "e_book";
|
||||||
private String sqlserverUrl = "";
|
private String sqlserverUrl = "";
|
||||||
private String sqlserverUser = "";
|
private String sqlserverUser = "";
|
||||||
private String sqlserverPassword = "";
|
private String sqlserverPassword = "";
|
||||||
|
|||||||
@@ -71,8 +71,12 @@ public class SyncHubController {
|
|||||||
return Result.success(syncHubService.receiveAtCenter(body.nodeCode(), body.sourceDb(), body.books()));
|
return Result.success(syncHubService.receiveAtCenter(body.nodeCode(), body.sourceDb(), body.books()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 总库检查每个节点还缺哪些书,只把缺的补过去。没有缺书的节点不会产生记录。 */
|
/**
|
||||||
@Operation(summary = "总库把节点还没有的书补回去")
|
* 总库往下推。
|
||||||
|
* 陆工大已配置 MySQL 时,从 version_resource 按批读取:纸质写入 book_attr,电子写入 e_book,已有 ISBN 的跳过。
|
||||||
|
* 一次最多新增 batch-size 条,没有推完时返回里会提示再调一次。不会一次写完总库全部图书。
|
||||||
|
*/
|
||||||
|
@Operation(summary = "总库按测试库表结构把 version_resource 分批回写到节点")
|
||||||
@PostMapping("/center/dispatch")
|
@PostMapping("/center/dispatch")
|
||||||
public Result<List<SyncHubRecord>> dispatch() {
|
public Result<List<SyncHubRecord>> dispatch() {
|
||||||
return Result.success(syncHubService.dispatchAll());
|
return Result.success(syncHubService.dispatchAll());
|
||||||
|
|||||||
@@ -18,6 +18,9 @@ public class BookDocument {
|
|||||||
|
|
||||||
private String title;
|
private String title;
|
||||||
|
|
||||||
|
/** 其他题名,下推到 book_attr.TITLE_S */
|
||||||
|
private String otherTitle;
|
||||||
|
|
||||||
private String author;
|
private String author;
|
||||||
|
|
||||||
private String isbn;
|
private String isbn;
|
||||||
@@ -26,6 +29,12 @@ public class BookDocument {
|
|||||||
|
|
||||||
private String publishYear;
|
private String publishYear;
|
||||||
|
|
||||||
|
/** 出版日期,下推到 PUB_DATE */
|
||||||
|
private String publishDate;
|
||||||
|
|
||||||
|
/** 版次,下推到 book_attr.VERSION */
|
||||||
|
private String edition;
|
||||||
|
|
||||||
private String keywords;
|
private String keywords;
|
||||||
|
|
||||||
private String summary;
|
private String summary;
|
||||||
@@ -42,6 +51,10 @@ public class BookDocument {
|
|||||||
|
|
||||||
private String fulltextUrl;
|
private String fulltextUrl;
|
||||||
|
|
||||||
|
/** version_resource.id,只在总库下推时用来记住读到哪一条。 */
|
||||||
|
@JsonIgnore
|
||||||
|
private Long resourceId;
|
||||||
|
|
||||||
@JsonIgnore
|
@JsonIgnore
|
||||||
public String docId() {
|
public String docId() {
|
||||||
String node = nodeCode == null ? "NODE" : nodeCode.trim();
|
String node = nodeCode == null ? "NODE" : nodeCode.trim();
|
||||||
|
|||||||
@@ -0,0 +1,252 @@
|
|||||||
|
package com.banben.modules.synchub.reader;
|
||||||
|
|
||||||
|
import com.banben.common.BusinessException;
|
||||||
|
import com.banben.modules.synchub.config.SyncHubProperties;
|
||||||
|
import com.banben.modules.synchub.model.BookDocument;
|
||||||
|
import com.banben.modules.synchub.service.BookCleaner;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Statement;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 把总库图书按陆工大测试库的表结构写回去。
|
||||||
|
* 纸质写入 book_attr,电子写入 e_book。ISBN 或题名已经存在的不再插入。
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class MysqlNodeWriter {
|
||||||
|
|
||||||
|
private final BookCleaner cleaner;
|
||||||
|
|
||||||
|
public WriteResult write(SyncHubProperties.Node node, List<BookDocument> books) {
|
||||||
|
if (node.getMysqlUrl() == null || node.getMysqlUrl().isBlank()) {
|
||||||
|
throw new BusinessException(node.getName() + " 还没配置 MySQL 地址");
|
||||||
|
}
|
||||||
|
String paper = table(node.getPaperTable(), "book_attr");
|
||||||
|
String ebook = table(node.getEbookTable(), "e_book");
|
||||||
|
int inserted = 0;
|
||||||
|
int skipped = 0;
|
||||||
|
try {
|
||||||
|
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
throw new BusinessException("缺少 MySQL 驱动");
|
||||||
|
}
|
||||||
|
try (Connection connection = DriverManager.getConnection(node.getMysqlUrl(), node.getMysqlUser(), node.getMysqlPassword())) {
|
||||||
|
// 测试库的 book_attr.RECORD_ID 指向 bibliography,但 bibliography 和字典表是空的,
|
||||||
|
// 现有两条书也没有父记录。这里按现有数据的方式只写书目表。
|
||||||
|
try (Statement statement = connection.createStatement()) {
|
||||||
|
statement.execute("SET FOREIGN_KEY_CHECKS = 0");
|
||||||
|
}
|
||||||
|
long paperId = maxId(connection, paper);
|
||||||
|
long ebookId = maxId(connection, ebook);
|
||||||
|
Set<String> seen = new HashSet<>();
|
||||||
|
for (BookDocument book : books) {
|
||||||
|
if (book.getTitle() == null || book.getTitle().isBlank()) {
|
||||||
|
skipped++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!seen.add(cleaner.matchKey(book))) {
|
||||||
|
skipped++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
boolean electronic = book.getMediaType() != null && book.getMediaType().contains("电子");
|
||||||
|
String target = electronic ? ebook : paper;
|
||||||
|
if (electronic ? existsEbook(connection, ebook, book) : existsPaper(connection, paper, book)) {
|
||||||
|
skipped++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (electronic) {
|
||||||
|
ebookId++;
|
||||||
|
insertEbook(connection, ebook, ebookId, book);
|
||||||
|
} else {
|
||||||
|
paperId++;
|
||||||
|
insertPaper(connection, paper, paperId, book);
|
||||||
|
}
|
||||||
|
inserted++;
|
||||||
|
}
|
||||||
|
try (Statement statement = connection.createStatement()) {
|
||||||
|
statement.execute("SET FOREIGN_KEY_CHECKS = 1");
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new BusinessException("写入" + node.getName() + "失败:" + safe(e.getMessage(), node.getMysqlPassword()));
|
||||||
|
}
|
||||||
|
return new WriteResult(inserted, skipped);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void insertPaper(Connection connection, String table, long id, BookDocument book) throws SQLException {
|
||||||
|
String isbn13 = isbn13(book.getIsbn());
|
||||||
|
String isbn10 = isbn13.length() == 10 ? isbn13 : "";
|
||||||
|
String sql = "INSERT INTO `" + table + "` (RECORD_ID, CALL_NO, TITLE, TITLE_S, ISBN13, ISBN10, ISBN, AUTHOR, PUBLISHER, PUB_DATE, PUB_YEAR, VERSION) "
|
||||||
|
+ "VALUES (?,?,?,?,?,?,?,?,?,?,?,?)";
|
||||||
|
try (PreparedStatement ps = connection.prepareStatement(sql)) {
|
||||||
|
ps.setLong(1, id);
|
||||||
|
ps.setString(2, cut(book.getClcCode(), 255));
|
||||||
|
ps.setString(3, cut(book.getTitle(), 1000));
|
||||||
|
ps.setString(4, cut(first(book.getOtherTitle(), book.getTitle()), 1000));
|
||||||
|
ps.setString(5, cut(isbn13, 32));
|
||||||
|
ps.setString(6, cut(isbn10, 32));
|
||||||
|
ps.setString(7, cut(book.getIsbn(), 64));
|
||||||
|
ps.setString(8, cut(book.getAuthor(), 500));
|
||||||
|
ps.setString(9, cut(book.getPublisher(), 500));
|
||||||
|
ps.setString(10, cut(book.getPublishDate(), 32));
|
||||||
|
ps.setString(11, cut(book.getPublishYear(), 16));
|
||||||
|
ps.setString(12, cut(book.getEdition(), 100));
|
||||||
|
ps.executeUpdate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void insertEbook(Connection connection, String table, long id, BookDocument book) throws SQLException {
|
||||||
|
String isbn13 = isbn13(book.getIsbn());
|
||||||
|
String sql = "INSERT INTO `" + table + "` (RECORD_ID, TITLE, AUTHOR, ISBN13, ISBN10, PUBLISHER, PUB_DATE, PUB_YEAR, DOC_ABSTRACT, KEYWORDS, FULLTEXT_LINK) "
|
||||||
|
+ "VALUES (?,?,?,?,?,?,?,?,?,?,?)";
|
||||||
|
try (PreparedStatement ps = connection.prepareStatement(sql)) {
|
||||||
|
ps.setLong(1, id);
|
||||||
|
ps.setString(2, cut(book.getTitle(), 1000));
|
||||||
|
ps.setString(3, cut(book.getAuthor(), 500));
|
||||||
|
ps.setString(4, cut(isbn13, 32));
|
||||||
|
ps.setString(5, isbn13.length() == 10 ? isbn13 : null);
|
||||||
|
ps.setString(6, cut(book.getPublisher(), 500));
|
||||||
|
ps.setString(7, cut(book.getPublishDate(), 32));
|
||||||
|
if (year(book.getPublishYear()) == null) {
|
||||||
|
ps.setObject(8, null);
|
||||||
|
} else {
|
||||||
|
ps.setInt(8, year(book.getPublishYear()));
|
||||||
|
}
|
||||||
|
ps.setString(9, cut(book.getSummary(), 500));
|
||||||
|
ps.setString(10, cut(book.getKeywords(), 255));
|
||||||
|
ps.setString(11, cut(book.getFulltextUrl(), 500));
|
||||||
|
ps.executeUpdate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean existsPaper(Connection connection, String table, BookDocument book) throws SQLException {
|
||||||
|
String isbn = isbn13(book.getIsbn());
|
||||||
|
if (!isbn.isEmpty()) {
|
||||||
|
try (PreparedStatement ps = connection.prepareStatement(
|
||||||
|
"SELECT 1 FROM `" + table + "` WHERE ISBN13 = ? OR ISBN = ? OR ISBN10 = ? LIMIT 1")) {
|
||||||
|
ps.setString(1, isbn);
|
||||||
|
ps.setString(2, book.getIsbn());
|
||||||
|
ps.setString(3, isbn);
|
||||||
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
|
if (rs.next()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return existsByTitle(connection, table, book);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean existsEbook(Connection connection, String table, BookDocument book) throws SQLException {
|
||||||
|
String isbn = isbn13(book.getIsbn());
|
||||||
|
if (!isbn.isEmpty()) {
|
||||||
|
try (PreparedStatement ps = connection.prepareStatement(
|
||||||
|
"SELECT 1 FROM `" + table + "` WHERE ISBN13 = ? OR ISBN10 = ? OR EISBN = ? LIMIT 1")) {
|
||||||
|
ps.setString(1, isbn);
|
||||||
|
ps.setString(2, isbn);
|
||||||
|
ps.setString(3, isbn);
|
||||||
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
|
if (rs.next()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return existsByTitle(connection, table, book);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean existsByTitle(Connection connection, String table, BookDocument book) throws SQLException {
|
||||||
|
try (PreparedStatement ps = connection.prepareStatement(
|
||||||
|
"SELECT 1 FROM `" + table + "` WHERE TITLE = ? AND IFNULL(AUTHOR,'') = ? LIMIT 1")) {
|
||||||
|
ps.setString(1, book.getTitle());
|
||||||
|
ps.setString(2, book.getAuthor() == null ? "" : book.getAuthor());
|
||||||
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
|
return rs.next();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private long maxId(Connection connection, String table) throws SQLException {
|
||||||
|
try (Statement statement = connection.createStatement();
|
||||||
|
ResultSet rs = statement.executeQuery("SELECT IFNULL(MAX(RECORD_ID), 0) FROM `" + table + "`")) {
|
||||||
|
rs.next();
|
||||||
|
return rs.getLong(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String table(String name, String fallback) {
|
||||||
|
String table = name == null || name.isBlank() ? fallback : name.trim();
|
||||||
|
if (!table.matches("[A-Za-z0-9_]+")) {
|
||||||
|
throw new BusinessException("图书表名不正确");
|
||||||
|
}
|
||||||
|
return table;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String isbn13(String isbn) {
|
||||||
|
if (isbn == null) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
StringBuilder out = new StringBuilder();
|
||||||
|
for (int i = 0; i < isbn.length(); i++) {
|
||||||
|
char c = isbn.charAt(i);
|
||||||
|
if (c >= '0' && c <= '9') {
|
||||||
|
out.append(c);
|
||||||
|
} else if (c == 'X' || c == 'x') {
|
||||||
|
out.append('x');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Integer year(String value) {
|
||||||
|
if (value == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
StringBuilder digits = new StringBuilder();
|
||||||
|
for (int i = 0; i < value.length() && digits.length() < 4; i++) {
|
||||||
|
char c = value.charAt(i);
|
||||||
|
if (c >= '0' && c <= '9') {
|
||||||
|
digits.append(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (digits.length() != 4) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return Integer.parseInt(digits.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
private String first(String value, String fallback) {
|
||||||
|
return value == null || value.isBlank() ? fallback : value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String cut(String value, int max) {
|
||||||
|
if (value == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
String text = value.trim();
|
||||||
|
return text.length() <= max ? text : text.substring(0, max);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String safe(String message, String password) {
|
||||||
|
if (message == null) {
|
||||||
|
return "写入失败";
|
||||||
|
}
|
||||||
|
if (password != null && !password.isBlank()) {
|
||||||
|
return message.replace(password, "******");
|
||||||
|
}
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** inserted 是新写入条数,skipped 是测试库里已经有、跳过的条数。 */
|
||||||
|
public record WriteResult(int inserted, int skipped) {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,9 +11,11 @@ import com.banben.modules.synchub.mapper.SyncHubRecordMapper;
|
|||||||
import com.banben.modules.synchub.model.BookDocument;
|
import com.banben.modules.synchub.model.BookDocument;
|
||||||
import com.banben.modules.synchub.reader.JdbcRemoteBookReader;
|
import com.banben.modules.synchub.reader.JdbcRemoteBookReader;
|
||||||
import com.banben.modules.synchub.reader.MockBookReader;
|
import com.banben.modules.synchub.reader.MockBookReader;
|
||||||
|
import com.banben.modules.synchub.reader.MysqlNodeWriter;
|
||||||
import com.banben.modules.synchub.store.BookStore;
|
import com.banben.modules.synchub.store.BookStore;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
@@ -44,6 +46,8 @@ public class SyncHubService {
|
|||||||
private final BookStore bookStore;
|
private final BookStore bookStore;
|
||||||
private final MockBookReader mockBookReader;
|
private final MockBookReader mockBookReader;
|
||||||
private final JdbcRemoteBookReader remoteBookReader;
|
private final JdbcRemoteBookReader remoteBookReader;
|
||||||
|
private final MysqlNodeWriter mysqlNodeWriter;
|
||||||
|
private final JdbcTemplate jdbcTemplate;
|
||||||
private final SyncHubRecordMapper recordMapper;
|
private final SyncHubRecordMapper recordMapper;
|
||||||
private final ObjectMapper objectMapper;
|
private final ObjectMapper objectMapper;
|
||||||
|
|
||||||
@@ -185,6 +189,15 @@ public class SyncHubService {
|
|||||||
record.setSkipCount(data.path("skipCount").asInt(0));
|
record.setSkipCount(data.path("skipCount").asInt(0));
|
||||||
} else {
|
} else {
|
||||||
SyncHubProperties.Node node = requireNode(record.getTargetNode());
|
SyncHubProperties.Node node = requireNode(record.getTargetNode());
|
||||||
|
if (node.getMysqlUrl() != null && !node.getMysqlUrl().isBlank()) {
|
||||||
|
List<BookDocument> page = readVersionResource(loadCursor(node.getCode()), properties.pageSize());
|
||||||
|
if (!page.isEmpty()) {
|
||||||
|
MysqlNodeWriter.WriteResult result = mysqlNodeWriter.write(node, page);
|
||||||
|
saveCursor(node.getCode(), page.get(page.size() - 1).getResourceId());
|
||||||
|
record.setBookCount(result.inserted());
|
||||||
|
record.setSkipCount(result.skipped());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
List<BookDocument> books = bookStore.findMissing(CENTER, node.getCode(), properties.pageSize());
|
List<BookDocument> books = bookStore.findMissing(CENTER, node.getCode(), properties.pageSize());
|
||||||
if (!books.isEmpty()) {
|
if (!books.isEmpty()) {
|
||||||
post(join(node.getCallbackUrl(), "/api/hub/node/receive"), new DeliverBody(node.getCode(), books));
|
post(join(node.getCallbackUrl(), "/api/hub/node/receive"), new DeliverBody(node.getCode(), books));
|
||||||
@@ -192,6 +205,7 @@ public class SyncHubService {
|
|||||||
record.setBookCount(books.size());
|
record.setBookCount(books.size());
|
||||||
record.setSkipCount(0);
|
record.setSkipCount(0);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
succeed(record);
|
succeed(record);
|
||||||
return record;
|
return record;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -208,7 +222,14 @@ public class SyncHubService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配了 MySQL 的节点,从 version_resource 按批写成对方的书目表。
|
||||||
|
* 没配 MySQL 的节点,仍把缺的书送到对方的接收接口。
|
||||||
|
*/
|
||||||
private SyncHubRecord dispatchMissing(SyncHubProperties.Node node) {
|
private SyncHubRecord dispatchMissing(SyncHubProperties.Node node) {
|
||||||
|
if (node.getMysqlUrl() != null && !node.getMysqlUrl().isBlank()) {
|
||||||
|
return dispatchToMysql(node);
|
||||||
|
}
|
||||||
int size = properties.pageSize();
|
int size = properties.pageSize();
|
||||||
List<BookDocument> missing = bookStore.findMissing(CENTER, node.getCode(), size);
|
List<BookDocument> missing = bookStore.findMissing(CENTER, node.getCode(), size);
|
||||||
if (missing.isEmpty()) {
|
if (missing.isEmpty()) {
|
||||||
@@ -245,6 +266,96 @@ public class SyncHubService {
|
|||||||
return record;
|
return record;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private SyncHubRecord dispatchToMysql(SyncHubProperties.Node node) {
|
||||||
|
long cursor = loadCursor(node.getCode());
|
||||||
|
int size = properties.pageSize();
|
||||||
|
int inserted = 0;
|
||||||
|
int skipped = 0;
|
||||||
|
boolean more = false;
|
||||||
|
try {
|
||||||
|
int pages = 0;
|
||||||
|
while (pages < 20 && inserted < size) {
|
||||||
|
List<BookDocument> page = readVersionResource(cursor, size);
|
||||||
|
if (page.isEmpty()) {
|
||||||
|
more = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
MysqlNodeWriter.WriteResult result = mysqlNodeWriter.write(node, page);
|
||||||
|
inserted += result.inserted();
|
||||||
|
skipped += result.skipped();
|
||||||
|
cursor = page.get(page.size() - 1).getResourceId();
|
||||||
|
saveCursor(node.getCode(), cursor);
|
||||||
|
pages++;
|
||||||
|
more = page.size() == size;
|
||||||
|
if (!more) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (inserted == 0 && skipped == 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
SyncHubRecord record = open(DOWN, "NC_CENTER", node.getCode(), "VERSION_RESOURCE");
|
||||||
|
record.setBookCount(inserted);
|
||||||
|
record.setSkipCount(skipped);
|
||||||
|
if (more) {
|
||||||
|
record.setErrorMessage("version_resource 后面还有,请再调用一次回传");
|
||||||
|
}
|
||||||
|
succeed(record);
|
||||||
|
return record;
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
SyncHubRecord record = open(DOWN, "NC_CENTER", node.getCode(), "VERSION_RESOURCE");
|
||||||
|
record.setBookCount(inserted);
|
||||||
|
record.setSkipCount(skipped);
|
||||||
|
fail(record, e);
|
||||||
|
return record;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 从总库 version_resource 取 id 大于 afterId 的一页,不改这张表。 */
|
||||||
|
private List<BookDocument> readVersionResource(long afterId, int limit) {
|
||||||
|
return jdbcTemplate.query(
|
||||||
|
"SELECT id, process_no, title, other_title, author, isbn, publisher, publish_year, publish_date, "
|
||||||
|
+ "clc_code, edition, keywords, summary, media_type, fulltext_url "
|
||||||
|
+ "FROM version_resource WHERE deleted = 0 AND id > ? AND title IS NOT NULL AND title <> '' "
|
||||||
|
+ "ORDER BY id LIMIT ?",
|
||||||
|
(rs, rowNum) -> {
|
||||||
|
BookDocument book = new BookDocument();
|
||||||
|
book.setResourceId(rs.getLong("id"));
|
||||||
|
book.setProcessNo(rs.getString("process_no"));
|
||||||
|
book.setTitle(rs.getString("title"));
|
||||||
|
book.setOtherTitle(rs.getString("other_title"));
|
||||||
|
book.setAuthor(rs.getString("author"));
|
||||||
|
book.setIsbn(rs.getString("isbn"));
|
||||||
|
book.setPublisher(rs.getString("publisher"));
|
||||||
|
book.setPublishYear(rs.getString("publish_year"));
|
||||||
|
book.setPublishDate(rs.getString("publish_date"));
|
||||||
|
book.setClcCode(rs.getString("clc_code"));
|
||||||
|
book.setEdition(rs.getString("edition"));
|
||||||
|
book.setKeywords(rs.getString("keywords"));
|
||||||
|
book.setSummary(rs.getString("summary"));
|
||||||
|
book.setMediaType(rs.getString("media_type"));
|
||||||
|
book.setFulltextUrl(rs.getString("fulltext_url"));
|
||||||
|
book.setSourceDb("VERSION_RESOURCE");
|
||||||
|
book.setNodeCode("NC_CENTER");
|
||||||
|
return book;
|
||||||
|
},
|
||||||
|
afterId, limit);
|
||||||
|
}
|
||||||
|
|
||||||
|
private long loadCursor(String nodeCode) {
|
||||||
|
List<Long> ids = jdbcTemplate.query(
|
||||||
|
"SELECT last_resource_id FROM sync_hub_cursor WHERE node_code = ?",
|
||||||
|
(rs, rowNum) -> rs.getLong(1), nodeCode);
|
||||||
|
return ids.isEmpty() ? 0L : ids.get(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void saveCursor(String nodeCode, long resourceId) {
|
||||||
|
jdbcTemplate.update(
|
||||||
|
"INSERT INTO sync_hub_cursor (node_code, last_resource_id, update_time) VALUES (?,?, CURRENT_TIMESTAMP) "
|
||||||
|
+ "ON CONFLICT (node_code) DO UPDATE SET last_resource_id = EXCLUDED.last_resource_id, update_time = CURRENT_TIMESTAMP",
|
||||||
|
nodeCode, resourceId);
|
||||||
|
}
|
||||||
|
|
||||||
private JdbcRemoteBookReader.Page readPage(SyncHubProperties.Node node, String source, String mediaType, int offset, int limit) {
|
private JdbcRemoteBookReader.Page readPage(SyncHubProperties.Node node, String source, String mediaType, int offset, int limit) {
|
||||||
if (source == null || source.isBlank() || "MOCK".equalsIgnoreCase(source)) {
|
if (source == null || source.isBlank() || "MOCK".equalsIgnoreCase(source)) {
|
||||||
List<BookDocument> all = new ArrayList<>(mockBookReader.read(node, mediaType));
|
List<BookDocument> all = new ArrayList<>(mockBookReader.read(node, mediaType));
|
||||||
|
|||||||
@@ -28,8 +28,9 @@ logging:
|
|||||||
level:
|
level:
|
||||||
com.banben: debug
|
com.banben: debug
|
||||||
|
|
||||||
# 节点与南昌总库的图书同步。本机没有 Elasticsearch,开发期用独立表暂存,不写入 version_resource。
|
# 节点与南昌总库的图书同步。
|
||||||
# 两台电脑联调时,把 center-url、callback-url 改成对方地址,口令两边保持一致。
|
# 陆工大暂用 10.1.1.243 的 ljgc_db。下推时从 version_resource 按批读出,
|
||||||
|
# 纸质写入 book_attr,电子写入 e_book。不把总库两百万条一次写完。
|
||||||
sync-hub:
|
sync-hub:
|
||||||
token: local-mock-hub
|
token: local-mock-hub
|
||||||
store: jdbc
|
store: jdbc
|
||||||
@@ -48,6 +49,12 @@ sync-hub:
|
|||||||
name: 南京陆工大
|
name: 南京陆工大
|
||||||
kinds: MYSQL,SQLSERVER
|
kinds: MYSQL,SQLSERVER
|
||||||
callback-url: http://127.0.0.1:8080
|
callback-url: http://127.0.0.1:8080
|
||||||
|
mysql-url: jdbc:mysql://10.1.1.243:3306/ljgc_db?useSSL=false&allowPublicKeyRetrieval=true&connectTimeout=8000&characterEncoding=utf8
|
||||||
|
mysql-user: root
|
||||||
|
mysql-password: 123456
|
||||||
|
mysql-query: SELECT RECORD_ID AS process_no, TITLE AS title, AUTHOR AS author, ISBN13 AS isbn, PUBLISHER AS publisher, PUB_YEAR AS publish_year, CALL_NO AS clc_code FROM book_attr
|
||||||
|
paper-table: book_attr
|
||||||
|
ebook-table: e_book
|
||||||
- code: BHF
|
- code: BHF
|
||||||
name: 边海防
|
name: 边海防
|
||||||
kinds: MOCK
|
kinds: MOCK
|
||||||
|
|||||||
Reference in New Issue
Block a user