use of com.linkedin.databus2.producers.ds.DbChangeEntry 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.producers.ds.DbChangeEntry in project databus by linkedin.
the class OpenReplicatorAvroEventFactory method createAndAppendEvent.
public int createAndAppendEvent(DbChangeEntry changeEntry, DbusEventBufferAppendable eventBuffer, boolean enableTracing, DbusEventsStatisticsCollector dbusEventsStatisticsCollector) throws EventCreationException, UnsupportedKeyException, DatabusException {
Object keyObj = obtainKey(changeEntry);
//Construct the Databus Event key, determine the key type and construct the key
DbusEventKey eventKey = new DbusEventKey(keyObj);
short lPartitionId = _partitionFunction.getPartition(eventKey);
//Get the md5 for the schema
SchemaId schemaId = SchemaId.createWithMd5(changeEntry.getSchema());
byte[] payload = serializeEvent(changeEntry.getRecord());
DbusEventInfo eventInfo = new DbusEventInfo(changeEntry.getOpCode(), changeEntry.getScn(), (short) _pSourceId, lPartitionId, changeEntry.getTimestampInNanos(), (short) _sourceId, schemaId.getByteArray(), payload, enableTracing, false);
boolean success = eventBuffer.appendEvent(eventKey, eventInfo, dbusEventsStatisticsCollector);
return success ? payload.length : -1;
}
use of com.linkedin.databus2.producers.ds.DbChangeEntry in project databus by linkedin.
the class ORListener method frameAvroRecord.
private void frameAvroRecord(long tableId, BinlogEventV4Header bh, List<Row> rl, final DbusOpcode doc) {
try {
final long timestampInNanos = bh.getTimestamp() * 1000000L;
final long scn = scn(_currFileNum, (int) bh.getPosition());
final boolean isReplicated = false;
final TableMapEvent tme = _tableMapEvents.get(tableId);
String tableName = tme.getDatabaseName().toString().toLowerCase() + "." + tme.getTableName().toString().toLowerCase();
VersionedSchema vs = _schemaRegistryService.fetchLatestVersionedSchemaBySourceName(_tableUriToSrcNameMap.get(tableName));
Schema schema = vs.getSchema();
if (_log.isDebugEnabled())
_log.debug("File Number :" + _currFileNum + ", Position :" + (int) bh.getPosition() + ", SCN =" + scn);
for (Row r : rl) {
List<Column> cl = r.getColumns();
GenericRecord gr = new GenericData.Record(schema);
generateAvroEvent(schema, cl, gr);
List<KeyPair> kps = generateKeyPair(cl, schema);
DbChangeEntry db = new DbChangeEntry(scn, timestampInNanos, gr, doc, isReplicated, schema, kps);
_transaction.getPerSourceTransaction(_tableUriToSrcIdMap.get(tableName)).mergeDbChangeEntrySet(db);
}
} catch (NoSuchSchemaException ne) {
throw new DatabusRuntimeException(ne);
} catch (DatabusException de) {
throw new DatabusRuntimeException(de);
}
}
Aggregations