Search in sources :

Example 1 with ResultSetPacket

use of com.alibaba.otter.canal.parse.driver.mysql.packets.server.ResultSetPacket in project canal by alibaba.

the class MysqlQueryExecutor method query.

/**
     * (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.write(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.socket().getRemoteSocketAddress());
    return resultSet;
}
Also used : ResultSetHeaderPacket(com.alibaba.otter.canal.parse.driver.mysql.packets.server.ResultSetHeaderPacket) QueryCommandPacket(com.alibaba.otter.canal.parse.driver.mysql.packets.client.QueryCommandPacket) ErrorPacket(com.alibaba.otter.canal.parse.driver.mysql.packets.server.ErrorPacket) RowDataPacket(com.alibaba.otter.canal.parse.driver.mysql.packets.server.RowDataPacket) ResultSetPacket(com.alibaba.otter.canal.parse.driver.mysql.packets.server.ResultSetPacket) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FieldPacket(com.alibaba.otter.canal.parse.driver.mysql.packets.server.FieldPacket)

Example 2 with ResultSetPacket

use of com.alibaba.otter.canal.parse.driver.mysql.packets.server.ResultSetPacket in project canal by alibaba.

the class MysqlEventParser method findServerId.

/**
     * 查询当前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);
    }
}
Also used : ResultSetPacket(com.alibaba.otter.canal.parse.driver.mysql.packets.server.ResultSetPacket) IOException(java.io.IOException) CanalParseException(com.alibaba.otter.canal.parse.exception.CanalParseException)

Example 3 with ResultSetPacket

use of com.alibaba.otter.canal.parse.driver.mysql.packets.server.ResultSetPacket in project canal by alibaba.

the class MysqlEventParser method findEndPosition.

/**
     * 查询当前的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)));
        return endPosition;
    } catch (IOException e) {
        throw new CanalParseException("command : 'show master status' has an error!", e);
    }
}
Also used : ResultSetPacket(com.alibaba.otter.canal.parse.driver.mysql.packets.server.ResultSetPacket) EntryPosition(com.alibaba.otter.canal.protocol.position.EntryPosition) IOException(java.io.IOException) CanalParseException(com.alibaba.otter.canal.parse.exception.CanalParseException)

Example 4 with ResultSetPacket

use of com.alibaba.otter.canal.parse.driver.mysql.packets.server.ResultSetPacket in project canal by alibaba.

the class MysqlConnection method loadBinlogImage.

/**
     * 获取一下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());
    }
}
Also used : ResultSetPacket(com.alibaba.otter.canal.parse.driver.mysql.packets.server.ResultSetPacket) IOException(java.io.IOException) CanalParseException(com.alibaba.otter.canal.parse.exception.CanalParseException)

Example 5 with ResultSetPacket

use of com.alibaba.otter.canal.parse.driver.mysql.packets.server.ResultSetPacket in project canal by alibaba.

the class MysqlConnection method loadBinlogChecksum.

/**
     * 获取主库checksum信息
     * https://dev.mysql.com/doc/refman/5.6/en/replication-options
     * -binary-log.html#option_mysqld_binlog-checksum
     */
private void loadBinlogChecksum() {
    ResultSetPacket rs = null;
    try {
        rs = query("select @master_binlog_checksum");
    } catch (IOException e) {
        throw new CanalParseException(e);
    }
    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;
    }
}
Also used : ResultSetPacket(com.alibaba.otter.canal.parse.driver.mysql.packets.server.ResultSetPacket) IOException(java.io.IOException) CanalParseException(com.alibaba.otter.canal.parse.exception.CanalParseException)

Aggregations

ResultSetPacket (com.alibaba.otter.canal.parse.driver.mysql.packets.server.ResultSetPacket)9 IOException (java.io.IOException)9 CanalParseException (com.alibaba.otter.canal.parse.exception.CanalParseException)6 FieldPacket (com.alibaba.otter.canal.parse.driver.mysql.packets.server.FieldPacket)2 EntryPosition (com.alibaba.otter.canal.protocol.position.EntryPosition)2 QueryCommandPacket (com.alibaba.otter.canal.parse.driver.mysql.packets.client.QueryCommandPacket)1 ErrorPacket (com.alibaba.otter.canal.parse.driver.mysql.packets.server.ErrorPacket)1 ResultSetHeaderPacket (com.alibaba.otter.canal.parse.driver.mysql.packets.server.ResultSetHeaderPacket)1 RowDataPacket (com.alibaba.otter.canal.parse.driver.mysql.packets.server.RowDataPacket)1 InetSocketAddress (java.net.InetSocketAddress)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1