use of org.spf4j.io.csv.CsvReader in project spf4j by zolyfarkas.
the class CsvEncoderTest method testCsvDecoder.
@Test
public void testCsvDecoder() throws IOException, CsvParseException {
String str = "RGF0ZSxWYWx1ZQoyMDE5LTAxLTAxLDQuNjA0CjIwMTgtMT" + "AtMDEsNC42MDcKMjAxOC0wNy0wMSw0LjYwOQoyMDE4LTA0LTAxLDQuNjEyCg==";
byte[] input = Base64.getDecoder().decode(str);
Schema array = SchemaBuilder.array().items().record("test").fields().requiredString("Date").requiredString("Value").endRecord();
LOG.debug(new String(input, StandardCharsets.UTF_8));
CsvReader reader = Csv.CSV.readerILEL(new InputStreamReader(new ByteArrayInputStream(input), StandardCharsets.UTF_8));
reader.readRow((c) -> {
});
CsvDecoder decoder = new CsvDecoder(reader, array);
long nrRead = decoder.readArrayStart();
String lastValue = null;
while (nrRead > 0) {
for (int i = 0; i < nrRead; i++) {
LOG.debug(decoder.readString());
lastValue = decoder.readString();
}
nrRead = decoder.arrayNext();
}
Assert.assertEquals("4.612", lastValue);
}
use of org.spf4j.io.csv.CsvReader in project spf4j by zolyfarkas.
the class CsvDecoder method arrayNext.
@Override
public long arrayNext() throws IOException {
parser.advance(Symbol.ITEM_END);
CsvReader.TokenType current = csvReader.current();
if (current == CsvReader.TokenType.START_DOCUMENT) {
throw new IllegalStateException("cannot be at the beginning of " + csvReader);
}
if (current == CsvReader.TokenType.END_ROW) {
try {
current = csvReader.next();
} catch (CsvParseException ex) {
throw new AvroRuntimeException(ex);
}
}
if (current == CsvReader.TokenType.ELEMENT) {
return 1L;
} else {
return 0L;
}
}
use of org.spf4j.io.csv.CsvReader 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);
}
}
use of org.spf4j.io.csv.CsvReader in project spf4j by zolyfarkas.
the class CsvTest method testCsvStream2.
@Test
public void testCsvStream2() throws IOException, CsvParseException {
CsvReader reader = Csv.readerNoBOM(new PushbackReader(new StringReader("bla")));
Assert.assertEquals(TokenType.ELEMENT, reader.next());
Assert.assertEquals("bla", reader.getElement().toString());
Assert.assertEquals(TokenType.END_ROW, reader.next());
Assert.assertEquals(TokenType.END_DOCUMENT, reader.next());
Assert.assertEquals(TokenType.END_DOCUMENT, reader.next());
}
use of org.spf4j.io.csv.CsvReader in project spf4j by zolyfarkas.
the class CsvTest method testCsvStream3.
@Test
public void testCsvStream3() throws IOException, CsvParseException {
CsvReader reader = Csv.readerNoBOM(new PushbackReader(new StringReader("\"bla\"")));
Assert.assertEquals(TokenType.ELEMENT, reader.next());
Assert.assertEquals("bla", reader.getElement().toString());
Assert.assertEquals(TokenType.END_ROW, reader.next());
Assert.assertEquals(TokenType.END_DOCUMENT, reader.next());
Assert.assertEquals(TokenType.END_DOCUMENT, reader.next());
}
Aggregations