Skip to content

Change data processing

Change data processing calls a stored procedure for each change an incremental group applies, in addition to applying it to the destination table or, with Only Stream Change Data, instead of it. You write the procedure, so it can do whatever your process needs with the changes, for example:

  • put a message on a queue such as IBM MQ, RabbitMQ or Microsoft Message Queuing, for a downstream system;
  • keep an audit trail of what was applied, with keys or full row images;
  • record only the changes that failed to apply, to investigate them;
  • send an email.

Without a procedure of your own, SQDR's procedure SQLOGGER, in the control database, writes each change to the key log.

Set it up

  1. On the incremental group's Advanced tab, under Change Data Logging, choose the Logging Level:

    Logging Level What is passed to the procedure
    *Disabled Nothing (default).
    Data-Always Every change, with its keys and data.
    Keys-Always Every change, with its keys only.
    Errors-Only Only changes that failed to apply.
  2. Choose where the procedure runs:

    • Control DBMS (default): in the control database, with the name qualified by the control database's schema.
    • Dest. DBMS: in the destination database, in the same transaction as the changes it records. Enter the name as the destination expects it.
  3. Enter your procedure's name in Stored Procedure (see Write your own procedure). Left as SQLOGGER, or empty, the changes go to the key log.
  4. Choose the Encoding of keys and data (JSON or XML; see What your procedure receives) and whether the Before Image of updates is included Separately or Combined. See Change Data Logging.
  5. Save the group and resume it.

Process the changes only

To leave the destination tables untouched and only pass the changes to your procedure:

Use Batch

Change data logging and Only Stream Change Data are handled by the Batch apply method. Keep groups that rely on them on Batch; see Under each apply method.

Write your own procedure

Start from a copy of SQLOGGER and change what it does with the values. Give your copy a different name. SQLOGGER's definition is in the scripts the SQDR Configuration Tool runs to create the control database, CNTRLDB_UDB.SQL (Db2 for LUW) and CNTRLDB_MSSQL.SQL (SQL Server), installed with SQDR (see Scripts and tools); you can also script it from the control database.

The procedure must accept these 21 parameters, in this order. What each one holds is described with the key log's columns, which have the same names (see Read the key log).

# Parameter Db2 for LUW type SQL Server type
1 group_id CHAR(16) FOR BIT DATA uniqueidentifier
2 subscription_id CHAR(16) FOR BIT DATA uniqueidentifier
3 resync_state CHAR(1) char(1)
4 transaction_id CHAR(10) FOR BIT DATA binary(10)
5 change_row_counter BIGINT bigint
6 change_row_sequence BIGINT bigint
7 change_row_log_detail VARCHAR(512) varchar(512)
8 change_row_member VARCHAR(10) varchar(10)
9 change_row_timestamp TIMESTAMP datetime
10 change_row_nanosecond INTEGER int
11 change_row_type CHAR(1) char(1)
12 action_taken CHAR(1) char(1)
13 result_row_count INTEGER int
14 flagged_count BIGINT bigint
15 keylog_ts TIMESTAMP datetime
16 result_error CLOB(64000) varchar(max)
17 before_key CLOB(64000) varchar(max)
18 before_key_edited CLOB(64000) varchar(max)
19 after_key CLOB(64000) varchar(max)
20 after_key_edited CLOB(64000) varchar(max)
21 after_image CLOB(64000) varchar(max)

What your procedure receives

The keys (before_key, after_key) and the data (after_image) are text in the group's Encoding:

  • JSON (the default): one object, with a member per column, {"COLUMN": value, ...}; a null is null.
  • XML: an element per column, <COLUMN>value</COLUMN>; a null is <COLUMN xsi:nil="true" />.

The keys always include every key column. In the data, a column can also be left out: depending on the source, its value is null, or it didn't change in an update.

Example: a Java procedure that writes the changes to a file

This sample procedure, SQLOGTOFILE, appends each change it receives (its type, keys and data) to a text file per group, in C:\temp on Windows or /tmp in the Docker container. Make sure the folder exists. Use it as a template: replace the file writing with what your process needs, such as putting a message on a queue.

