Search in sources :

Example 1 with KeyPair

use of com.linkedin.databus2.ggParser.XmlStateMachine.ColumnsState.KeyPair in project databus by linkedin.

the class ORListener method generateKeyPair.

private List<KeyPair> generateKeyPair(List<Column> cl, Schema schema) throws DatabusException {
    Object o = null;
    Schema.Type st = null;
    // Build PrimaryKeySchema
    String pkFieldName = SchemaHelper.getMetaField(schema, "pk");
    if (pkFieldName == null) {
        throw new DatabusException("No primary key specified in the schema");
    }
    PrimaryKeySchema pkSchema = new PrimaryKeySchema(pkFieldName);
    List<Schema.Field> fields = schema.getFields();
    List<KeyPair> kpl = new ArrayList<KeyPair>();
    int cnt = 0;
    for (Schema.Field field : fields) {
        if (pkSchema.isPartOfPrimaryKey(field)) {
            o = cl.get(cnt).getValue();
            st = field.schema().getType();
            KeyPair kp = new KeyPair(o, st);
            kpl.add(kp);
        }
        cnt++;
    }
    return kpl;
}
Also used : KeyPair(com.linkedin.databus2.producers.ds.KeyPair) Schema(org.apache.avro.Schema) PrimaryKeySchema(com.linkedin.databus2.producers.ds.PrimaryKeySchema) VersionedSchema(com.linkedin.databus2.schemas.VersionedSchema) ArrayList(java.util.ArrayList) Field(org.apache.avro.Schema.Field) Field(org.apache.avro.Schema.Field) DatabusException(com.linkedin.databus2.core.DatabusException) PrimaryKeySchema(com.linkedin.databus2.producers.ds.PrimaryKeySchema)

Example 2 with KeyPair

use of com.linkedin.databus2.ggParser.XmlStateMachine.ColumnsState.KeyPair in project databus by linkedin.

the class ColumnsState method constructPkeys.

/**
 * Constructs the primary key pair and stores it in the current state
 * @param eventFields Map containing dbFieldName => fieldValue
 * @param pkFieldName The name of primaryKey field (comma seperated list). e.g., if the schema has pk=id, member_id. Then, pkFieldName is id, member_id.
 *                    This is used only for logging purpose
 * @param pk The primary key object stored as an class object
 * @param field The current field being processed(Avro)
 * @param databaseFieldName Field being processed(oracle name)
 * @param fieldValueObj Value of the current field being processed
 * @throws DatabusException
 */
private void constructPkeys(HashMap<String, ColumnState.EventField> eventFields, String pkFieldName, PrimaryKey pk, Schema.Field field, String databaseFieldName, Object fieldValueObj) throws DatabusException {
    if (eventFields.get(databaseFieldName).isKey()) {
        if (!pk.isPartOfPrimaryKey(field))
            throw new DatabusException("The primary key is not as expected. Expected: " + pkFieldName + " found from xml: " + field.name());
        if (fieldValueObj == null)
            throw new DatabusException("Unable to find the value of the object");
        Schema.Type pkFieldType = SchemaHelper.unwindUnionSchema(field).getType();
        KeyPair pair = new KeyPair(fieldValueObj, pkFieldType);
        _keyPairs.add(pair);
    }
}
Also used : DatabusException(com.linkedin.databus2.core.DatabusException) Schema(org.apache.avro.Schema)

Example 3 with KeyPair

use of com.linkedin.databus2.ggParser.XmlStateMachine.ColumnsState.KeyPair in project databus by linkedin.

the class ORListener method generateKeyPair.

