Search in sources :

Example 1 with PrimaryKeySchema

use of com.linkedin.databus2.producers.ds.PrimaryKeySchema 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 PrimaryKeySchema

use of com.linkedin.databus2.producers.ds.PrimaryKeySchema 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)

Aggregations

DatabusException (com.linkedin.databus2.core.DatabusException)2 KeyPair (com.linkedin.databus2.producers.ds.KeyPair)2 PrimaryKeySchema (com.linkedin.databus2.producers.ds.PrimaryKeySchema)2 VersionedSchema (com.linkedin.databus2.schemas.VersionedSchema)2 ArrayList (java.util.ArrayList)2 Schema (org.apache.avro.Schema)2 Field (org.apache.avro.Schema.Field)2