Search in sources :

Example 1 with FieldPacket

use of com.alibaba.otter.canal.parse.driver.mysql.packets.server.FieldPacket 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 FieldPacket

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

the class TableMetaCache method parserTableMeta.

private List<FieldMeta> parserTableMeta(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.setIsNullable(packet.getFieldValues().get(nameMaps.get(IS_NULLABLE) + i * size));
        meta.setIskey(packet.getFieldValues().get(nameMaps.get(COLUMN_KEY) + i * size));
        meta.setDefaultValue(packet.getFieldValues().get(nameMaps.get(COLUMN_DEFAULT) + i * size));
        meta.setExtra(packet.getFieldValues().get(nameMaps.get(EXTRA) + i * size));
        result.add(meta);
    }
    return result;
}
Also used : HashMap(java.util.HashMap) FieldMeta(com.alibaba.otter.canal.parse.inbound.TableMeta.FieldMeta) ArrayList(java.util.ArrayList) FieldPacket(com.alibaba.otter.canal.parse.driver.mysql.packets.server.FieldPacket)

Example 3 with FieldPacket

use of com.alibaba.otter.canal.parse.driver.mysql.packets.server.FieldPacket 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)

Aggregations

FieldPacket (com.alibaba.otter.canal.parse.driver.mysql.packets.server.FieldPacket)3 ResultSetPacket (com.alibaba.otter.canal.parse.driver.mysql.packets.server.ResultSetPacket)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)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 FieldMeta (com.alibaba.otter.canal.parse.inbound.TableMeta.FieldMeta)1