CsvEncoder extends Encoder implements Parser.ActionHandler { public CsvEncoder(final CsvWriter csvWriter, final Schema writerSchema) throws IOException { this.csvWriter = csvWriter; this.parser = new Parser(new JsonGrammarGenerator().generate(writerSchema), this); if (writerSchema.getType() != Schema.Type.ARRAY) { throw new IllegalArgumentException("schema must be an array: " + writerSchema); } if (writerSchema.getElementType().getType() != Schema.Type.RECORD) { throw new IllegalArgumentException("schema array element must be record: " + writerSchema); } this.writerSchema = writerSchema; } CsvEncoder(final CsvWriter csvWriter, final Schema writerSchema); void writeHeader(); @Override void writeNull(); @Override void writeBoolean(final boolean bln); @Override void writeInt(final int i); @Override void writeLong(final long l); @Override void writeFloat(final float f); @Override void writeDouble(final double d); @Override void writeString(final Utf8 utf8); @Override void writeBytes(final ByteBuffer bb); @Override void writeBytes(final byte[] bytes, final int i, final int length); @Override void writeFixed(final byte[] bytes, final int i, final int length); @Override void writeEnum(final int e); @Override void writeArrayStart(); @Override void writeArrayEnd(); @Override void writeMapStart(); @Override void writeMapEnd(); @Override void writeIndex(final int unionIndex); @Override void flush(); @Override @Nullable Symbol doAction(final Symbol symbol, final Symbol symbol1); @Override void setItemCount(final long l); @Override void startItem(); @Override String toString();  }
@Test public void testCsvEncoder() throws IOException, CsvParseException { Schema rowschema = SchemaBuilder.record("CsvRow").fields() .requiredBoolean("boolField") .requiredBytes("bytesField") .requiredInt("intField") .requiredLong("longField") .requiredString("stringField") .requiredDouble("doubleField") .name("recordField").type(PackageInfo.getClassSchema()).noDefault() .nullableString("nullableString", null) .name("enumField").type(Schema.createEnum("myEnum", "", "", Arrays.asList("e1", "e2"))).noDefault() .endRecord(); List<GenericRecord> list = testArray(rowschema); Schema csvSchema = Schema.createArray(rowschema); CharSeparatedValues csv = new CharSeparatedValues(','); StringWriter writeTo = new StringWriter(); CsvEncoder encoder = new CsvEncoder(csv.writer(writeTo), csvSchema); encoder.writeHeader(); DatumWriter writer = new GenericDatumWriter(csvSchema); writer.write(list, encoder); encoder.flush(); LOG.debug("written:", writeTo); StringReader reader = new StringReader(writeTo.toString()); CsvDecoder decoder = new CsvDecoder(csv.reader(reader), csvSchema); decoder.skipHeader(); GenericDatumReader dr = new GenericDatumReader(csvSchema); Object parsed = dr.read(null, decoder); LOG.debug("parser:", parsed); }