It's for a Db2 for LUW database: the control database (the usual case, in the SQDR schema next to SQLOGGER) or a Db2 destination.

  1. Build the Java class below into a JAR and register it in the database with the CREATE PROCEDURE statement below: for example with IBM Data Studio (a Data Development Project with a Java stored procedure), or with javac, jar and CALL SQLJ.INSTALL_JAR, naming the JAR SQLOGTOFILE.
  2. The user who creates the procedure needs the CREATE_EXTERNAL_ROUTINE authority. Without it, creating the procedure fails with SQLCODE -552 (SQLSTATE 42502); a database administrator grants it with GRANT CREATE_EXTERNAL_ROUTINE ON DATABASE TO <user>.
  3. In the group's Change Data Logging, choose a Logging Level (for example Data-Always), Control DBMS or Dest. DBMS for where you created it, and SQLOGTOFILE as the Stored Procedure (see Set it up).
  4. When the group next applies changes, check that the file is created and holds the changes.
CREATE PROCEDURE SQLOGTOFILE ( IN groupId CHAR (16) FOR BIT DATA,
  IN subscriptionId CHAR (16) FOR BIT DATA,
  IN resyncState CHAR(1),
  IN transaction_id CHAR (10) FOR BIT DATA,
  IN changeRowCounter BIGINT,
  IN changeRowSequence BIGINT,
  IN changeRowLogDetail VARCHAR(512),
  IN change_row_member VARCHAR(10),
  IN changeRowTimestamp TIMESTAMP,
  IN changeRowNanosecond INTEGER,
  IN changeRowType CHAR(1),
  IN actionTaken CHAR(1),
  IN resultRowCount INTEGER,
  IN flaggedCount BIGINT,
  IN keylogTs TIMESTAMP,
  IN resultError CLOB(64000),
  IN beforeKey CLOB(64000),
  IN beforeKeyEdited CLOB(64000),
  IN afterKey CLOB(64000),
  IN afterKeyEdited CLOB(64000),
  IN afterImage CLOB(64000) )
NOT DETERMINISTIC CALLED ON NULL INPUT
LANGUAGE Java
EXTERNAL NAME 'SQLOGTOFILE:com.starquest.logtofile.SQLOGTOFILE.x_SQLOGTOFILE'
FENCED
THREADSAFE
PARAMETER STYLE JAVA
package com.starquest.logtofile;

import java.sql.Clob;
import java.net.URI;
import java.sql.Timestamp;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.OpenOption;
import java.nio.file.StandardOpenOption;
import java.math.BigInteger;

public class SQLOGTOFILE {

    public static void x_SQLOGTOFILE(byte[] groupId, byte[] subscriptionId, String resyncState, byte[] transaction_id,
            long changeRowCounter, long changeRowSequence, String changeRowLogDetail, String change_row_member,
            Timestamp changeRowTimestamp, int changeRowNanosecond, String changeRowType, String actionTaken,
            int resultRowCount, long flaggedCount, Timestamp keylogTs, Clob resultError, Clob beforeKey,
            Clob beforeKeyEdited, Clob afterKey, Clob afterKeyEdited, Clob afterImage) {
        try {
            OpenOption[] options = { StandardOpenOption.CREATE, StandardOpenOption.APPEND };
            String filename = "log" + (new BigInteger(groupId)).toString(16) + ".txt";
            URI uri = System.getProperty("file.separator").equals("\\")
                    ? new URI("file:///C:/temp/" + filename)
                    : new URI("file:///tmp/" + filename);
            Path p = Paths.get(uri);
            PrintStream out = new PrintStream(Files.newOutputStream(p, options));
            out.println("<dml_type>" + changeRowType + "</dml_type>"
                    + "<beforekey>" + (beforeKey != null ? beforeKey.getSubString(1, (int) beforeKey.length()) : "NULL") + "</beforekey>"
                    + "<afterkey>" + (afterKey != null ? afterKey.getSubString(1, (int) afterKey.length()) : "NULL") + "</afterkey>"
                    + "<afterimage>" + (afterImage != null ? afterImage.getSubString(1, (int) afterImage.length()) : "NULL") + "</afterimage>");
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}