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

The following examples show how to use com.alibaba.otter.canal.parse.driver.mysql.packets.server.ResultSetPacket#getFieldValues() . 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: MysqlConnection.java    From canal-1.1.3 with Apache License 2.0 6 votes vote down vote up
/**
 * 获取一下binlog format格式
 */
private void loadBinlogFormat() {
    ResultSetPacket rs = null;
    try {
        rs = query("show variables like 'binlog_format'");
    } catch (IOException e) {
        throw new CanalParseException(e);
    }

    List<String> columnValues = rs.getFieldValues();
    if (columnValues == null || columnValues.size() != 2) {
        logger.warn("unexpected binlog format query result, this may cause unexpected result, so throw exception to request network to io shutdown.");
        throw new IllegalStateException("unexpected binlog format query result:" + rs.getFieldValues());
    }

    binlogFormat = BinlogFormat.valuesOf(columnValues.get(1));
    if (binlogFormat == null) {
        throw new IllegalStateException("unexpected binlog format query result:" + rs.getFieldValues());
    }
}
 
Example 2
Source File: MysqlConnection.java    From canal-1.1.3 with Apache License 2.0 6 votes vote down vote up
/**
 * 获取一下binlog image格式
 */
private void loadBinlogImage() {
    ResultSetPacket rs = null;
    try {
        rs = query("show variables like 'binlog_row_image'");
    } catch (IOException e) {
        throw new CanalParseException(e);
    }

    List<String> columnValues = rs.getFieldValues();
    if (columnValues == null || columnValues.size() != 2) {
        // 可能历时版本没有image特性
        binlogImage = BinlogImage.FULL;
    } else {
        binlogImage = BinlogImage.valuesOf(columnValues.get(1));
    }

    if (binlogFormat == null) {
        throw new IllegalStateException("unexpected binlog image query result:" + rs.getFieldValues());
    }
}
 
Example 3
Source File: MysqlConnection.java    From canal-1.1.3 with Apache License 2.0 6 votes vote down vote up
/**
 * 获取主库checksum信息
 * 
 * <pre>
 * mariadb区别于mysql会在binlog的第一个事件Rotate_Event里也会采用checksum逻辑,而mysql是在第二个binlog事件之后才感知是否需要处理checksum
 * 导致maraidb只要是开启checksum就会出现binlog文件名解析乱码
 * fixed issue : https://github.com/alibaba/canal/issues/1081
 * </pre>
 */
private void loadBinlogChecksum() {
    ResultSetPacket rs = null;
    try {
        rs = query("select @@global.binlog_checksum");
        List<String> columnValues = rs.getFieldValues();
        if (columnValues != null && columnValues.size() >= 1 && columnValues.get(0).toUpperCase().equals("CRC32")) {
            binlogChecksum = LogEvent.BINLOG_CHECKSUM_ALG_CRC32;
        } else {
            binlogChecksum = LogEvent.BINLOG_CHECKSUM_ALG_OFF;
        }
    } catch (Throwable e) {
        logger.error("", e);
        binlogChecksum = LogEvent.BINLOG_CHECKSUM_ALG_OFF;
    }
}
 
Example 4
Source File: MysqlEventParser.java    From canal-1.1.3 with Apache License 2.0 6 votes vote down vote up
/**
 * 查询当前的binlog位置
 */
private EntryPosition findEndPosition(MysqlConnection mysqlConnection) {
    try {
        ResultSetPacket packet = mysqlConnection.query("show master status");
        List<String> fields = packet.getFieldValues();
        if (CollectionUtils.isEmpty(fields)) {
            throw new CanalParseException("command : 'show master status' has an error! pls check. you need (at least one of) the SUPER,REPLICATION CLIENT privilege(s) for this operation");
        }
        EntryPosition endPosition = new EntryPosition(fields.get(0), Long.valueOf(fields.get(1)));
        if (isGTIDMode() && fields.size() > 4) {
            endPosition.setGtid(fields.get(4));
        }
        return endPosition;
    } catch (IOException e) {
        throw new CanalParseException("command : 'show master status' has an error!", e);
    }
}
 
Example 5
Source File: MysqlEventParser.java    From canal with Apache License 2.0 6 votes vote down vote up
/**
 * 查询当前的binlog位置
 */
