Java Code Examples for com.alibaba.otter.canal.parse.driver.mysql.packets.server.ResultSetPacket#getFieldDescriptors()

The following examples show how to use com.alibaba.otter.canal.parse.driver.mysql.packets.server.ResultSetPacket#getFieldDescriptors() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TableMetaCache.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
/**
 * 处理desc table的结果
 */
public static List<FieldMeta> parseTableMetaByDesc(ResultSetPacket packet) {
    Map<String, Integer> nameMaps = new HashMap<String, Integer>(6, 1f);
    int index = 0;
    for (FieldPacket fieldPacket : packet.getFieldDescriptors()) {
        nameMaps.put(fieldPacket.getOriginalName(), index++);
    }

    int size = packet.getFieldDescriptors().size();
    int count = packet.getFieldValues().size() / packet.getFieldDescriptors().size();
    List<FieldMeta> result = new ArrayList<FieldMeta>();
    for (int i = 0; i < count; i++) {
        FieldMeta meta = new FieldMeta();
        // 做一个优化,使用String.intern(),共享String对象,减少内存使用
        meta.setColumnName(packet.getFieldValues().get(nameMaps.get(COLUMN_NAME) + i * size).intern());
        meta.setColumnType(packet.getFieldValues().get(nameMaps.get(COLUMN_TYPE) + i * size));
        meta.setNullable(StringUtils.equalsIgnoreCase(packet.getFieldValues().get(nameMaps.get(IS_NULLABLE) + i
                                                                                  * size),
            "YES"));
        meta.setKey("PRI".equalsIgnoreCase(packet.getFieldValues().get(nameMaps.get(COLUMN_KEY) + i * size)));
        meta.setUnique("UNI".equalsIgnoreCase(packet.getFieldValues().get(nameMaps.get(COLUMN_KEY) + i * size)));
        // 特殊处理引号
        meta.setDefaultValue(DruidDdlParser.unescapeQuotaName(packet.getFieldValues()
            .get(nameMaps.get(COLUMN_DEFAULT) + i * size)));
        meta.setExtra(packet.getFieldValues().get(nameMaps.get(EXTRA) + i * size));

        result.add(meta);
    }

    return result;
}
 
Example 2
Source File: MysqlEventParser.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
/**
 * 查询当前的slave视图的binlog位置
 */
@SuppressWarnings("unused")
private SlaveEntryPosition findSlavePosition(MysqlConnection mysqlConnection) {
    try {
        ResultSetPacket packet = mysqlConnection.query("show slave status");
        List<FieldPacket> names = packet.getFieldDescriptors();
        List<String> fields = packet.getFieldValues();
        if (CollectionUtils.isEmpty(fields)) {
            return null;
        }

        int i = 0;
        Map<String, String> maps = new HashMap<String, String>(names.size(), 1f);
        for (FieldPacket name : names) {
            maps.put(name.getName(), fields.get(i));
            i++;
        }

        String errno = maps.get("Last_Errno");
        String slaveIORunning = maps.get("Slave_IO_Running"); // Slave_SQL_Running
        String slaveSQLRunning = maps.get("Slave_SQL_Running"); // Slave_SQL_Running
        if ((!"0".equals(errno)) || (!"Yes".equalsIgnoreCase(slaveIORunning))
            || (!"Yes".equalsIgnoreCase(slaveSQLRunning))) {
            logger.warn("Ignoring failed slave: " + mysqlConnection.getConnector().getAddress() + ", Last_Errno = "
                        + errno + ", Slave_IO_Running = " + slaveIORunning + ", Slave_SQL_Running = "
                        + slaveSQLRunning);
            return null;
        }

        String masterHost = maps.get("Master_Host");
        String masterPort = maps.get("Master_Port");
        String binlog = maps.get("Master_Log_File");
        String position = maps.get("Exec_Master_Log_Pos");
        return new SlaveEntryPosition(binlog, Long.valueOf(position), masterHost, masterPort);
    } catch (IOException e) {
        logger.error("find slave position error", e);
    }

    return null;
}
 
