use of com.alibaba.druid.sql.repository.Schema in project canal by alibaba.
the class DatabaseTableMeta method applySnapshotToDB.
/**
* 发布数据到console上
*/
private boolean applySnapshotToDB(EntryPosition position, boolean init) {
// 获取一份快照
Map<String, String> schemaDdls = null;
lock.readLock().lock();
try {
if (!init && !hasNewDdl) {
// 如果是持续构建,则识别一下是否有DDL变更过,如果没有就忽略了
return false;
}
this.hasNewDdl = false;
schemaDdls = memoryTableMeta.snapshot();
} finally {
lock.readLock().unlock();
}
MemoryTableMeta tmpMemoryTableMeta = new MemoryTableMeta();
for (Map.Entry<String, String> entry : schemaDdls.entrySet()) {
tmpMemoryTableMeta.apply(position, entry.getKey(), entry.getValue(), null);
}
// 基于临时内存对象进行对比
boolean compareAll = true;
for (Schema schema : tmpMemoryTableMeta.getRepository().getSchemas()) {
for (String table : schema.showTables()) {
String fullName = schema.getName() + "." + table;
if (blackFilter == null || !blackFilter.filter(fullName)) {
if (filter == null || filter.filter(fullName)) {
// 在生成snapshot时重新过滤一遍
if (!compareTableMetaDbAndMemory(connection, tmpMemoryTableMeta, schema.getName(), table)) {
compareAll = false;
}
}
}
}
}
if (compareAll) {
Map<String, String> content = new HashMap<>();
content.put("destination", destination);
content.put("binlogFile", position.getJournalName());
content.put("binlogOffest", String.valueOf(position.getPosition()));
content.put("binlogMasterId", String.valueOf(position.getServerId()));
content.put("binlogTimestamp", String.valueOf(position.getTimestamp()));
content.put("data", JSON.toJSONString(schemaDdls));
if (content.isEmpty()) {
throw new RuntimeException("apply failed caused by content is empty in applySnapshotToDB");
}
MetaSnapshotDO snapshotDO = new MetaSnapshotDO();
try {
BeanUtils.populate(snapshotDO, content);
metaSnapshotDAO.insert(snapshotDO);
} catch (Throwable e) {
if (isUkDuplicateException(e)) {
// 忽略掉重复的位点
logger.info("dup apply snapshot use position : " + position + " , just ignore");
} else {
throw new CanalParseException("apply failed caused by : " + e.getMessage(), e);
}
}
return true;
} else {
logger.error("compare failed , check log");
}
return false;
}
use of com.alibaba.druid.sql.repository.Schema in project canal by alibaba.
the class MemoryTableMeta method snapshot.
public Map<String, String> snapshot() {
Map<String, String> schemaDdls = new HashMap<>();
for (Schema schema : repository.getSchemas()) {
StringBuffer data = new StringBuffer(4 * 1024);
for (String table : schema.showTables()) {
SchemaObject schemaObject = schema.findTable(table);
schemaObject.getStatement().output(data);
data.append("; \n");
}
schemaDdls.put(schema.getName(), data.toString());
}
return schemaDdls;
}
use of com.alibaba.druid.sql.repository.Schema in project canal by alibaba.
the class MemoryTableMeta method find.
@Override
public TableMeta find(String schema, String table) {
List<String> keys = Arrays.asList(schema, table);
TableMeta tableMeta = tableMetas.get(keys);
if (tableMeta == null) {
synchronized (this) {
tableMeta = tableMetas.get(keys);
if (tableMeta == null) {
Schema schemaRep = repository.findSchema(schema);
if (schemaRep == null) {
return null;
}
SchemaObject data = schemaRep.findTable(table);
if (data == null) {
return null;
}
SQLStatement statement = data.getStatement();
if (statement == null) {
return null;
}
if (statement instanceof SQLCreateTableStatement) {
tableMeta = parse((SQLCreateTableStatement) statement);
}
if (tableMeta != null) {
if (table != null) {
tableMeta.setTable(table);
}
if (schema != null) {
tableMeta.setSchema(schema);
}
tableMetas.put(keys, tableMeta);
}
}
}
}
return tableMeta;
}
use of com.alibaba.druid.sql.repository.Schema in project canal by alibaba.
the class MemoryTableMeta_DDL_Test method test_any.
@Test
public void test_any() throws Throwable {
MemoryTableMeta memoryTableMeta = new MemoryTableMeta();
URL url = Thread.currentThread().getContextClassLoader().getResource("dummy.txt");
File dummyFile = new File(url.getFile());
File create = new File(dummyFile.getParent() + "/ddl", "ddl_any.sql");
String sql = StringUtils.join(IOUtils.readLines(new FileInputStream(create)), "\n");
memoryTableMeta.apply(null, "test", sql, null);
List<String> tableNames = new ArrayList<>();
for (Schema schema : memoryTableMeta.getRepository().getSchemas()) {
tableNames.addAll(schema.showTables());
}
for (String table : tableNames) {
TableMeta sourceMeta = memoryTableMeta.find("test", table);
System.out.println(sourceMeta.toString());
}
}
use of com.alibaba.druid.sql.repository.Schema in project canal by alibaba.
the class MemoryTableMeta_DDL_Test method test_create_if_not_exist.
@Test
public void test_create_if_not_exist() throws Throwable {
MemoryTableMeta memoryTableMeta = new MemoryTableMeta();
URL url = Thread.currentThread().getContextClassLoader().getResource("dummy.txt");
File dummyFile = new File(url.getFile());
File create = new File(dummyFile.getParent() + "/ddl", "ddl_create_if_not_exist.sql");
String sql = StringUtils.join(IOUtils.readLines(new FileInputStream(create)), "\n");
memoryTableMeta.apply(null, "test", sql, null);
List<String> tableNames = new ArrayList<>();
for (Schema schema : memoryTableMeta.getRepository().getSchemas()) {
tableNames.addAll(schema.showTables());
}
for (String table : tableNames) {
TableMeta sourceMeta = memoryTableMeta.find("test", table);
System.out.println(sourceMeta.toString());
}
}
Aggregations