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;
}
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);
}
}
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;
}
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();
}
}
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;
}
}
Aggregations