private List<KeyPair> generateKeyPair(GenericRecord gr, VersionedSchema versionedSchema) throws DatabusException {
    Object o = null;
    Schema.Type st = null;
    List<Field> pkFieldList = versionedSchema.getPkFieldList();
    if (pkFieldList.isEmpty()) {
        String pkFieldName = SchemaHelper.getMetaField(versionedSchema.getSchema(), "pk");
        if (pkFieldName == null) {
            throw new DatabusException("No primary key specified in the schema");
        }
        PrimaryKeySchema pkSchema = new PrimaryKeySchema(pkFieldName);
        List<Schema.Field> fields = versionedSchema.getSchema().getFields();
        for (int i = 0; i < fields.size(); i++) {
            Schema.Field field = fields.get(i);
            if (pkSchema.isPartOfPrimaryKey(field)) {
                pkFieldList.add(field);
            }
        }
    }
    List<KeyPair> kpl = new ArrayList<KeyPair>();
    for (Field field : pkFieldList) {
        o = gr.get(field.name());
        st = field.schema().getType();
        KeyPair kp = new KeyPair(o, st);
        kpl.add(kp);
    }
    if (kpl == null || kpl.isEmpty()) {
        String pkFieldName = SchemaHelper.getMetaField(versionedSchema.getSchema(), "pk");
        StringBuilder sb = new StringBuilder();
        for (Schema.Field f : versionedSchema.getSchema().getFields()) {
            sb.append(f.name()).append(",");
        }
        throw new DatabusException("pk is assigned to " + pkFieldName + " but fieldList is " + sb.toString());
    }
    return kpl;
}
Also used : KeyPair(com.linkedin.databus2.producers.ds.KeyPair) Schema(org.apache.avro.Schema) PrimaryKeySchema(com.linkedin.databus2.producers.ds.PrimaryKeySchema) VersionedSchema(com.linkedin.databus2.schemas.VersionedSchema) ArrayList(java.util.ArrayList) Field(org.apache.avro.Schema.Field) Field(org.apache.avro.Schema.Field) DatabusException(com.linkedin.databus2.core.DatabusException) PrimaryKeySchema(com.linkedin.databus2.producers.ds.PrimaryKeySchema)

Example 4 with KeyPair

use of com.linkedin.databus2.ggParser.XmlStateMachine.ColumnsState.KeyPair in project databus by linkedin.

the class OpenReplicatorAvroEventFactory method obtainKey.

/**
 * Given a DBImage, returns the key
 * If it is a single key, it returns the object if it is LONG /INT / STRING
 * For compound key, it casts the fields as String, delimits the fields and returns the appended string
 * @param dbChangeEntry The post-image of the event
 * @return Actual key object
 * @throws DatabusException
 */
private Object obtainKey(DbChangeEntry dbChangeEntry) throws DatabusException {
    if (null == dbChangeEntry) {
        throw new DatabusException("DBUpdateImage is null");
    }
    List<KeyPair> pairs = dbChangeEntry.getPkeys();
    if (null == pairs || pairs.size() == 0) {
        throw new DatabusException("There do not seem to be any keys");
    }
    if (pairs.size() == 1) {
        Object key = pairs.get(0).getKey();
        Schema.Type pKeyType = pairs.get(0).getKeyType();
        Object keyObj = null;
        if (pKeyType == Schema.Type.INT) {
            if (key instanceof Integer) {
                keyObj = key;
            } else {
                throw new DatabusException("Schema.Type does not match actual key type (INT) " + key.getClass().getName());
            }
        } else if (pKeyType == Schema.Type.LONG) {
            if (key instanceof Long) {
                keyObj = key;
            } else {
                throw new DatabusException("Schema.Type does not match actual key type (LONG) " + key.getClass().getName());
            }
            keyObj = key;
        } else {
            keyObj = key;
        }
        return keyObj;
    } else {
        // Treat multiple keys as a separate case to avoid unnecessary casts
        Iterator<KeyPair> li = pairs.iterator();
        StringBuilder compositeKey = new StringBuilder();
        while (li.hasNext()) {
            KeyPair kp = li.next();
            Schema.Type pKeyType = kp.getKeyType();
            Object key = kp.getKey();
            if (pKeyType == Schema.Type.INT) {
                if (key instanceof Integer)
                    compositeKey.append(kp.getKey().toString());
                else
                    throw new DatabusException("Schema.Type does not match actual key type (INT) " + key.getClass().getName());
            } else if (pKeyType == Schema.Type.LONG) {
                if (key instanceof Long)
                    compositeKey.append(key.toString());
                else
                    throw new DatabusException("Schema.Type does not match actual key type (LONG) " + key.getClass().getName());
            } else {
                compositeKey.append(key);
            }
            if (li.hasNext()) {
                // Add the delimiter for all keys except the last key
                compositeKey.append(DbusConstants.COMPOUND_KEY_DELIMITER);
            }
        }
        return compositeKey.toString();
    }
}
Also used : KeyPair(com.linkedin.databus2.producers.ds.KeyPair) DatabusException(com.linkedin.databus2.core.DatabusException) PrimaryKeySchema(com.linkedin.databus2.producers.ds.PrimaryKeySchema) Schema(org.apache.avro.Schema)

Example 5 with KeyPair

