Search in sources :

Example 1 with ColumnDefinition

use of org.apache.nifi.cdc.event.ColumnDefinition in project nifi by apache.

the class InsertRowsWriter method writeRow.

protected void writeRow(InsertRowsEventInfo event, Serializable[] row, BitSet includedColumns) throws IOException {
    jsonGenerator.writeArrayFieldStart("columns");
    int i = includedColumns.nextSetBit(0);
    while (i != -1) {
        jsonGenerator.writeStartObject();
        jsonGenerator.writeNumberField("id", i + 1);
        ColumnDefinition columnDefinition = event.getColumnByIndex(i);
        Integer columnType = null;
        if (columnDefinition != null) {
            jsonGenerator.writeStringField("name", columnDefinition.getName());
            columnType = columnDefinition.getType();
            jsonGenerator.writeNumberField("column_type", columnType);
        }
        if (row[i] == null) {
            jsonGenerator.writeNullField("value");
        } else {
            jsonGenerator.writeObjectField("value", MySQLCDCUtils.getWritableObject(columnType, row[i]));
        }
        jsonGenerator.writeEndObject();
        i = includedColumns.nextSetBit(i + 1);
    }
    jsonGenerator.writeEndArray();
}
Also used : ColumnDefinition(org.apache.nifi.cdc.event.ColumnDefinition)

Example 2 with ColumnDefinition

use of org.apache.nifi.cdc.event.ColumnDefinition in project nifi by apache.

the class DeleteRowsWriter method writeRow.

protected void writeRow(DeleteRowsEventInfo event, Serializable[] row, BitSet includedColumns) throws IOException {
    jsonGenerator.writeArrayFieldStart("columns");
    int i = includedColumns.nextSetBit(0);
    while (i != -1) {
        jsonGenerator.writeStartObject();
        jsonGenerator.writeNumberField("id", i + 1);
        ColumnDefinition columnDefinition = event.getColumnByIndex(i);
        Integer columnType = null;
        if (columnDefinition != null) {
            jsonGenerator.writeStringField("name", columnDefinition.getName());
            columnType = columnDefinition.getType();
            jsonGenerator.writeNumberField("column_type", columnType);
        }
        if (row[i] == null) {
            jsonGenerator.writeNullField("value");
        } else {
            jsonGenerator.writeObjectField("value", MySQLCDCUtils.getWritableObject(columnType, row[i]));
        }
        jsonGenerator.writeEndObject();
        i = includedColumns.nextSetBit(i + 1);
    }
    jsonGenerator.writeEndArray();
}
Also used : ColumnDefinition(org.apache.nifi.cdc.event.ColumnDefinition)

Example 3 with ColumnDefinition

use of org.apache.nifi.cdc.event.ColumnDefinition in project nifi by apache.

the class UpdateRowsWriter method writeRow.

protected void writeRow(UpdateRowsEventInfo event, Map.Entry<Serializable[], Serializable[]> row, BitSet includedColumns) throws IOException {
    jsonGenerator.writeArrayFieldStart("columns");
    int i = includedColumns.nextSetBit(0);
    while (i != -1) {
        jsonGenerator.writeStartObject();
        jsonGenerator.writeNumberField("id", i + 1);
        ColumnDefinition columnDefinition = event.getColumnByIndex(i);
        Integer columnType = null;
        if (columnDefinition != null) {
            jsonGenerator.writeStringField("name", columnDefinition.getName());
            columnType = columnDefinition.getType();
            jsonGenerator.writeNumberField("column_type", columnType);
        }
        Serializable[] oldRow = row.getKey();
        Serializable[] newRow = row.getValue();
        if (oldRow[i] == null) {
            jsonGenerator.writeNullField("last_value");
        } else {
            jsonGenerator.writeObjectField("last_value", MySQLCDCUtils.getWritableObject(columnType, oldRow[i]));
        }
        if (newRow[i] == null) {
            jsonGenerator.writeNullField("value");
        } else {
            jsonGenerator.writeObjectField("value", MySQLCDCUtils.getWritableObject(columnType, newRow[i]));
        }
        jsonGenerator.writeEndObject();
        i = includedColumns.nextSetBit(i + 1);
    }
    jsonGenerator.writeEndArray();
}
Also used : Serializable(java.io.Serializable) ColumnDefinition(org.apache.nifi.cdc.event.ColumnDefinition)

Example 4 with ColumnDefinition

use of org.apache.nifi.cdc.event.ColumnDefinition in project nifi by apache.

the class CaptureChangeMySQL method loadTableInfo.

/**
 * Retrieves the column information for the specified database and table. The column information can be used to enrich CDC events coming from the RDBMS.
 *
 * @param key A TableInfoCacheKey reference, which contains the database and table names
 * @return A TableInfo instance with the ColumnDefinitions provided (if retrieved successfully from the database)
 */
protected TableInfo loadTableInfo(TableInfoCacheKey key) throws SQLException {
    TableInfo tableInfo = null;
    if (jdbcConnection != null) {
        try (Statement s = jdbcConnection.createStatement()) {
            s.execute("USE " + key.getDatabaseName());
            ResultSet rs = s.executeQuery("SELECT * FROM " + key.getTableName() + " LIMIT 0");
            ResultSetMetaData rsmd = rs.getMetaData();
            int numCols = rsmd.getColumnCount();
            List<ColumnDefinition> columnDefinitions = new ArrayList<>();
            for (int i = 1; i <= numCols; i++) {
                // Use the column label if it exists, otherwise use the column name. We're not doing aliasing here, but it's better practice.
                String columnLabel = rsmd.getColumnLabel(i);
                columnDefinitions.add(new ColumnDefinition(rsmd.getColumnType(i), columnLabel != null ? columnLabel : rsmd.getColumnName(i)));
            }
            tableInfo = new TableInfo(key.getDatabaseName(), key.getTableName(), key.getTableId(), columnDefinitions);
        }
    }
    return tableInfo;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) TableInfo(org.apache.nifi.cdc.event.TableInfo) ColumnDefinition(org.apache.nifi.cdc.event.ColumnDefinition)

Aggregations

ColumnDefinition (org.apache.nifi.cdc.event.ColumnDefinition)4 Serializable (java.io.Serializable)1 ResultSet (java.sql.ResultSet)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 TableInfo (org.apache.nifi.cdc.event.TableInfo)1