Search in sources :

Example 1 with DbusEventAvroDecoder

use of com.linkedin.databus.client.DbusEventAvroDecoder in project databus by linkedin.

the class BootstrapTableReader method init.

public static void init(String[] args) throws Exception {
    parseArgs(args);
    BootstrapSeederMain.Config bsConf = new BootstrapSeederMain.Config();
    ConfigLoader<BootstrapSeederMain.StaticConfig> configLoader = new ConfigLoader<BootstrapSeederMain.StaticConfig>("databus.reader.", bsConf);
    _bsStaticConfig = configLoader.loadConfig(_sBootstrapConfigProps);
    Config qConf = new Config();
    ConfigLoader<StaticConfig> configLoader2 = new ConfigLoader<StaticConfig>("databus.query.", qConf);
    _queryStaticConfig = configLoader2.loadConfig(_sQueryConfigProps);
    SchemaRegistryService schemaRegistry = FileSystemSchemaRegistryService.build(_bsStaticConfig.getSchemaRegistry().getFileSystem());
    LOG.info("Schema = " + schemaRegistry.fetchLatestSchemaBySourceName(_queryStaticConfig.getSourceName()));
    _schema = Schema.parse(schemaRegistry.fetchLatestSchemaBySourceName(_queryStaticConfig.getSourceName()));
    VersionedSchema vs = new VersionedSchema(_schema.getFullName(), (short) 1, _schema, null);
    VersionedSchemaSet schemaSet = new VersionedSchemaSet();
    schemaSet.add(vs);
    _decoder = new DbusEventAvroDecoder(schemaSet);
}
Also used : ConfigLoader(com.linkedin.databus.core.util.ConfigLoader) DbusEventAvroDecoder(com.linkedin.databus.client.DbusEventAvroDecoder) FileSystemSchemaRegistryService(com.linkedin.databus2.schemas.FileSystemSchemaRegistryService) SchemaRegistryService(com.linkedin.databus2.schemas.SchemaRegistryService) VersionedSchemaSet(com.linkedin.databus2.schemas.VersionedSchemaSet) VersionedSchema(com.linkedin.databus2.schemas.VersionedSchema)

Example 2 with DbusEventAvroDecoder

use of com.linkedin.databus.client.DbusEventAvroDecoder in project databus by linkedin.

the class TestInternalMetadata method testGetMetadata_UnhappyPath_EventHasNoMetadata.

/**
 * Verifies that getMetadata() returns null if event has no metadata.
 */
@Test
public void testGetMetadata_UnhappyPath_EventHasNoMetadata() throws Exception {
    LOG.info("starting testGetMetadata_UnhappyPath_EventHasNoMetadata()");
    // build the event without any metadata
    DbusEvent event = createEvent(null);
    // create a metadata schema set, just because we like to
    VersionedSchemaSet metadataSchemaSet = new VersionedSchemaSet();
    metadataSchemaSet.add(SchemaRegistryService.DEFAULT_METADATA_SCHEMA_SOURCE, METADATA_SCHEMA_VERSION, new SchemaId(METADATA_SCHEMA_CHECKSUM), CORRECT_METADATA_SCHEMA, // preserve original string
    true);
    // now create the decoder and attempt to use it to extract and decode the event's metadata
    DbusEventAvroDecoder eventDecoder = createDecoder(metadataSchemaSet);
    try {
        GenericRecord reuse = null;
        GenericRecord decodedMetadata = eventDecoder.getMetadata(event, reuse);
        Assert.assertNull(decodedMetadata, "getMetadata() should have returned null;");
    } catch (Exception ex) {
        Assert.fail("getMetadata() should not have thrown exception: " + ex);
    }
    LOG.info("leaving testGetMetadata_UnhappyPath_EventHasNoMetadata()");
}
Also used : DbusEvent(com.linkedin.databus.core.DbusEvent) DbusEventAvroDecoder(com.linkedin.databus.client.DbusEventAvroDecoder) SchemaId(com.linkedin.databus2.schemas.SchemaId) VersionedSchemaSet(com.linkedin.databus2.schemas.VersionedSchemaSet) GenericRecord(org.apache.avro.generic.GenericRecord) Test(org.testng.annotations.Test)

