Search in sources :

Example 1 with TableMeta

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

the class MemoryTableMeta_DDL_Test method test2.

@Test
public void test2() 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_test2.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.assertEquals(meta.getFieldMetaByName("id").isKey(), true);
    Assert.assertEquals(meta.getFieldMetaByName("name").isUnique(), true);
}
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 2 with TableMeta

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

the class MemoryTableMeta_DDL_Test method test3.

@Test
public void test3() 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_test3.sql");
    String sql = StringUtils.join(IOUtils.readLines(new FileInputStream(create)), "\n");
    memoryTableMeta.apply(null, "test", sql, null);
    TableMeta meta = memoryTableMeta.find("test", "quniya4");
    System.out.println(meta);
    Assert.assertTrue(meta.getFields().get(0).getColumnName().equalsIgnoreCase("id"));
}
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 3 with TableMeta

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

the class MemoryTableMetaTest method testSimple.

@Test
public void testSimple() 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", "create.sql");
    String sql = StringUtils.join(IOUtils.readLines(new FileInputStream(create)), "\n");
    memoryTableMeta.apply(null, "test", sql, null);
    TableMeta meta = memoryTableMeta.find("test", "test");
    System.out.println(meta);
    Assert.assertTrue(meta.getFieldMetaByName("ID").isKey());
}
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 4 with TableMeta

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

the class TableMetaCache method parseTableMeta.

public static List<FieldMeta> parseTableMeta(String schema, String table, ResultSetPacket packet) {
    if (packet.getFieldValues().size() > 1) {
        String createDDL = packet.getFieldValues().get(1);
        MemoryTableMeta memoryTableMeta = new MemoryTableMeta();
        memoryTableMeta.apply(DatabaseTableMeta.INIT_POSITION, schema, createDDL, null);
        TableMeta tableMeta = memoryTableMeta.find(schema, table);
        return tableMeta.getFields();
    } else {
        return new ArrayList<>();
    }
}
Also used : MemoryTableMeta(com.alibaba.otter.canal.parse.inbound.mysql.tsdb.MemoryTableMeta) ArrayList(java.util.ArrayList) DatabaseTableMeta(com.alibaba.otter.canal.parse.inbound.mysql.tsdb.DatabaseTableMeta) TableMeta(com.alibaba.otter.canal.parse.inbound.TableMeta) MemoryTableMeta(com.alibaba.otter.canal.parse.inbound.mysql.tsdb.MemoryTableMeta)

Example 5 with TableMeta

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

the class TableMetaCache method getTableMetaByDB.

private synchronized TableMeta getTableMetaByDB(String fullname) throws IOException {
    try {
        ResultSetPacket packet = connection.query("show create table " + fullname);
        String[] names = StringUtils.split(fullname, "`.`");
        String schema = names[0];
        String table = names[1].substring(0, names[1].length());
        return new TableMeta(schema, table, parseTableMeta(schema, table, packet));
    } catch (Throwable e) {
        // fallback to desc table
        ResultSetPacket packet = connection.query("desc " + fullname);
        String[] names = StringUtils.split(fullname, "`.`");
        String schema = names[0];
        String table = names[1].substring(0, names[1].length());
        return new TableMeta(schema, table, parseTableMetaByDesc(packet));
    }
}
Also used : ResultSetPacket(com.alibaba.otter.canal.parse.driver.mysql.packets.server.ResultSetPacket) DatabaseTableMeta(com.alibaba.otter.canal.parse.inbound.mysql.tsdb.DatabaseTableMeta) TableMeta(com.alibaba.otter.canal.parse.inbound.TableMeta) MemoryTableMeta(com.alibaba.otter.canal.parse.inbound.mysql.tsdb.MemoryTableMeta)

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