Search in sources :

Example 16 with SchemaId

use of com.linkedin.databus2.schemas.SchemaId in project databus by linkedin.

the class DbusEventAvroDecoder method dumpEventValueInJSON.

public void dumpEventValueInJSON(DbusEvent e, OutputStream out) {
    byte[] md5 = new byte[16];
    e.schemaId(md5);
    SchemaId schemaId = new SchemaId(md5);
    VersionedSchema sourceSchema = _schemaSet.getById(schemaId);
    ByteBuffer valueBuffer = e.value();
    byte[] valueBytes = new byte[valueBuffer.remaining()];
    valueBuffer.get(valueBytes);
    try {
        Schema schema = sourceSchema.getSchema();
        DatumReader<Object> reader = new GenericDatumReader<Object>(schema);
        binDecoder.set(DecoderFactory.defaultFactory().createBinaryDecoder(valueBytes, binDecoder.get()));
        Object datum = reader.read(null, binDecoder.get());
        DatumWriter<Object> writer = new GenericDatumWriter<Object>(schema);
        JsonGenerator g = new JsonFactory().createJsonGenerator(out, JsonEncoding.UTF8);
        // write the src ID
        g.writeStartObject();
        g.writeFieldName(SRC_ID_FIELD_NAME);
        g.writeNumber(e.getSourceId());
        g.writeFieldName(OPCODE_FIELD_NAME);
        g.writeString(e.getOpcode().toString());
        g.writeFieldName("partId");
        g.writeNumber(Integer.valueOf(e.getPartitionId()));
        g.writeFieldName(VALUE_FIELD_NAME);
        writer.write(datum, new JsonEncoder(schema, g));
        g.writeEndObject();
        g.writeEndObject();
        try {
            g.writeEndObject();
        } catch (JsonGenerationException e_json) {
        // ignore the error as some how avro JsonEncoder may some times missing two }
        }
        g.flush();
    } catch (IOException e1) {
        LOG.error("event value serialization error; event = " + e, e1);
    }
}
Also used : JsonEncoder(org.apache.avro.io.JsonEncoder) GenericDatumReader(org.apache.avro.generic.GenericDatumReader) Schema(org.apache.avro.Schema) VersionedSchema(com.linkedin.databus2.schemas.VersionedSchema) JsonFactory(org.codehaus.jackson.JsonFactory) GenericDatumWriter(org.apache.avro.generic.GenericDatumWriter) IOException(java.io.IOException) VersionedSchema(com.linkedin.databus2.schemas.VersionedSchema) ByteBuffer(java.nio.ByteBuffer) SchemaId(com.linkedin.databus2.schemas.SchemaId) JsonGenerator(org.codehaus.jackson.JsonGenerator) JsonGenerationException(org.codehaus.jackson.JsonGenerationException)

Example 17 with SchemaId

use of com.linkedin.databus2.schemas.SchemaId in project databus by linkedin.

the class TestGenericDispatcher method initBufferWithEvents.

private void initBufferWithEvents(DbusEventBuffer eventsBuf, long keyBase, int numEvents, short srcId, Hashtable<Long, AtomicInteger> keyCounts, Hashtable<Short, AtomicInteger> srcidCounts) {
    String schemaStr = null;
    switch(srcId) {
        case 1:
            schemaStr = SOURCE1_SCHEMA_STR;
            break;
        case 2:
            schemaStr = SOURCE2_SCHEMA_STR;
            break;
        case 3:
            schemaStr = SOURCE3_SCHEMA_STR;
            break;
        default:
            break;
    }
    SchemaId schemaId = schemaStr != null ? SchemaId.createWithMd5(schemaStr) : null;
    byte[] schemaBytes = schemaId != null ? schemaId.getByteArray() : new byte[16];
    initBufferWithEvents(eventsBuf, keyBase, numEvents, srcId, schemaBytes, keyCounts, srcidCounts);
}
Also used : SchemaId(com.linkedin.databus2.schemas.SchemaId)

Aggregations

SchemaId (com.linkedin.databus2.schemas.SchemaId)14 VersionedSchema (com.linkedin.databus2.schemas.VersionedSchema)6 VersionedSchemaSet (com.linkedin.databus2.schemas.VersionedSchemaSet)5 Test (org.testng.annotations.Test)5 IOException (java.io.IOException)4 ByteBuffer (java.nio.ByteBuffer)4 GenericRecord (org.apache.avro.generic.GenericRecord)4 DbusEventAvroDecoder (com.linkedin.databus.client.DbusEventAvroDecoder)3 DbusEvent (com.linkedin.databus.core.DbusEvent)3 DbusEventInfo (com.linkedin.databus.core.DbusEventInfo)3 DbusEventKey (com.linkedin.databus.core.DbusEventKey)3 DatabusException (com.linkedin.databus2.core.DatabusException)3 RegisterResponseMetadataEntry (com.linkedin.databus2.core.container.request.RegisterResponseMetadataEntry)3 HashMap (java.util.HashMap)3 DatabusRuntimeException (com.linkedin.databus.core.DatabusRuntimeException)2 DbusEventPart (com.linkedin.databus.core.DbusEventPart)2 IdNamePair (com.linkedin.databus.core.util.IdNamePair)2 RegisterResponseEntry (com.linkedin.databus2.core.container.request.RegisterResponseEntry)2 DatabusSourcesConnection (com.linkedin.databus.client.DatabusSourcesConnection)1 SCN (com.linkedin.databus.client.pub.SCN)1