private EntryPosition findEndPosition(MysqlConnection mysqlConnection) {
    try {
        ResultSetPacket packet = mysqlConnection.query("show master status");
        List<String> fields = packet.getFieldValues();
        if (CollectionUtils.isEmpty(fields)) {
            throw new CanalParseException("command : 'show master status' has an error! pls check. you need (at least one of) the SUPER,REPLICATION CLIENT privilege(s) for this operation");
        }
        EntryPosition endPosition = new EntryPosition(fields.get(0), Long.valueOf(fields.get(1)));
        if (isGTIDMode() && fields.size() > 4) {
            endPosition.setGtid(fields.get(4));
        }
        return endPosition;
    } catch (IOException e) {
        throw new CanalParseException("command : 'show master status' has an error!", e);
    }
}
 
Example 6
Source File: MysqlConnection.java    From canal with Apache License 2.0 6 votes vote down vote up
/**
 * 获取一下binlog format格式
 */
private void loadBinlogFormat() {
    ResultSetPacket rs = null;
    try {
        rs = query("show variables like 'binlog_format'");
    } catch (IOException e) {
        throw new CanalParseException(e);
    }

    List<String> columnValues = rs.getFieldValues();
    if (columnValues == null || columnValues.size() != 2) {
        logger.warn("unexpected binlog format query result, this may cause unexpected result, so throw exception to request network to io shutdown.");
        throw new IllegalStateException("unexpected binlog format query result:" + rs.getFieldValues());
    }

    binlogFormat = BinlogFormat.valuesOf(columnValues.get(1));
    if (binlogFormat == null) {
        throw new IllegalStateException("unexpected binlog format query result:" + rs.getFieldValues());
    }
}
 
Example 7
Source File: MysqlConnection.java    From canal with Apache License 2.0 6 votes vote down vote up
/**
 * 获取一下binlog image格式
 */
private void loadBinlogImage() {
    ResultSetPacket rs = null;
    try {
        rs = query("show variables like 'binlog_row_image'");
    } catch (IOException e) {
        throw new CanalParseException(e);
    }

    List<String> columnValues = rs.getFieldValues();
    if (columnValues == null || columnValues.size() != 2) {
        // 可能历时版本没有image特性
        binlogImage = BinlogImage.FULL;
    } else {
        binlogImage = BinlogImage.valuesOf(columnValues.get(1));
    }

    if (binlogFormat == null) {
        throw new IllegalStateException("unexpected binlog image query result:" + rs.getFieldValues());
    }
}
 
Example 8
Source File: MysqlConnection.java    From canal with Apache License 2.0 6 votes vote down vote up
/**
 * 获取主库checksum信息
 * 
 * <pre>
 * mariadb区别于mysql会在binlog的第一个事件Rotate_Event里也会采用checksum逻辑,而mysql是在第二个binlog事件之后才感知是否需要处理checksum
 * 导致maraidb只要是开启checksum就会出现binlog文件名解析乱码
 * fixed issue : https://github.com/alibaba/canal/issues/1081
 * </pre>
 */
private void loadBinlogChecksum() {
    ResultSetPacket rs = null;
    try {
        rs = query("select @@global.binlog_checksum");
        List<String> columnValues = rs.getFieldValues();
        if (columnValues != null && columnValues.size() >= 1 && columnValues.get(0) != null
            && columnValues.get(0).toUpperCase().equals("CRC32")) {
            binlogChecksum = LogEvent.BINLOG_CHECKSUM_ALG_CRC32;
        } else {
            binlogChecksum = LogEvent.BINLOG_CHECKSUM_ALG_OFF;
        }
    } catch (Throwable e) {
        // logger.error("", e);
        binlogChecksum = LogEvent.BINLOG_CHECKSUM_ALG_OFF;
    }
}
 
Example 9
Source File: DirectLogFetcherTest.java    From canal with Apache License 2.0 5 votes vote down vote up
private void loadBinlogChecksum(MysqlConnector connector) {
    ResultSetPacket rs = null;
    try {
        rs = query("select @@global.binlog_checksum", connector);
    } catch (IOException e) {
        throw new CanalParseException(e);
    }

    List<String> columnValues = rs.getFieldValues();
    if (columnValues != null && columnValues.size() >= 1 && columnValues.get(0).toUpperCase().equals("CRC32")) {
        binlogChecksum = LogEvent.BINLOG_CHECKSUM_ALG_CRC32;
    } else {
        binlogChecksum = LogEvent.BINLOG_CHECKSUM_ALG_OFF;
    }
}
 
Example 10
Source File: MysqlConnection.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
@Override
public long queryServerId() throws IOException {
    ResultSetPacket resultSetPacket = query("show variables like 'server_id'");
    List<String> fieldValues = resultSetPacket.getFieldValues();
    if (fieldValues == null || fieldValues.size() != 2) {
        return 0;
    }
    return NumberUtils.toLong(fieldValues.get(1));
}
 