Example 3
Source File: TableMetaCache.java    From canal with Apache License 2.0 5 votes vote down vote up
/**
 * 处理desc table的结果
 */
public static List<FieldMeta> parseTableMetaByDesc(ResultSetPacket packet) {
    Map<String, Integer> nameMaps = new HashMap<String, Integer>(6, 1f);
    int index = 0;
    for (FieldPacket fieldPacket : packet.getFieldDescriptors()) {
        nameMaps.put(fieldPacket.getOriginalName(), index++);
    }

    int size = packet.getFieldDescriptors().size();
    int count = packet.getFieldValues().size() / packet.getFieldDescriptors().size();
    List<FieldMeta> result = new ArrayList<FieldMeta>();
    for (int i = 0; i < count; i++) {
        FieldMeta meta = new FieldMeta();
        // 做一个优化,使用String.intern(),共享String对象,减少内存使用
        meta.setColumnName(packet.getFieldValues().get(nameMaps.get(COLUMN_NAME) + i * size).intern());
        meta.setColumnType(packet.getFieldValues().get(nameMaps.get(COLUMN_TYPE) + i * size));
        meta.setNullable(StringUtils.equalsIgnoreCase(packet.getFieldValues().get(nameMaps.get(IS_NULLABLE) + i
                                                                                  * size),
            "YES"));
        meta.setKey("PRI".equalsIgnoreCase(packet.getFieldValues().get(nameMaps.get(COLUMN_KEY) + i * size)));
        meta.setUnique("UNI".equalsIgnoreCase(packet.getFieldValues().get(nameMaps.get(COLUMN_KEY) + i * size)));
        // 特殊处理引号
        meta.setDefaultValue(DruidDdlParser.unescapeQuotaName(packet.getFieldValues()
            .get(nameMaps.get(COLUMN_DEFAULT) + i * size)));
        meta.setExtra(packet.getFieldValues().get(nameMaps.get(EXTRA) + i * size));

        result.add(meta);
    }

    return result;
}
 
Example 4
Source File: MysqlEventParser.java    From canal with Apache License 2.0 5 votes vote down vote up
/**
 * 查询当前的slave视图的binlog位置
 */
@SuppressWarnings("unused")
private SlaveEntryPosition findSlavePosition(MysqlConnection mysqlConnection) {
    try {
        ResultSetPacket packet = mysqlConnection.query("show slave status");
        List<FieldPacket> names = packet.getFieldDescriptors();
        List<String> fields = packet.getFieldValues();
        if (CollectionUtils.isEmpty(fields)) {
            return null;
        }

        int i = 0;
        Map<String, String> maps = new HashMap<String, String>(names.size(), 1f);
        for (FieldPacket name : names) {
            maps.put(name.getName(), fields.get(i));
            i++;
        }

        String errno = maps.get("Last_Errno");
        String slaveIORunning = maps.get("Slave_IO_Running"); // Slave_SQL_Running
        String slaveSQLRunning = maps.get("Slave_SQL_Running"); // Slave_SQL_Running
        if ((!"0".equals(errno)) || (!"Yes".equalsIgnoreCase(slaveIORunning))
            || (!"Yes".equalsIgnoreCase(slaveSQLRunning))) {
            logger.warn("Ignoring failed slave: " + mysqlConnection.getConnector().getAddress() + ", Last_Errno = "
                        + errno + ", Slave_IO_Running = " + slaveIORunning + ", Slave_SQL_Running = "
                        + slaveSQLRunning);
            return null;
        }

        String masterHost = maps.get("Master_Host");
        String masterPort = maps.get("Master_Port");
        String binlog = maps.get("Master_Log_File");
        String position = maps.get("Exec_Master_Log_Pos");
        return new SlaveEntryPosition(binlog, Long.valueOf(position), masterHost, masterPort);
    } catch (IOException e) {
        logger.error("find slave position error", e);
    }

    return null;
}