Class BinlogSourceInfo


@NotThreadSafe public class BinlogSourceInfo extends BaseSourceInfo
Information about the source, including the position in the source binary log we have processed.

The BinlogPartition.getSourcePartition() information describes the database whose log is currently being processed. Typically, the database is identified by the host address port of the database server and the name of the database. Here's a JSON representation of an example database:
     {
         "server": "production-server"
     }
 
The offset includes the binlogFilename() and the binlogPosition(), along with details on how many BinlogOffsetContext.eventsToSkipUponRestart() and the BinlogOffsetContext.rowsToSkipUponRestart(). Another JSON example:
     {
         "server_id": 112233,
         "ts_sec": 1234567,
         "gtid": "<binary-log-database-formatted-gtid>",
         "file": "mysql-bin.000003",
         "pos": 990,
         "event": 0,
         "row": 0,
         "snapshot": true
     }
 
The "gtid" field only appears in offsets produced when GTIDs are enabled. The "@{code snapshot}" field only appears in offsets produced when the connector is in the middle of a snapshot. And finally, the "ts" field contains the seconds since the Unix epoch for the database event; the message Envelope also has a timestamp, but that timestamp is in milliseconds since the Unix epoch.

Each change event envelope also includes the AbstractSourceInfo.struct() that contains database information about that specific event, including a mixture of the fields from the binlog filename and position where the event can be found, and when GTIDs are enabled, the GTID of the transaction in which the event occurs. Like with the offset, the "snapshot" field only appears for events produced when the connector is in the middle of a snapshot. Note that this information is likely different from the offset information, since the connector may need to restart from either just after the most recently completed transaction or the beginning of the most recently started transaction (whichever appears later in the binlog).

Here's a JSON representation of the source metadata for an event that represents the partition and offset:
     {
         "name": "production-server",
         "server_id": 112233,
         "ts_sec", 1234567,
         "gtid": "<binary-log-database-formatted-gtid>",
         "file": "mysql-bin.000003",
         "pos": 1081,
         "row": 0,
         "snapshot": true,
         "thread": 1,
         "db": "inventory",
         "table": "products"
     }
 
Author:
Randall Hauch, Chris Cranford
  • Field Details

  • Constructor Details

  • Method Details

    • timestamp

      protected Instant timestamp()
      Specified by:
      timestamp in class AbstractSourceInfo
    • database

      protected String database()
      Specified by:
      database in class AbstractSourceInfo
    • setQuery

      public void setQuery(String query)
      Set the original SQL query.
      Parameters:
      query - the original SQL query that generated the event.
    • getQuery

      public String getQuery()
      Returns:
      the original SQL query that generated the event. NULL if no such query is associated.
    • setBinlogPosition

      public void setBinlogPosition(String binlogFilename, long positionOfFirstEvent)
      Set the position in the binlog where we will start reading.
      Parameters:
      binlogFilename - the name of the binary log file; may not be null
      positionOfFirstEvent - the position in the binary log file to begin processing
    • setEventPosition

      public void setEventPosition(long positionOfCurrentEvent)
      Set the position within the binary log file of the current event.
      Parameters:
      positionOfCurrentEvent - the position within the binary log file of the current event
    • setRowNumber

      public void setRowNumber(int eventRowNumber)
      Given the row number within a binlog event and the total number of rows in that event, compute the Kafka Connect offset that is to be included in the produced change event describing the row.

      This method should always be called before AbstractSourceInfo.struct().
      Parameters:
      eventRowNumber - the 0-based row number within the event for which the offset is to be produced
      See Also:
    • databaseEvent

      public void databaseEvent(String databaseName)
      Set the database that the current binlog event represents.
      Parameters:
      databaseName - the database name
    • tableEvent

      public void tableEvent(Set<TableId> tableIds)
      Sets the associated table identifiers that the current binlog event represents.
      Parameters:
      tableIds - set of table ids, should not be null but may be empty
    • tableEvent

      public void tableEvent(TableId tableId)
      Sets the associate table identifier that the current binlog event represents.
      Parameters:
      tableId - the table id, should not be null
    • startGtid

      public void startGtid(String gtid)
      Set the starting global transaction identifier
      Parameters:
      gtid - the global transaction identifier
    • setBinlogServerId

      public void setBinlogServerId(long serverId)
      Set the server ID as found within the binary log file.
      Parameters:
      serverId - the server ID found within the binary log file
    • setSourceTime

      public void setSourceTime(Instant timestamp)
      Set the instant from the binlog log file for the specified event.
      Parameters:
      timestamp - the time the binlog event occurred
    • setBinlogThread

      public void setBinlogThread(long threadId)
      Set the identifier of the binlog thread that generated the most recent event.
      Parameters:
      threadId - the thread identifier; may be negative if not known
    • binlogFilename

      public String binlogFilename()
      Get the name of the binary log file that has last been processed.
      Returns:
      the name of the binary log file; null if it has not been set
    • binlogPosition

      public long binlogPosition()
      Get the position within the binary log file of the next event to be processed.
      Returns:
      the position within the binary log file; zero if it has not been set
    • getServerId

      public long getServerId()
      Get the server ID
      Returns:
      the server ID within the binary log file; 0 if it has not been set
    • getThreadId

      public long getThreadId()
      Get the identifier of the binlog thread
      Returns:
      the binary log thread identifier; -1 if it has not been set
    • table

      public String table()
      Returns a string representation of the table(s) affected by the current event. Will only represent more than a single table for events in the user-facing schema history topic for certain types of DDL events. Will be null for DDL events not applying to tables, i.e. CREATE DATABASE.
    • getCurrentGtid

      public String getCurrentGtid()
      Get the current recorded global transaction identifier (GTID).
      Returns:
      the current global transaction identifier (GTID); will be null if GTID is not enabled
    • getCurrentBinlogFilename

      public String getCurrentBinlogFilename()
      Get the current binlog file being processed.
      Returns:
      the current binlog file being processed; null if it has not been set
    • getCurrentBinlogPosition

      public long getCurrentBinlogPosition()
      Get the current binlog file position.
      Returns:
      the current binlog file position; 0 if it has not been set
    • getCurrentRowNumber

      public int getCurrentRowNumber()
      Returns the computed row number within a binlog event.
      Returns:
      the computed binlog event row number; 0 if it has not been set
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • createDocumentFromOffset

      public static Document createDocumentFromOffset(Map<String,?> offset)
      Create a Document from the given offset.
      Parameters:
      offset - the offset to create the document from.
      Returns:
      a Document with the offset data.