Java Code Examples for com.alibaba.otter.canal.parse.driver.mysql.packets.server.ResultSetPacket

The following examples show how to use com.alibaba.otter.canal.parse.driver.mysql.packets.server.ResultSetPacket. These examples are extracted from open source projects. 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 Project: canal-1.1.3   Source File: MysqlConnection.java    License: 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 Project: canal-1.1.3   Source File: MysqlConnection.java    License: 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 Project: canal-1.1.3   Source File: MysqlConnection.java    License: 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 Project: canal-1.1.3   Source File: MysqlEventParser.java    License: 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 Project: canal-1.1.3   Source File: TableMetaCacheTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSimple() throws IOException {
    MysqlConnection connection = new MysqlConnection(new InetSocketAddress("127.0.0.1", 3306), "root", "hello");
    try {
        connection.connect();
    } catch (IOException e) {
        Assert.fail(e.getMessage());
    }

    List<ResultSetPacket> packets = connection.queryMulti("show create table test.ljh_test");
    String createDDL = null;
    if (packets.get(0).getFieldValues().size() > 0) {
        createDDL = packets.get(0).getFieldValues().get(1);
    }

    System.out.println(createDDL);

    // TableMetaCache cache = new TableMetaCache(connection);
    // TableMeta meta = cache.getTableMeta("otter1", "otter_stability1");
    // Assert.assertNotNull(meta);
    // for (FieldMeta field : meta.getFields()) {
    // System.out.println("filed :" + field.getColumnName() + " , isKey : "
    // + field.isKey() + " , isNull : "
    // + field.isNullable());
    // }
}
 
Example 6
Source Project: canal   Source File: MysqlConnection.java    License: 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 Project: canal   Source File: MysqlConnection.java    License: 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 Project: canal   Source File: MysqlConnection.java    License: 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 Project: canal   Source File: MysqlEventParser.java    License: 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 10
Source Project: canal   Source File: TableMetaCacheTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSimple() throws IOException {
    MysqlConnection connection = new MysqlConnection(new InetSocketAddress("127.0.0.1", 3306), "root", "hello");
    try {
        connection.connect();
    } catch (IOException e) {
        Assert.fail(e.getMessage());
    }

    List<ResultSetPacket> packets = connection.queryMulti("show create table test.ljh_test");
    String createDDL = null;
    if (packets.get(0).getFieldValues().size() > 0) {
        createDDL = packets.get(0).getFieldValues().get(1);
    }

    System.out.println(createDDL);

    // TableMetaCache cache = new TableMetaCache(connection);
    // TableMeta meta = cache.getTableMeta("otter1", "otter_stability1");
    // Assert.assertNotNull(meta);
    // for (FieldMeta field : meta.getFields()) {
    // System.out.println("filed :" + field.getColumnName() + " , isKey : "
    // + field.isKey() + " , isNull : "
    // + field.isNullable());
    // }
}
 
Example 11
Source Project: canal-1.1.3   Source File: MysqlConnection.java    License: 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 12
Source Project: canal-1.1.3   Source File: TableMetaCache.java    License: Apache License 2.0 5 votes vote down vote up
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<FieldMeta>();
    }
}
 
Example 13
Source Project: canal-1.1.3   Source File: TableMetaCache.java    License: 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 14
Source Project: canal-1.1.3   Source File: MysqlEventParser.java    License: 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 15
Source Project: canal-1.1.3   Source File: MysqlEventParser.java    License: 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 16
Source Project: canal-1.1.3   Source File: MysqlEventParser.java    License: 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 Project: canal-1.1.3   Source File: DirectLogFetcherTest.java    License: 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 18
Source Project: canal   Source File: MysqlConnection.java    License: 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 19
Source Project: canal   Source File: TableMetaCache.java    License: Apache License 2.0 5 votes vote down vote up
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<FieldMeta>();
    }
}
 
Example 20
Source Project: canal   Source File: TableMetaCache.java    License: 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 21
Source Project: canal   Source File: MysqlEventParser.java    License: 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 22
Source Project: canal   Source File: MysqlEventParser.java    License: 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 23
Source Project: canal   Source File: MysqlEventParser.java    License: 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 24
Source Project: canal   Source File: DirectLogFetcherTest.java    License: 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 25
Source Project: canal-1.1.3   Source File: MysqlQueryExecutor.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * (Result Set Header Packet) the number of columns <br>
 * (Field Packets) column descriptors <br>
 * (EOF Packet) marker: end of Field Packets <br>
 * (Row Data Packets) row contents <br>
 * (EOF Packet) marker: end of Data Packets
 * 
 * @param queryString
 * @return
 * @throws IOException
 */
public ResultSetPacket query(String queryString) throws IOException {
    QueryCommandPacket cmd = new QueryCommandPacket();
    cmd.setQueryString(queryString);
    byte[] bodyBytes = cmd.toBytes();
    PacketManager.writeBody(channel, bodyBytes);
    byte[] body = readNextPacket();

    if (body[0] < 0) {
        ErrorPacket packet = new ErrorPacket();
        packet.fromBytes(body);
        throw new IOException(packet + "\n with command: " + queryString);
    }

    ResultSetHeaderPacket rsHeader = new ResultSetHeaderPacket();
    rsHeader.fromBytes(body);

    List<FieldPacket> fields = new ArrayList<FieldPacket>();
    for (int i = 0; i < rsHeader.getColumnCount(); i++) {
        FieldPacket fp = new FieldPacket();
        fp.fromBytes(readNextPacket());
        fields.add(fp);
    }

    readEofPacket();

    List<RowDataPacket> rowData = new ArrayList<RowDataPacket>();
    while (true) {
        body = readNextPacket();
        if (body[0] == -2) {
            break;
        }
        RowDataPacket rowDataPacket = new RowDataPacket();
        rowDataPacket.fromBytes(body);
        rowData.add(rowDataPacket);
    }

    ResultSetPacket resultSet = new ResultSetPacket();
    resultSet.getFieldDescriptors().addAll(fields);
    for (RowDataPacket r : rowData) {
        resultSet.getFieldValues().addAll(r.getColumns());
    }
    resultSet.setSourceAddress(channel.getRemoteSocketAddress());

    return resultSet;
}
 
Example 26
Source Project: canal-1.1.3   Source File: MysqlQueryExecutor.java    License: Apache License 2.0 4 votes vote down vote up
public List<ResultSetPacket> queryMulti(String queryString) throws IOException {
    QueryCommandPacket cmd = new QueryCommandPacket();
    cmd.setQueryString(queryString);
    byte[] bodyBytes = cmd.toBytes();
    PacketManager.writeBody(channel, bodyBytes);
    List<ResultSetPacket> resultSets = new ArrayList<ResultSetPacket>();
    boolean moreResult = true;
    while (moreResult) {
        byte[] body = readNextPacket();
        if (body[0] < 0) {
            ErrorPacket packet = new ErrorPacket();
            packet.fromBytes(body);
            throw new IOException(packet + "\n with command: " + queryString);
        }

        ResultSetHeaderPacket rsHeader = new ResultSetHeaderPacket();
        rsHeader.fromBytes(body);

        List<FieldPacket> fields = new ArrayList<FieldPacket>();
        for (int i = 0; i < rsHeader.getColumnCount(); i++) {
            FieldPacket fp = new FieldPacket();
            fp.fromBytes(readNextPacket());
            fields.add(fp);
        }

        moreResult = readEofPacket();

        List<RowDataPacket> rowData = new ArrayList<RowDataPacket>();
        while (true) {
            body = readNextPacket();
            if (body[0] == -2) {
                break;
            }
            RowDataPacket rowDataPacket = new RowDataPacket();
            rowDataPacket.fromBytes(body);
            rowData.add(rowDataPacket);
        }

        ResultSetPacket resultSet = new ResultSetPacket();
        resultSet.getFieldDescriptors().addAll(fields);
        for (RowDataPacket r : rowData) {
            resultSet.getFieldValues().addAll(r.getColumns());
        }
        resultSet.setSourceAddress(channel.getRemoteSocketAddress());
        resultSets.add(resultSet);
    }

    return resultSets;
}
 
Example 27
Source Project: canal-1.1.3   Source File: MysqlConnection.java    License: Apache License 2.0 4 votes vote down vote up
public ResultSetPacket query(String cmd) throws IOException {
    MysqlQueryExecutor exector = new MysqlQueryExecutor(connector);
    return exector.query(cmd);
}
 
Example 28
Source Project: canal-1.1.3   Source File: MysqlConnection.java    License: Apache License 2.0 4 votes vote down vote up
public List<ResultSetPacket> queryMulti(String cmd) throws IOException {
    MysqlQueryExecutor exector = new MysqlQueryExecutor(connector);
    return exector.queryMulti(cmd);
}
 
Example 29
Source Project: canal-1.1.3   Source File: DirectLogFetcherTest.java    License: Apache License 2.0 4 votes vote down vote up
public ResultSetPacket query(String cmd, MysqlConnector connector) throws IOException {
    MysqlQueryExecutor exector = new MysqlQueryExecutor(connector);
    return exector.query(cmd);
}
 
Example 30
Source Project: canal   Source File: MysqlQueryExecutor.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * (Result Set Header Packet) the number of columns <br>
 * (Field Packets) column descriptors <br>
 * (EOF Packet) marker: end of Field Packets <br>
 * (Row Data Packets) row contents <br>
 * (EOF Packet) marker: end of Data Packets
 * 
 * @param queryString
 * @return
 * @throws IOException
 */
public ResultSetPacket query(String queryString) throws IOException {
    QueryCommandPacket cmd = new QueryCommandPacket();
    cmd.setQueryString(queryString);
    byte[] bodyBytes = cmd.toBytes();
    PacketManager.writeBody(channel, bodyBytes);
    byte[] body = readNextPacket();

    if (body[0] < 0) {
        ErrorPacket packet = new ErrorPacket();
        packet.fromBytes(body);
        throw new IOException(packet + "\n with command: " + queryString);
    }

    ResultSetHeaderPacket rsHeader = new ResultSetHeaderPacket();
    rsHeader.fromBytes(body);

    List<FieldPacket> fields = new ArrayList<FieldPacket>();
    for (int i = 0; i < rsHeader.getColumnCount(); i++) {
        FieldPacket fp = new FieldPacket();
        fp.fromBytes(readNextPacket());
        fields.add(fp);
    }

    readEofPacket();

    List<RowDataPacket> rowData = new ArrayList<RowDataPacket>();
    while (true) {
        body = readNextPacket();
        if (body[0] == -2) {
            break;
        }
        RowDataPacket rowDataPacket = new RowDataPacket();
        rowDataPacket.fromBytes(body);
        rowData.add(rowDataPacket);
    }

    ResultSetPacket resultSet = new ResultSetPacket();
    resultSet.getFieldDescriptors().addAll(fields);
    for (RowDataPacket r : rowData) {
        resultSet.getFieldValues().addAll(r.getColumns());
    }
    resultSet.setSourceAddress(channel.getRemoteSocketAddress());

    return resultSet;
}