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);
}
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);
}
}
Aggregations