Search in sources :

Example 1 with AppendableOutputStream

use of org.spf4j.io.AppendableOutputStream in project spf4j by zolyfarkas.

the class GenericRecordAppender method append.

@Override
public void append(final GenericRecord object, final Appendable appendTo) throws IOException {
    StringBuilder sb = TMP.get();
    sb.setLength(0);
    try (AppendableOutputStream bos = new AppendableOutputStream(appendTo, Charsets.UTF_8)) {
        final Schema schema = object.getSchema();
        GenericDatumWriter<GenericRecord> writer = new GenericDatumWriter<>(schema);
        JsonEncoder jsonEncoder = SpecificRecordAppender.EF.jsonEncoder(schema, bos);
        writer.write(object, jsonEncoder);
        jsonEncoder.flush();
    } catch (IOException | RuntimeException ex) {
        writeSerializationError(object, sb, ex);
    }
    appendTo.append(sb);
}
Also used : JsonEncoder(org.apache.avro.io.JsonEncoder) Schema(org.apache.avro.Schema) AppendableOutputStream(org.spf4j.io.AppendableOutputStream) GenericDatumWriter(org.apache.avro.generic.GenericDatumWriter) IOException(java.io.IOException) GenericRecord(org.apache.avro.generic.GenericRecord)

Example 2 with AppendableOutputStream

use of org.spf4j.io.AppendableOutputStream in project spf4j by zolyfarkas.

the class SpecificRecordAppender method append.

@Override
public void append(final SpecificRecord object, final Appendable appendTo) throws IOException {
    StringBuilder sb = TMP.get();
    sb.setLength(0);
    try (AppendableOutputStream bos = new AppendableOutputStream(sb, Charsets.UTF_8)) {
        final Schema schema = object.getSchema();
        SpecificDatumWriter<SpecificRecord> writer = new SpecificDatumWriter<>(schema);
        JsonEncoder jsonEncoder = EF.jsonEncoder(schema, bos);
        writer.write(object, jsonEncoder);
        jsonEncoder.flush();
    } catch (IOException | RuntimeException ex) {
        writeSerializationError(object, sb, ex);
    }
    appendTo.append(sb);
}
Also used : JsonEncoder(org.apache.avro.io.JsonEncoder) SpecificRecord(org.apache.avro.specific.SpecificRecord) Schema(org.apache.avro.Schema) AppendableOutputStream(org.spf4j.io.AppendableOutputStream) IOException(java.io.IOException) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter)

Example 3 with AppendableOutputStream

use of org.spf4j.io.AppendableOutputStream in project spf4j by zolyfarkas.

the class SpecificRecordAppender method writeSerializationError.

@SuppressFBWarnings("ITC_INHERITANCE_TYPE_CHECKING")
static void writeSerializationError(final Object object, final StringBuilder sb, final Exception ex) throws IOException {
    if (STRICT_SERIALIZATION) {
        if (ex instanceof IOException) {
            throw (IOException) ex;
        } else if (ex instanceof RuntimeException) {
            throw (RuntimeException) ex;
        } else {
            throw new IllegalStateException(ex);
        }
    }
    sb.setLength(0);
    sb.append("{\"SerializationError\":\n");
    try (AppendableOutputStream bos = new AppendableOutputStream(sb, Charsets.UTF_8)) {
        JThrowable at = Converters.convert(ex);
        Schema schema = at.getSchema();
        SpecificDatumWriter<SpecificRecord> writer = new SpecificDatumWriter<>(schema);
        JsonEncoder jsonEncoder = EF.jsonEncoder(schema, bos, true);
        writer.write(at, jsonEncoder);
        jsonEncoder.flush();
    }
    sb.append(",\n");
    sb.append("\"ObjectAsString\":\n\"");
    EscapeJsonStringAppendableWrapper escaper = new EscapeJsonStringAppendableWrapper(sb);
    escaper.append(object.toString());
    sb.append("\"}");
}
Also used : JThrowable(org.spf4j.base.avro.JThrowable) JsonEncoder(org.apache.avro.io.JsonEncoder) SpecificRecord(org.apache.avro.specific.SpecificRecord) Schema(org.apache.avro.Schema) AppendableOutputStream(org.spf4j.io.AppendableOutputStream) EscapeJsonStringAppendableWrapper(org.spf4j.base.EscapeJsonStringAppendableWrapper) IOException(java.io.IOException) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

IOException (java.io.IOException)3 Schema (org.apache.avro.Schema)3 JsonEncoder (org.apache.avro.io.JsonEncoder)3 AppendableOutputStream (org.spf4j.io.AppendableOutputStream)3 SpecificDatumWriter (org.apache.avro.specific.SpecificDatumWriter)2 SpecificRecord (org.apache.avro.specific.SpecificRecord)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 GenericDatumWriter (org.apache.avro.generic.GenericDatumWriter)1 GenericRecord (org.apache.avro.generic.GenericRecord)1 EscapeJsonStringAppendableWrapper (org.spf4j.base.EscapeJsonStringAppendableWrapper)1 JThrowable (org.spf4j.base.avro.JThrowable)1