示例#1
0
 def processEntry(self, entry):
     entry_type = entry.entryType
     if entry_type in [
             EntryProtocol_pb2.EntryType.TRANSACTIONBEGIN,
             EntryProtocol_pb2.EntryType.TRANSACTIONEND
     ]:
         return 2
     row_change = EntryProtocol_pb2.RowChange()
     row_change.MergeFromString(entry.storeValue)
     event_type = row_change.eventType
     header = entry.header
     database = header.schemaName
     table = header.tableName
     event_type = header.eventType
     for row in row_change.rowDatas:
         if self.processRow(row, table, event_type) == 0:
             return 0
     return 1
示例#2
0
    def get_without_ack(self, batch_size=10, timeout=-1, unit=-1):
        get = CanalProtocol_pb2.Get()
        get.client_id = self.client_id
        get.destination = self.destination
        get.auto_ack = False
        get.fetch_size = batch_size
        get.timeout = timeout
        get.unit = unit

        packet = CanalProtocol_pb2.Packet()
        packet.type = CanalProtocol_pb2.PacketType.GET
        packet.body = get.SerializeToString()

        self.connector.write_with_header(packet.SerializeToString())

        data = self.connector.read_next_packet()
        packet = CanalProtocol_pb2.Packet()
        packet.MergeFromString(data)

        message = dict(id=0, entries=[])
        if packet.type == CanalProtocol_pb2.PacketType.MESSAGES:
            messages = CanalProtocol_pb2.Messages()
            messages.MergeFromString(packet.body)
            if messages.batch_id > 0:
                message['id'] = messages.batch_id
                for item in messages.messages:
                    entry = EntryProtocol_pb2.Entry()
                    entry.MergeFromString(item)
                    message['entries'].append(entry)
        elif packet.type == CanalProtocol_pb2.PacketType.ACK:
            ack = CanalProtocol_pb2.PacketType.Ack()
            ack.MergeFromString(packet.body)
            if ack.error_code > 0:
                raise Exception(
                    'get data error. error code:%s, error message:%s' %
                    (ack.error_code, ack.error_message))
        else:
            raise Exception('unexpected packet type:%s' % (packet.type))

        return message
示例#3
0
client = Client()
client.connect(host='localhost', port=11111)
client.check_valid("canal", "canal")
client.subscribe(destination="example")

while True:
    message = client.get(100)
    entries = message['entries']
    for entry in entries:
        entry_type = entry.entryType
        if entry_type in [
                EntryProtocol_pb2.EntryType.TRANSACTIONBEGIN,
                EntryProtocol_pb2.EntryType.TRANSACTIONEND
        ]:
            continue
        row_change = EntryProtocol_pb2.RowChange()
        row_change.MergeFromString(entry.storeValue)
        event_type = row_change.eventType
        header = entry.header
        database = header.schemaName
        table = header.tableName
        event_type = header.eventType
        for row in row_change.rowDatas:
            format_data = dict()
            if event_type == EntryProtocol_pb2.EventType.DELETE:
                for column in row.beforeColumns:
                    format_data = {column.name: column.value}
            elif event_type == EntryProtocol_pb2.EventType.INSERT:
                for column in row.afterColumns:
                    format_data = {column.name: column.value}
            else: