Search in sources :

Example 1 with DemoRecordInfo

use of org.spf4j.demo.avro.DemoRecordInfo in project spf4j by zolyfarkas.

the class CsvEncoderTest method testEncodeDecodeSpecific.

@Test
public void testEncodeDecodeSpecific() throws IOException, CsvParseException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    Schema aSchema = Schema.createArray(DemoRecordInfo.SCHEMA$);
    CsvEncoder csvEncoder = new CsvEncoder(Csv.CSV.writer(new OutputStreamWriter(bos, StandardCharsets.UTF_8)), aSchema);
    csvEncoder.writeHeader();
    DatumWriter writer = new GenericDatumWriter(aSchema);
    List<DemoRecordInfo> testRecords = testRecords();
    writer.write(testRecords, csvEncoder);
    csvEncoder.flush();
    LOG.debug("serialized", bos.toString("UTF8"));
    ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
    DecodedSchema ds = CsvDecoder.tryDecodeSchema(bis, aSchema);
    Decoder decoder = ds.getDecoder();
    SpecificDatumReader reader = new SpecificDatumReader(ds.getSchema());
    Object read = reader.read(reader, decoder);
    LOG.debug("deserialized", read);
    Assert.assertEquals(testRecords, read);
}
Also used : DemoRecordInfo(org.spf4j.demo.avro.DemoRecordInfo) DecodedSchema(org.spf4j.avro.DecodedSchema) DecodedSchema(org.spf4j.avro.DecodedSchema) Schema(org.apache.avro.Schema) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GenericDatumWriter(org.apache.avro.generic.GenericDatumWriter) Decoder(org.apache.avro.io.Decoder) GenericDatumWriter(org.apache.avro.generic.GenericDatumWriter) DatumWriter(org.apache.avro.io.DatumWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) OutputStreamWriter(java.io.OutputStreamWriter) SpecificDatumReader(org.apache.avro.specific.SpecificDatumReader) Test(org.junit.Test)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Schema (org.apache.avro.Schema)1 GenericDatumWriter (org.apache.avro.generic.GenericDatumWriter)1 DatumWriter (org.apache.avro.io.DatumWriter)1 Decoder (org.apache.avro.io.Decoder)1 SpecificDatumReader (org.apache.avro.specific.SpecificDatumReader)1 Test (org.junit.Test)1 DecodedSchema (org.spf4j.avro.DecodedSchema)1 DemoRecordInfo (org.spf4j.demo.avro.DemoRecordInfo)1