use of org.apache.nifi.serialization.record.RecordSchema 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.record.RecordSchema 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.record.RecordSchema 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);
}
}
use of org.apache.nifi.serialization.record.RecordSchema in project nifi by apache.
the class TestCSVRecordReader method testExcelFormat.
@Test
public void testExcelFormat() throws IOException, MalformedRecordException {
final List<RecordField> fields = new ArrayList<RecordField>();
fields.add(new RecordField("fieldA", RecordFieldType.STRING.getDataType()));
fields.add(new RecordField("fieldB", RecordFieldType.STRING.getDataType()));
final RecordSchema schema = new SimpleRecordSchema(fields);
final String headerLine = "fieldA,fieldB";
final String inputRecord = "valueA,valueB";
final String csvData = headerLine + "\n" + inputRecord;
final byte[] inputData = csvData.getBytes();
try (final InputStream bais = new ByteArrayInputStream(inputData);
final CSVRecordReader reader = createReader(bais, schema, CSVFormat.EXCEL)) {
final Object[] record = reader.nextRecord().getValues();
final Object[] expectedValues = new Object[] { "valueA", "valueB" };
Assert.assertArrayEquals(expectedValues, record);
assertNull(reader.nextRecord());
}
}
use of org.apache.nifi.serialization.record.RecordSchema in project nifi by apache.
the class TestCSVRecordReader method testMultipleRecords.
@Test
public void testMultipleRecords() 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/multi-bank-account.csv"));
final CSVRecordReader reader = createReader(fis, schema, format)) {
final Object[] firstRecord = reader.nextRecord().getValues();
final Object[] firstExpectedValues = new Object[] { "1", "John Doe", 4750.89D, "123 My Street", "My City", "MS", "11111", "USA" };
Assert.assertArrayEquals(firstExpectedValues, firstRecord);
final Object[] secondRecord = reader.nextRecord().getValues();
final Object[] secondExpectedValues = new Object[] { "2", "Jane Doe", 4820.09D, "321 Your Street", "Your City", "NY", "33333", "USA" };
Assert.assertArrayEquals(secondExpectedValues, secondRecord);
assertNull(reader.nextRecord());
}
}
Aggregations