Search in sources :

Example 11 with TableMeta

use of com.alibaba.otter.canal.parse.inbound.TableMeta 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;
}
Also used : SchemaObject(com.alibaba.druid.sql.repository.SchemaObject) SQLCreateTableStatement(com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement) Schema(com.alibaba.druid.sql.repository.Schema) TableMeta(com.alibaba.otter.canal.parse.inbound.TableMeta) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 12 with TableMeta

use of com.alibaba.otter.canal.parse.inbound.TableMeta 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());
    }
}
Also used : Schema(com.alibaba.druid.sql.repository.Schema) ArrayList(java.util.ArrayList) TableMeta(com.alibaba.otter.canal.parse.inbound.TableMeta) File(java.io.File) URL(java.net.URL) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 13 with TableMeta

use of com.alibaba.otter.canal.parse.inbound.TableMeta in project canal by alibaba.

the class MemoryTableMeta_DDL_Test method test1.

@Test
public void test1() 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_test1.sql");
    String sql = StringUtils.join(IOUtils.readLines(new FileInputStream(create)), "\n");
    memoryTableMeta.apply(null, "test", sql, null);
    TableMeta meta = memoryTableMeta.find("yushitai_test", "card_record");
    System.out.println(meta);
    Assert.assertNotNull(meta.getFieldMetaByName("customization_id"));
    meta = memoryTableMeta.find("yushitai_test", "_card_record_gho");
    Assert.assertNull(meta);
}
Also used : TableMeta(com.alibaba.otter.canal.parse.inbound.TableMeta) File(java.io.File) URL(java.net.URL) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 14 with TableMeta

use of com.alibaba.otter.canal.parse.inbound.TableMeta 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());
    }
}
Also used : Schema(com.alibaba.druid.sql.repository.Schema) ArrayList(java.util.ArrayList) TableMeta(com.alibaba.otter.canal.parse.inbound.TableMeta) File(java.io.File) URL(java.net.URL) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 15 with TableMeta

use of com.alibaba.otter.canal.parse.inbound.TableMeta in project canal by alibaba.

the class MemoryTableMeta_Random_DDL_Test method compareTableMeta.

private void compareTableMeta(int num, MemoryTableMeta source, MemoryTableMeta target) {
    List<String> tableNames = new ArrayList<>();
    for (Schema schema : source.getRepository().getSchemas()) {
        tableNames.addAll(schema.showTables());
    }
    for (String table : tableNames) {
        TableMeta sourceMeta = source.find("test", table);
        TableMeta targetMeta = target.find("test", table);
        boolean result = DatabaseTableMeta.compareTableMeta(sourceMeta, targetMeta);
        if (!result) {
            Assert.fail(sourceMeta.toString() + " vs " + targetMeta.toString());
        }
    }
}
Also used : Schema(com.alibaba.druid.sql.repository.Schema) ArrayList(java.util.ArrayList) TableMeta(com.alibaba.otter.canal.parse.inbound.TableMeta)

Aggregations

TableMeta (com.alibaba.otter.canal.parse.inbound.TableMeta)15 File (java.io.File)6 FileInputStream (java.io.FileInputStream)6 URL (java.net.URL)6 Test (org.junit.Test)6 Schema (com.alibaba.druid.sql.repository.Schema)4 CanalParseException (com.alibaba.otter.canal.parse.exception.CanalParseException)4 ArrayList (java.util.ArrayList)4 ResultSetPacket (com.alibaba.otter.canal.parse.driver.mysql.packets.server.ResultSetPacket)3 DatabaseTableMeta (com.alibaba.otter.canal.parse.inbound.mysql.tsdb.DatabaseTableMeta)3 MemoryTableMeta (com.alibaba.otter.canal.parse.inbound.mysql.tsdb.MemoryTableMeta)3 ByteString (com.google.protobuf.ByteString)2 TableMapLogEvent (com.taobao.tddl.dbsync.binlog.event.TableMapLogEvent)2 IOException (java.io.IOException)2 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1 SQLColumnConstraint (com.alibaba.druid.sql.ast.statement.SQLColumnConstraint)1 SQLCreateTableStatement (com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement)1 SQLNotNullConstraint (com.alibaba.druid.sql.ast.statement.SQLNotNullConstraint)1 SQLNullConstraint (com.alibaba.druid.sql.ast.statement.SQLNullConstraint)1 SQLTableElement (com.alibaba.druid.sql.ast.statement.SQLTableElement)1