Search in sources :

Example 1 with DecodedSchema

use of org.spf4j.avro.DecodedSchema 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)

Example 2 with DecodedSchema

use of org.spf4j.avro.DecodedSchema in project spf4j by zolyfarkas.

the class CsvDecoder method tryDecodeSchema.

/**
 * Will try to decode the writer schema based on the csv headers, and the reader schema.
 * @param is
 * @param readerSchema
 * @return
 * @throws IOException
 */
public static DecodedSchema tryDecodeSchema(final InputStream is, @Nullable final Schema readerSchema) throws IOException {
    try {
        CsvReader reader = Csv.CSV.reader(new InputStreamReader(is, StandardCharsets.UTF_8));
        Schema record;
        if (readerSchema == null) {
            record = Schema.createRecord("DynCsv", "Infered schema", "org.spf4j.avro", false);
            List<Schema.Field> bfields = new ArrayList<>();
            reader.readRow(cs -> bfields.add(AvroCompatUtils.createField(cs.toString(), Schema.create(Schema.Type.STRING), null, null, false, false, Schema.Field.Order.IGNORE)));
            record.setFields(bfields);
        } else {
            Schema elementType = readerSchema.getElementType();
            List<CharSequence> list = new ArrayList<>(elementType.getFields().size());
            reader.readRow(cs -> list.add(cs.toString()));
            record = Schemas.project(elementType, list);
        }
        Schema arraySchema = Schema.createArray(record);
        return new DecodedSchema(arraySchema, new CsvDecoder(reader, arraySchema));
    } catch (CsvParseException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : CsvReader(org.spf4j.io.csv.CsvReader) DecodedSchema(org.spf4j.avro.DecodedSchema) AvroRuntimeException(org.apache.avro.AvroRuntimeException) InputStreamReader(java.io.InputStreamReader) CsvParseException(org.spf4j.io.csv.CsvParseException) DecodedSchema(org.spf4j.avro.DecodedSchema) Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList)

Aggregations

Schema (org.apache.avro.Schema)2 DecodedSchema (org.spf4j.avro.DecodedSchema)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1 ArrayList (java.util.ArrayList)1 AvroRuntimeException (org.apache.avro.AvroRuntimeException)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 DemoRecordInfo (org.spf4j.demo.avro.DemoRecordInfo)1 CsvParseException (org.spf4j.io.csv.CsvParseException)1 CsvReader (org.spf4j.io.csv.CsvReader)1