Search in sources :

Example 11 with SimpleRecordSchema

use of org.apache.nifi.serialization.SimpleRecordSchema in project nifi by apache.

the class TestCSVRecordReader method testDateNullFormat.

@Test
public void testDateNullFormat() throws IOException, MalformedRecordException {
    final String text = "date\n1983-01-01";
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("date", RecordFieldType.DATE.getDataType()));
    final RecordSchema schema = new SimpleRecordSchema(fields);
    try (final InputStream bais = new ByteArrayInputStream(text.getBytes());
        final CSVRecordReader reader = new CSVRecordReader(bais, Mockito.mock(ComponentLog.class), schema, format, true, false, null, RecordFieldType.TIME.getDefaultFormat(), RecordFieldType.TIMESTAMP.getDefaultFormat(), "UTF-8")) {
        final Record record = reader.nextRecord(false, false);
        assertEquals("1983-01-01", (String) record.getValue("date"));
    }
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) RecordField(org.apache.nifi.serialization.record.RecordField) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Record(org.apache.nifi.serialization.record.Record) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) ComponentLog(org.apache.nifi.logging.ComponentLog) Test(org.junit.Test)

Example 12 with SimpleRecordSchema

use of org.apache.nifi.serialization.SimpleRecordSchema in project nifi by apache.

the class TestCSVRecordReader method testSimpleParse.

@Test
public void testSimpleParse() throws IOException, MalformedRecordException {
    final List<RecordField> fields = getDefaultFields();
    fields.replaceAll(f -> f.getFieldName().equals("balance") ? new RecordField("balance", doubleDataType) : f);
    final RecordSchema schema = new SimpleRecordSchema(fields);
    try (final InputStream fis = new FileInputStream(new File("src/test/resources/csv/single-bank-account.csv"));
        final CSVRecordReader reader = createReader(fis, schema, format)) {
        final Object[] record = reader.nextRecord().getValues();
        final Object[] expectedValues = new Object[] { "1", "John Doe", 4750.89D, "123 My Street", "My City", "MS", "11111", "USA" };
        Assert.assertArrayEquals(expectedValues, record);
        assertNull(reader.nextRecord());
    }
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) RecordField(org.apache.nifi.serialization.record.RecordField) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 13 with SimpleRecordSchema

use of org.apache.nifi.serialization.SimpleRecordSchema in project nifi by apache.

the class TestCSVRecordReader method testTimeNoCoersionUnexpectedFormat.

@Test
public void testTimeNoCoersionUnexpectedFormat() throws IOException, MalformedRecordException {
    final String text = "time\n01:02:03";
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("time", RecordFieldType.TIME.getDataType()));
    final RecordSchema schema = new SimpleRecordSchema(fields);
    try (final InputStream bais = new ByteArrayInputStream(text.getBytes());
        final CSVRecordReader reader = new CSVRecordReader(bais, Mockito.mock(ComponentLog.class), schema, format, true, false, RecordFieldType.DATE.getDefaultFormat(), "HH-MM-SS", RecordFieldType.TIMESTAMP.getDefaultFormat(), "UTF-8")) {
        final Record record = reader.nextRecord(false, false);
        assertEquals("01:02:03", (String) record.getValue("time"));
    }
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) RecordField(org.apache.nifi.serialization.record.RecordField) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Record(org.apache.nifi.serialization.record.Record) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) ComponentLog(org.apache.nifi.logging.ComponentLog) Test(org.junit.Test)

Example 14 with SimpleRecordSchema

use of org.apache.nifi.serialization.SimpleRecordSchema in project nifi by apache.

the class TestCSVRecordReader method testDateNoCoersionExpectedFormat.

