Search in sources :

Example 1 with Column

use of com.google.code.or.common.glossary.Column in project databus by linkedin.

the class ORListener method generateAvroEvent.

private void generateAvroEvent(VersionedSchema vs, List<Column> cols, GenericRecord record) throws DatabusException {
    // Get Ordered list of field by dbFieldPosition
    List<Schema.Field> orderedFields = SchemaHelper.getOrderedFieldsByDBFieldPosition(vs);
    // Build Map<AvroFieldType, Columns>
    if (orderedFields.size() != cols.size()) {
        throw new DatabusException("Mismatch in db schema vs avro schema");
    }
    int cnt = 0;
    Map<String, Column> avroFieldCol = new HashMap<String, Column>();
    for (Schema.Field field : orderedFields) {
        avroFieldCol.put(field.name(), cols.get(cnt));
        cnt++;
    }
    for (Schema.Field field : orderedFields) {
        if (field.schema().getType() == Schema.Type.ARRAY) {
            throw new DatabusException("The parser cannot handle ARRAY datatypes. Found in field: " + field);
        } else {
            // The current database field being processed
            // (field is avro field name  and databaseFieldName is oracle field name (one to one mapping)
            String databaseFieldName = SchemaHelper.getMetaField(field, "dbFieldName").toLowerCase();
            _log.debug("databaseFieldName = " + databaseFieldName);
            // Insert the field into the generic record
            insertFieldIntoRecord(avroFieldCol, record, databaseFieldName, field);
        }
    }
    if (_log.isDebugEnabled()) {
        _log.debug("Generic record = " + record);
    }
}
Also used : Field(org.apache.avro.Schema.Field) DatabusException(com.linkedin.databus2.core.DatabusException) LongColumn(com.google.code.or.common.glossary.column.LongColumn) LongLongColumn(com.google.code.or.common.glossary.column.LongLongColumn) TinyColumn(com.google.code.or.common.glossary.column.TinyColumn) Column(com.google.code.or.common.glossary.Column) Int24Column(com.google.code.or.common.glossary.column.Int24Column) NullColumn(com.google.code.or.common.glossary.column.NullColumn) ShortColumn(com.google.code.or.common.glossary.column.ShortColumn) HashMap(java.util.HashMap) Schema(org.apache.avro.Schema) PrimaryKeySchema(com.linkedin.databus2.producers.ds.PrimaryKeySchema) VersionedSchema(com.linkedin.databus2.schemas.VersionedSchema) Field(org.apache.avro.Schema.Field)

Example 2 with Column

use of com.google.code.or.common.glossary.Column in project databus by linkedin.

the class ORListener method generateAvroEvent.

private void generateAvroEvent(Schema schema, List<Column> cols, GenericRecord record) throws DatabusException {
    // Get Ordered list of field by dbFieldPosition
    List<Schema.Field> orderedFields = SchemaHelper.getOrderedFieldsByMetaField(schema, "dbFieldPosition", new Comparator<String>() {

        @Override
        public int compare(String o1, String o2) {
            Integer pos1 = Integer.parseInt(o1);
            Integer pos2 = Integer.parseInt(o2);
            return pos1.compareTo(pos2);
        }
    });
    // Build Map<AvroFieldType, Columns>
    if (orderedFields.size() != cols.size()) {
        throw new DatabusException("Mismatch in db schema vs avro schema");
    }
    int cnt = 0;
    Map<String, Column> avroFieldCol = new HashMap<String, Column>();
    for (Schema.Field field : orderedFields) {
        avroFieldCol.put(field.name(), cols.get(cnt));
        cnt++;
    }
    for (Schema.Field field : orderedFields) {
        if (field.schema().getType() == Schema.Type.ARRAY) {
            throw new DatabusException("The parser cannot handle ARRAY datatypes. Found in field: " + field);
        } else {
            // The current database field being processed
            // (field is avro field name  and databaseFieldName is oracle field name (one to one mapping)
            String databaseFieldName = SchemaHelper.getMetaField(field, "dbFieldName").toLowerCase();
            _log.debug("databaseFieldName = " + databaseFieldName);
            //Insert the field into the generic record
            insertFieldIntoRecord(avroFieldCol, record, databaseFieldName, field);
        }
    }
    if (_log.isDebugEnabled()) {
        _log.debug("Generic record = " + record);
    }
}
Also used : HashMap(java.util.HashMap) Schema(org.apache.avro.Schema) PrimaryKeySchema(com.linkedin.databus2.producers.ds.PrimaryKeySchema) VersionedSchema(com.linkedin.databus2.schemas.VersionedSchema) Field(org.apache.avro.Schema.Field) BigInteger(java.math.BigInteger) Field(org.apache.avro.Schema.Field) DatabusException(com.linkedin.databus2.core.DatabusException) FloatColumn(com.google.code.or.common.glossary.column.FloatColumn) SetColumn(com.google.code.or.common.glossary.column.SetColumn) DatetimeColumn(com.google.code.or.common.glossary.column.DatetimeColumn) EnumColumn(com.google.code.or.common.glossary.column.EnumColumn) TimestampColumn(com.google.code.or.common.glossary.column.TimestampColumn) TimeColumn(com.google.code.or.common.glossary.column.TimeColumn) BitColumn(com.google.code.or.common.glossary.column.BitColumn) LongColumn(com.google.code.or.common.glossary.column.LongColumn) YearColumn(com.google.code.or.common.glossary.column.YearColumn) Datetime2Column(com.google.code.or.common.glossary.column.Datetime2Column) DecimalColumn(com.google.code.or.common.glossary.column.DecimalColumn) DoubleColumn(com.google.code.or.common.glossary.column.DoubleColumn) LongLongColumn(com.google.code.or.common.glossary.column.LongLongColumn) TinyColumn(com.google.code.or.common.glossary.column.TinyColumn) Column(com.google.code.or.common.glossary.Column) StringColumn(com.google.code.or.common.glossary.column.StringColumn) Int24Column(com.google.code.or.common.glossary.column.Int24Column) BlobColumn(com.google.code.or.common.glossary.column.BlobColumn) NullColumn(com.google.code.or.common.glossary.column.NullColumn) ShortColumn(com.google.code.or.common.glossary.column.ShortColumn) DateColumn(com.google.code.or.common.glossary.column.DateColumn)

