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