Search in sources :

Example 6 with JsonEncoder

use of org.apache.avro.io.JsonEncoder in project databus by linkedin.

the class ContainerStatsRequestProcessor method processInboundTrafficTotalStats.

private void processInboundTrafficTotalStats(DatabusRequest request) throws IOException {
    ContainerTrafficTotalStatsMBean inboundTrafficTotalStatsMBean = _containerStatsCollector.getInboundTrafficTotalStats();
    if (null == inboundTrafficTotalStatsMBean)
        return;
    //String json = inboundTrafficTotalStatsMBean.toJson();
    JsonEncoder jsonEncoder = inboundTrafficTotalStatsMBean.createJsonEncoder(Channels.newOutputStream(request.getResponseContent()));
    inboundTrafficTotalStatsMBean.toJson(jsonEncoder, null);
    if (request.getRequestType() == HttpMethod.PUT || request.getRequestType() == HttpMethod.POST) {
        enableOrResetStatsMBean(inboundTrafficTotalStatsMBean, request);
    }
}
Also used : ContainerTrafficTotalStatsMBean(com.linkedin.databus2.core.container.monitoring.mbean.ContainerTrafficTotalStatsMBean) JsonEncoder(org.apache.avro.io.JsonEncoder)

Example 7 with JsonEncoder

use of org.apache.avro.io.JsonEncoder 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 8 with JsonEncoder

use of org.apache.avro.io.JsonEncoder in project databus by linkedin.

the class AbstractMonitoringMBean method toJson.

@Override
public String toJson() {
    ByteArrayOutputStream out = new ByteArrayOutputStream(10000);
    try {
        JsonEncoder jsonEncoder = createJsonEncoder(out);
        toJson(jsonEncoder, null);
    } catch (IOException ioe) {
        LOG.error("JSON serialization error", ioe);
        out.reset();
    }
    return out.toString();
}
Also used : JsonEncoder(org.apache.avro.io.JsonEncoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

JsonEncoder (org.apache.avro.io.JsonEncoder)8 IOException (java.io.IOException)3 GenericDatumWriter (org.apache.avro.generic.GenericDatumWriter)3 GenericRecord (org.apache.avro.generic.GenericRecord)3 ContainerTrafficTotalStatsMBean (com.linkedin.databus2.core.container.monitoring.mbean.ContainerTrafficTotalStatsMBean)2 JsonFactory (org.codehaus.jackson.JsonFactory)2 JsonGenerator (org.codehaus.jackson.JsonGenerator)2 ConsumerCallbackResult (com.linkedin.databus.client.pub.ConsumerCallbackResult)1 ContainerTrafficTotalStats (com.linkedin.databus2.core.container.monitoring.mbean.ContainerTrafficTotalStats)1 SchemaId (com.linkedin.databus2.schemas.SchemaId)1 VersionedSchema (com.linkedin.databus2.schemas.VersionedSchema)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 ByteBuffer (java.nio.ByteBuffer)1 Schema (org.apache.avro.Schema)1 GenericDatumReader (org.apache.avro.generic.GenericDatumReader)1 BinaryEncoder (org.apache.avro.io.BinaryEncoder)1 Encoder (org.apache.avro.io.Encoder)1 JsonGenerationException (org.codehaus.jackson.JsonGenerationException)1