use of com.linkedin.databus2.ggParser.XmlStateMachine.ColumnsState.KeyPair in project databus by linkedin.

the class GoldenGateEventProducer method obtainKey.

/**
 * Given a DBImage, returns the key
 * If it is a single key, it returns the object if it is LONG /INT / STRING
 * For compound key, it casts the fields as String, delimits the fields and returns the appended string
 * @param dbUpdate The post-image of the event
 * @return Actual key object
 * @throws DatabusException
 */
protected static Object obtainKey(DbUpdateState.DBUpdateImage dbUpdate) throws DatabusException {
    if (null == dbUpdate) {
        throw new DatabusException("DBUpdateImage is null");
    }
    List<KeyPair> pairs = dbUpdate.getKeyPairs();
    if (null == pairs || pairs.size() == 0) {
        throw new DatabusException("There do not seem to be any keys");
    }
    if (pairs.size() == 1) {
        Object key = dbUpdate.getKeyPairs().get(0).getKey();
        Schema.Type pKeyType = dbUpdate.getKeyPairs().get(0).getKeyType();
        Object keyObj = null;
        if (pKeyType == Schema.Type.INT) {
            if (key instanceof Integer) {
                keyObj = key;
            } else {
                throw new DatabusException("Schema.Type does not match actual key type (INT) " + key.getClass().getName());
            }
        } else if (pKeyType == Schema.Type.LONG) {
            if (key instanceof Long) {
                keyObj = key;
            } else {
                throw new DatabusException("Schema.Type does not match actual key type (LONG) " + key.getClass().getName());
            }
            keyObj = key;
        } else {
            keyObj = key;
        }
        return keyObj;
    } else {
        // Treat multiple keys as a separate case to avoid unnecessary casts
        Iterator<KeyPair> li = pairs.iterator();
        String compositeKey = "";
        while (li.hasNext()) {
            KeyPair kp = li.next();
            Schema.Type pKeyType = kp.getKeyType();
            Object key = kp.getKey();
            if (pKeyType == Schema.Type.INT) {
                if (key instanceof Integer)
                    compositeKey += kp.getKey().toString();
                else
                    throw new DatabusException("Schema.Type does not match actual key type (INT) " + key.getClass().getName());
            } else if (pKeyType == Schema.Type.LONG) {
                if (key instanceof Long)
                    compositeKey += key.toString();
                else
                    throw new DatabusException("Schema.Type does not match actual key type (LONG) " + key.getClass().getName());
            } else {
                compositeKey += key;
            }
            if (li.hasNext()) {
                // Add the delimiter for all keys except the last key
                compositeKey += DbusConstants.COMPOUND_KEY_DELIMITER;
            }
        }
        return compositeKey;
    }
}
Also used : KeyPair(com.linkedin.databus2.ggParser.XmlStateMachine.ColumnsState.KeyPair) DatabusException(com.linkedin.databus2.core.DatabusException) Schema(org.apache.avro.Schema) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Aggregations

DatabusException (com.linkedin.databus2.core.DatabusException)6 Schema (org.apache.avro.Schema)6 KeyPair (com.linkedin.databus2.producers.ds.KeyPair)4 PrimaryKeySchema (com.linkedin.databus2.producers.ds.PrimaryKeySchema)4 VersionedSchema (com.linkedin.databus2.schemas.VersionedSchema)3 ArrayList (java.util.ArrayList)2 Field (org.apache.avro.Schema.Field)2 TableMapEvent (com.google.code.or.binlog.impl.event.TableMapEvent)1 Column (com.google.code.or.common.glossary.Column)1 Row (com.google.code.or.common.glossary.Row)1 Int24Column (com.google.code.or.common.glossary.column.Int24Column)1 LongColumn (com.google.code.or.common.glossary.column.LongColumn)1 LongLongColumn (com.google.code.or.common.glossary.column.LongLongColumn)1 NullColumn (com.google.code.or.common.glossary.column.NullColumn)1 ShortColumn (com.google.code.or.common.glossary.column.ShortColumn)1 TinyColumn (com.google.code.or.common.glossary.column.TinyColumn)1 DatabusRuntimeException (com.linkedin.databus.core.DatabusRuntimeException)1 KeyPair (com.linkedin.databus2.ggParser.XmlStateMachine.ColumnsState.KeyPair)1 DbChangeEntry (com.linkedin.databus2.producers.ds.DbChangeEntry)1 NoSuchSchemaException (com.linkedin.databus2.schemas.NoSuchSchemaException)1