Search in sources :

Example 6 with CsvReader

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);
}
Also used : CsvReader(org.spf4j.io.csv.CsvReader) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) DecodedSchema(org.spf4j.avro.DecodedSchema) Schema(org.apache.avro.Schema) Test(org.junit.Test)

Example 7 with CsvReader

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;
    }
}
Also used : CsvReader(org.spf4j.io.csv.CsvReader) CsvParseException(org.spf4j.io.csv.CsvParseException) AvroRuntimeException(org.apache.avro.AvroRuntimeException)

Example 8 with CsvReader

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

Example 9 with CsvReader

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());
}
Also used : CsvReader(org.spf4j.io.csv.CsvReader) StringReader(java.io.StringReader) Test(org.junit.Test)

Example 10 with CsvReader

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());
}
Also used : CsvReader(org.spf4j.io.csv.CsvReader) StringReader(java.io.StringReader) Test(org.junit.Test)

Aggregations

CsvReader (org.spf4j.io.csv.CsvReader)10 Test (org.junit.Test)8 StringReader (java.io.StringReader)6 InputStreamReader (java.io.InputStreamReader)3 AvroRuntimeException (org.apache.avro.AvroRuntimeException)2 Schema (org.apache.avro.Schema)2 DecodedSchema (org.spf4j.avro.DecodedSchema)2 CsvParseException (org.spf4j.io.csv.CsvParseException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 TokenType (org.spf4j.io.csv.CsvReader.TokenType)1