Example 3 with Column

use of com.google.code.or.common.glossary.Column in project databus by linkedin.

the class ORListener method insertFieldIntoRecord.

/**
 * Given the following :
 * 1. A row data as a map of (dbFieldName, Column) data)
 * 2. A generic record to populate
 * 3. dbFieldName
 * 4. avroFieldName
 *
 * The method locates the Column for the dbFieldName, extracts the data as a Java Object,
 * inserts into the generic record with avroFieldName.name() as the key
 */
private void insertFieldIntoRecord(Map<String, Column> eventFields, GenericRecord record, String dbFieldName, Schema.Field avroField) throws DatabusException {
    String f = avroField.name();
    Column fieldValue = eventFields.get(f);
    boolean isFieldNull = (fieldValue == null);
    Object fieldValueObj = null;
    try {
        if (!isFieldNull)
            fieldValueObj = orToAvroType(fieldValue, avroField);
        else
            fieldValueObj = null;
        record.put(avroField.name(), fieldValueObj);
    } catch (DatabusException e) {
        _log.error("Unable to process field: " + avroField.name());
        throw e;
    }
    return;
}
Also used : DatabusException(com.linkedin.databus2.core.DatabusException) LongColumn(com.google.code.or.common.glossary.column.LongColumn) LongLongColumn(com.google.code.or.common.glossary.column.LongLongColumn) TinyColumn(com.google.code.or.common.glossary.column.TinyColumn) Column(com.google.code.or.common.glossary.Column) Int24Column(com.google.code.or.common.glossary.column.Int24Column) NullColumn(com.google.code.or.common.glossary.column.NullColumn) ShortColumn(com.google.code.or.common.glossary.column.ShortColumn)

Example 4 with Column

use of com.google.code.or.common.glossary.Column 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(vs, cl, gr);
            List<KeyPair> kps = generateKeyPair(gr, vs);
            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);
    }
}
Also used : TableMapEvent(com.google.code.or.binlog.impl.event.TableMapEvent) 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) VersionedSchema(com.linkedin.databus2.schemas.VersionedSchema) DbChangeEntry(com.linkedin.databus2.producers.ds.DbChangeEntry) NoSuchSchemaException(com.linkedin.databus2.schemas.NoSuchSchemaException) DatabusException(com.linkedin.databus2.core.DatabusException) LongColumn(com.google.code.or.common.glossary.column.LongColumn) LongLongColumn(com.google.code.or.common.glossary.column.LongLongColumn) TinyColumn(com.google.code.or.common.glossary.column.TinyColumn) Column(com.google.code.or.common.glossary.Column) Int24Column(com.google.code.or.common.glossary.column.Int24Column) NullColumn(com.google.code.or.common.glossary.column.NullColumn) ShortColumn(com.google.code.or.common.glossary.column.ShortColumn) GenericRecord(org.apache.avro.generic.GenericRecord) Row(com.google.code.or.common.glossary.Row) GenericRecord(org.apache.avro.generic.GenericRecord) DatabusRuntimeException(com.linkedin.databus.core.DatabusRuntimeException)

Aggregations

Column (com.google.code.or.common.glossary.Column)4 Int24Column (com.google.code.or.common.glossary.column.Int24Column)4 LongColumn (com.google.code.or.common.glossary.column.LongColumn)4 LongLongColumn (com.google.code.or.common.glossary.column.LongLongColumn)4 NullColumn (com.google.code.or.common.glossary.column.NullColumn)4 ShortColumn (com.google.code.or.common.glossary.column.ShortColumn)4 TinyColumn (com.google.code.or.common.glossary.column.TinyColumn)4 DatabusException (com.linkedin.databus2.core.DatabusException)4 PrimaryKeySchema (com.linkedin.databus2.producers.ds.PrimaryKeySchema)3 VersionedSchema (com.linkedin.databus2.schemas.VersionedSchema)3 Schema (org.apache.avro.Schema)3 HashMap (java.util.HashMap)2 Field (org.apache.avro.Schema.Field)2 TableMapEvent (com.google.code.or.binlog.impl.event.TableMapEvent)1 Row (com.google.code.or.common.glossary.Row)1 BitColumn (com.google.code.or.common.glossary.column.BitColumn)1 BlobColumn (com.google.code.or.common.glossary.column.BlobColumn)1 DateColumn (com.google.code.or.common.glossary.column.DateColumn)1 Datetime2Column (com.google.code.or.common.glossary.column.Datetime2Column)1 DatetimeColumn (com.google.code.or.common.glossary.column.DatetimeColumn)1