Search in sources :

Example 6 with ResultSetPacket

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

the class MysqlConnection method loadBinlogFormat.

/**
     * 获取一下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());
    }
}
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 7 with ResultSetPacket

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

the class MysqlEventParser method findStartPosition.

/**
     * 查询当前的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);
    }
}
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 8 with ResultSetPacket

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

the class MysqlEventParser method findSlavePosition.

/**
     * 查询当前的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");
        // Slave_SQL_Running
        String slaveIORunning = maps.get("Slave_IO_Running");
        // Slave_SQL_Running
        String slaveSQLRunning = maps.get("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;
}
Also used : HashMap(java.util.HashMap) ResultSetPacket(com.alibaba.otter.canal.parse.driver.mysql.packets.server.ResultSetPacket) IOException(java.io.IOException) FieldPacket(com.alibaba.otter.canal.parse.driver.mysql.packets.server.FieldPacket)

Example 9 with ResultSetPacket

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

the class MysqlConnectorTest method testQuery.

@Test
public void testQuery() {
    MysqlConnector connector = new MysqlConnector(new InetSocketAddress("127.0.0.1", 3306), "xxxxx", "xxxxx");
    try {
        connector.connect();
        MysqlQueryExecutor executor = new MysqlQueryExecutor(connector);
        ResultSetPacket result = executor.query("show variables like '%char%';");
        System.out.println(result);
        result = executor.query("select * from test.test1");
        System.out.println(result);
    } catch (IOException e) {
        Assert.fail(e.getMessage());
    } finally {
        try {
            connector.disconnect();
        } catch (IOException e) {
            Assert.fail(e.getMessage());
        }
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ResultSetPacket(com.alibaba.otter.canal.parse.driver.mysql.packets.server.ResultSetPacket) IOException(java.io.IOException) Test(org.junit.Test)

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