Example 3 with DbusEventAvroDecoder

use of com.linkedin.databus.client.DbusEventAvroDecoder in project databus by linkedin.

the class GenericRecordDtailPrinter method printEvent.

/**
 * @see com.linkedin.databus2.tools.dtail.DtailPrinter#printEvent(com.linkedin.databus.core.DbusEventInternalReadable, com.linkedin.databus.client.pub.DbusEventDecoder)
 */
@Override
public ConsumerCallbackResult printEvent(DbusEventInternalReadable e, DbusEventDecoder eventDecoder) {
    DbusEventAvroDecoder avroDecoder = (DbusEventAvroDecoder) eventDecoder;
    switch(_metadataOutput) {
        case NONE:
            GenericRecord payload = eventDecoder.getGenericRecord(e, null);
            return payload != null ? printGenericRecord(payload) : ConsumerCallbackResult.SUCCESS;
        case ONLY:
            GenericRecord metadata = avroDecoder.getMetadata(e, null);
            return null != metadata ? printGenericRecord(metadata) : ConsumerCallbackResult.SUCCESS;
        case INCLUDE:
            GenericRecord payload1 = avroDecoder.getGenericRecord(e, null);
            GenericRecord metadata1 = avroDecoder.getMetadata(e, null);
            Schema pschema = Schema.createUnion(Arrays.asList(avroDecoder.getPayloadSchema(e).getSchema(), Schema.create(Type.NULL)));
            Field pfield = new Field("payload", pschema, "payload", null);
            VersionedSchema metaschema = avroDecoder.getMetadataSchema(e);
            Schema mschema = null != metaschema ? Schema.createUnion(Arrays.asList(metaschema.getSchema(), Schema.create(Type.NULL))) : Schema.createUnion(Arrays.asList(Schema.create(Type.INT), Schema.create(Type.NULL)));
            Field mfield = new Field("metadata", mschema, "metadata", null);
            Schema combined = Schema.createRecord(Arrays.asList(pfield, mfield));
            GenericRecord r = new GenericData.Record(combined);
            r.put(0, payload1);
            r.put(1, metadata1);
            return printGenericRecord(r);
        default:
            LOG.error("unknown metadata output mode: " + _metadataOutput);
            return ConsumerCallbackResult.ERROR_FATAL;
    }
}
Also used : Field(org.apache.avro.Schema.Field) DbusEventAvroDecoder(com.linkedin.databus.client.DbusEventAvroDecoder) Schema(org.apache.avro.Schema) VersionedSchema(com.linkedin.databus2.schemas.VersionedSchema) GenericRecord(org.apache.avro.generic.GenericRecord) GenericRecord(org.apache.avro.generic.GenericRecord) VersionedSchema(com.linkedin.databus2.schemas.VersionedSchema)

Example 4 with DbusEventAvroDecoder

use of com.linkedin.databus.client.DbusEventAvroDecoder in project databus by linkedin.

the class TestInternalMetadata method testGetMetadata_UnhappyPath_BadSchema.

/**
 * Verifies that getMetadata() returns null if there's a mismatch between the event's metadata
 * and the metadata schema whose signature/checksum is specified in the event header.
 */