Example 11
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;
}
 
Example 12
Source File: MysqlEventParser.java    From canal with Apache License 2.0 5 votes vote down vote up
/**
 * 查询当前的binlog位置
 */
private EntryPosition findStartPosition(MysqlConnection mysqlConnection) {
    try {
        ResultSetPacket packet = mysqlConnection.query("show binlog events limit 1");
        List<String> fields = packet.getFieldValues();
        if (CollectionUtils.isEmpty(fields)) {
            throw new CanalParseException("command : 'show binlog events limit 1' has an error! pls check. you need (at least one of) the SUPER,REPLICATION CLIENT privilege(s) for this operation");
        }
        EntryPosition endPosition = new EntryPosition(fields.get(0), Long.valueOf(fields.get(1)));
        return endPosition;
    } catch (IOException e) {
        throw new CanalParseException("command : 'show binlog events limit 1' has an error!", e);
    }

}
 
Example 13
Source File: MysqlEventParser.java    From canal with Apache License 2.0 5 votes vote down vote up
/**
 * 查询当前db的serverId信息
 */
private Long findServerId(MysqlConnection mysqlConnection) {
    try {
        ResultSetPacket packet = mysqlConnection.query("show variables like 'server_id'");
        List<String> fields = packet.getFieldValues();
        if (CollectionUtils.isEmpty(fields)) {
            throw new CanalParseException("command : show variables like 'server_id' has an error! pls check. you need (at least one of) the SUPER,REPLICATION CLIENT privilege(s) for this operation");
        }
        return Long.valueOf(fields.get(1));
    } catch (IOException e) {
        throw new CanalParseException("command : show variables like 'server_id' has an error!", e);
    }
}
 
Example 14
Source File: MysqlConnection.java    From canal with Apache License 2.0 5 votes vote down vote up
@Override
public long queryServerId() throws IOException {
    ResultSetPacket resultSetPacket = query("show variables like 'server_id'");
    List<String> fieldValues = resultSetPacket.getFieldValues();
    if (fieldValues == null || fieldValues.size() != 2) {
        return 0;
    }
    return NumberUtils.toLong(fieldValues.get(1));
}
 
Example 15
Source File: DirectLogFetcherTest.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
private void loadBinlogChecksum(MysqlConnector connector) {
    ResultSetPacket rs = null;
    try {
        rs = query("select @@global.binlog_checksum", connector);
    } catch (IOException e) {
        throw new CanalParseException(e);
    }

    List<String> columnValues = rs.getFieldValues();
    if (columnValues != null && columnValues.size() >= 1 && columnValues.get(0).toUpperCase().equals("CRC32")) {
        binlogChecksum = LogEvent.BINLOG_CHECKSUM_ALG_CRC32;
    } else {
        binlogChecksum = LogEvent.BINLOG_CHECKSUM_ALG_OFF;
    }
}
 
Example 16
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 17
Source File: MysqlEventParser.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
/**
 * 查询当前的binlog位置
 */
private EntryPosition findStartPosition(MysqlConnection mysqlConnection) {
    try {
        ResultSetPacket packet = mysqlConnection.query("show binlog events limit 1");
        List<String> fields = packet.getFieldValues();
        if (CollectionUtils.isEmpty(fields)) {
            throw new CanalParseException("command : 'show binlog events limit 1' has an error! pls check. you need (at least one of) the SUPER,REPLICATION CLIENT privilege(s) for this operation");
        }
        EntryPosition endPosition = new EntryPosition(fields.get(0), Long.valueOf(fields.get(1)));
        return endPosition;
    } catch (IOException e) {
        throw new CanalParseException("command : 'show binlog events limit 1' has an error!", e);
    }

}
 
Example 18
Source File: MysqlEventParser.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
/**
 * 查询当前db的serverId信息
 */
private Long findServerId(MysqlConnection mysqlConnection) {
    try {
        ResultSetPacket packet = mysqlConnection.query("show variables like 'server_id'");
        List<String> fields = packet.getFieldValues();
        if (CollectionUtils.isEmpty(fields)) {
            throw new CanalParseException("command : show variables like 'server_id' has an error! pls check. you need (at least one of) the SUPER,REPLICATION CLIENT privilege(s) for this operation");
        }
        return Long.valueOf(fields.get(1));
    } catch (IOException e) {
        throw new CanalParseException("command : show variables like 'server_id' has an error!", e);
    }
}