@Test
public void testDateNoCoersionExpectedFormat() throws IOException, MalformedRecordException {
    final String text = "date\n11/30/1983";
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("date", RecordFieldType.DATE.getDataType()));
    final RecordSchema schema = new SimpleRecordSchema(fields);
    try (final InputStream bais = new ByteArrayInputStream(text.getBytes());
        final CSVRecordReader reader = new CSVRecordReader(bais, Mockito.mock(ComponentLog.class), schema, format, true, false, "MM/dd/yyyy", RecordFieldType.TIME.getDefaultFormat(), RecordFieldType.TIMESTAMP.getDefaultFormat(), "UTF-8")) {
        final Record record = reader.nextRecord(false, false);
        final java.sql.Date date = (Date) record.getValue("date");
        final Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("gmt"));
        calendar.setTimeInMillis(date.getTime());
        assertEquals(1983, calendar.get(Calendar.YEAR));
        assertEquals(10, calendar.get(Calendar.MONTH));
        assertEquals(30, calendar.get(Calendar.DAY_OF_MONTH));
    }
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) RecordField(org.apache.nifi.serialization.record.RecordField) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) ComponentLog(org.apache.nifi.logging.ComponentLog) Date(java.sql.Date) Date(java.sql.Date) ByteArrayInputStream(java.io.ByteArrayInputStream) Record(org.apache.nifi.serialization.record.Record) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) Test(org.junit.Test)

Example 15 with SimpleRecordSchema

use of org.apache.nifi.serialization.SimpleRecordSchema in project nifi by apache.

the class TestCSVRecordReader method testTimestampNoCoersionExpectedFormat.

@Test
public void testTimestampNoCoersionExpectedFormat() throws IOException, MalformedRecordException, ParseException {
    final String timeFormat = "HH!mm!ss";
    DateFormat dateFmt = new SimpleDateFormat(timeFormat);
    dateFmt.setTimeZone(TimeZone.getTimeZone("gmt"));
    final String timeVal = "19!02!03";
    final String text = "timestamp\n" + timeVal;
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("timestamp", RecordFieldType.TIMESTAMP.getDataType()));
    final RecordSchema schema = new SimpleRecordSchema(fields);
    try (final InputStream bais = new ByteArrayInputStream(text.getBytes());
        final CSVRecordReader reader = new CSVRecordReader(bais, Mockito.mock(ComponentLog.class), schema, format, true, false, RecordFieldType.DATE.getDefaultFormat(), RecordFieldType.TIME.getDefaultFormat(), timeFormat, "UTF-8")) {
        final Record record = reader.nextRecord(false, false);
        final java.sql.Timestamp time = (Timestamp) record.getValue("timestamp");
        assertEquals(new Timestamp(dateFmt.parse(timeVal).getTime()), time);
    }
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) RecordField(org.apache.nifi.serialization.record.RecordField) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Timestamp(java.sql.Timestamp) ArrayList(java.util.ArrayList) ComponentLog(org.apache.nifi.logging.ComponentLog) Timestamp(java.sql.Timestamp) ByteArrayInputStream(java.io.ByteArrayInputStream) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) Record(org.apache.nifi.serialization.record.Record) SimpleDateFormat(java.text.SimpleDateFormat) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) Test(org.junit.Test)

Aggregations

SimpleRecordSchema (org.apache.nifi.serialization.SimpleRecordSchema)171 RecordSchema (org.apache.nifi.serialization.record.RecordSchema)152 Test (org.junit.Test)146 RecordField (org.apache.nifi.serialization.record.RecordField)127 Record (org.apache.nifi.serialization.record.Record)120 ArrayList (java.util.ArrayList)114 MapRecord (org.apache.nifi.serialization.record.MapRecord)86 HashMap (java.util.HashMap)82 InputStream (java.io.InputStream)58 FileInputStream (java.io.FileInputStream)53 ByteArrayInputStream (java.io.ByteArrayInputStream)52 ComponentLog (org.apache.nifi.logging.ComponentLog)50 LinkedHashMap (java.util.LinkedHashMap)33 File (java.io.File)30 DataType (org.apache.nifi.serialization.record.DataType)26 ByteArrayOutputStream (java.io.ByteArrayOutputStream)21 MalformedRecordException (org.apache.nifi.serialization.MalformedRecordException)19 RecordFieldType (org.apache.nifi.serialization.record.RecordFieldType)19 List (java.util.List)18 IOException (java.io.IOException)17