@Test
public void testGetMetadata_UnhappyPath_BadSchema() throws Exception {
    LOG.info("starting testGetMetadata_UnhappyPath_BadSchema()");
    // build the event's metadata and then the event
    DbusEventPart metadataPart = createMetadataPart();
    DbusEvent event = createEvent(metadataPart);
    // create a metadata schema set with a schema that claims to match the event's
    // metadata but doesn't actually
    VersionedSchemaSet metadataSchemaSet = new VersionedSchemaSet();
    metadataSchemaSet.add(SchemaRegistryService.DEFAULT_METADATA_SCHEMA_SOURCE, // METADATA_SCHEMA_VERSION
    metadataPart.getSchemaVersion(), // METADATA_SCHEMA_CHECKSUM
    new SchemaId(metadataPart.getSchemaDigest()), INCORRECT_METADATA_SCHEMA, // preserve original string
    true);
    // now create the decoder and attempt to use it to extract and decode the event's metadata
    DbusEventAvroDecoder eventDecoder = createDecoder(metadataSchemaSet);
    try {
        GenericRecord reuse = null;
        GenericRecord decodedMetadata = eventDecoder.getMetadata(event, reuse);
        Assert.assertNull(decodedMetadata, "getMetadata() should have returned null;");
    } catch (Exception ex) {
        Assert.fail("getMetadata() should not have thrown exception: " + ex);
    }
    LOG.info("leaving testGetMetadata_UnhappyPath_BadSchema()");
}
Also used : DbusEventPart(com.linkedin.databus.core.DbusEventPart) DbusEvent(com.linkedin.databus.core.DbusEvent) DbusEventAvroDecoder(com.linkedin.databus.client.DbusEventAvroDecoder) SchemaId(com.linkedin.databus2.schemas.SchemaId) VersionedSchemaSet(com.linkedin.databus2.schemas.VersionedSchemaSet) GenericRecord(org.apache.avro.generic.GenericRecord) Test(org.testng.annotations.Test)

Example 5 with DbusEventAvroDecoder

use of com.linkedin.databus.client.DbusEventAvroDecoder in project databus by linkedin.

the class LoggingConsumer method logTypedValue.

protected void logTypedValue(DbusEvent e, DbusEventDecoder eventDecoder, RuntimeConfig rtConfig, String phase) {
    if (eventDecoder instanceof DbusEventAvroDecoder) {
        try {
            DbusEventAvroDecoder avroDecoder = (DbusEventAvroDecoder) eventDecoder;
            ByteArrayOutputStream stringOut = new ByteArrayOutputStream();
            stringOut.write(phase.getBytes("UTF-8"));
            avroDecoder.dumpEventValueInJSON(e, stringOut);
            stringOut.flush();
            _log.log(rtConfig.getLogLevel(), stringOut.toString("UTF-8"));
        } catch (IOException ex) {
            _log.warn("typed value serialization error:" + ex.getMessage(), ex);
        } catch (RuntimeException ex) {
            _log.warn("typed value serialization error:" + ex.getMessage(), ex);
        }
    }
}
Also used : DbusEventAvroDecoder(com.linkedin.databus.client.DbusEventAvroDecoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

DbusEventAvroDecoder (com.linkedin.databus.client.DbusEventAvroDecoder)9 VersionedSchemaSet (com.linkedin.databus2.schemas.VersionedSchemaSet)6 GenericRecord (org.apache.avro.generic.GenericRecord)5 DbusEvent (com.linkedin.databus.core.DbusEvent)4 DbusEventPart (com.linkedin.databus.core.DbusEventPart)4 Test (org.testng.annotations.Test)4 SchemaId (com.linkedin.databus2.schemas.SchemaId)3 VersionedSchema (com.linkedin.databus2.schemas.VersionedSchema)3 IOException (java.io.IOException)3 FileSystemSchemaRegistryService (com.linkedin.databus2.schemas.FileSystemSchemaRegistryService)2 SchemaRegistryService (com.linkedin.databus2.schemas.SchemaRegistryService)2 Schema (org.apache.avro.Schema)2 Field (org.apache.avro.Schema.Field)2 ResultSetEntry (com.linkedin.databus.bootstrap.utils.BootstrapAuditTableReader.ResultSetEntry)1 ConfigLoader (com.linkedin.databus.core.util.ConfigLoader)1 OracleTriggerMonitoredSourceInfo (com.linkedin.databus2.producers.db.OracleTriggerMonitoredSourceInfo)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1