@Test public void testDate() 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(); 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)); } }
@Override public Record nextRecord(final boolean coerceTypes, final boolean dropUnknownFields) throws IOException, MalformedRecordException { final RecordSchema schema = getSchema(); final List<RecordField> recordFields = getRecordFields(); final int numFieldNames = recordFields.size(); for (final CSVRecord csvRecord : csvParser) { final Map<String, Object> values = new LinkedHashMap<>(recordFields.size() * 2); for (int i = 0; i < csvRecord.size(); i++) { final String rawValue = csvRecord.get(i); final String rawFieldName; final DataType dataType; if (i >= numFieldNames) { if (!dropUnknownFields) { values.put("unknown_field_index_" + i, rawValue); } continue; } else { final RecordField recordField = recordFields.get(i); rawFieldName = recordField.getFieldName(); dataType = recordField.getDataType(); } final Object value; if (coerceTypes) { value = convert(rawValue, dataType, rawFieldName); } else { value = convertSimpleIfPossible(rawValue, dataType, rawFieldName); } values.put(rawFieldName, value); } return new MapRecord(schema, values, coerceTypes, dropUnknownFields); } return null; }
CSVRecordReader extends AbstractCSVRecordReader { @Override public Record nextRecord(final boolean coerceTypes, final boolean dropUnknownFields) throws IOException, MalformedRecordException { final RecordSchema schema = getSchema(); final List<RecordField> recordFields = getRecordFields(); final int numFieldNames = recordFields.size(); for (final CSVRecord csvRecord : csvParser) { final Map<String, Object> values = new LinkedHashMap<>(recordFields.size() * 2); for (int i = 0; i < csvRecord.size(); i++) { final String rawValue = csvRecord.get(i); final String rawFieldName; final DataType dataType; if (i >= numFieldNames) { if (!dropUnknownFields) { values.put("unknown_field_index_" + i, rawValue); } continue; } else { final RecordField recordField = recordFields.get(i); rawFieldName = recordField.getFieldName(); dataType = recordField.getDataType(); } final Object value; if (coerceTypes) { value = convert(rawValue, dataType, rawFieldName); } else { value = convertSimpleIfPossible(rawValue, dataType, rawFieldName); } values.put(rawFieldName, value); } return new MapRecord(schema, values, coerceTypes, dropUnknownFields); } return null; } }
CSVRecordReader extends AbstractCSVRecordReader { @Override public Record nextRecord(final boolean coerceTypes, final boolean dropUnknownFields) throws IOException, MalformedRecordException { final RecordSchema schema = getSchema(); final List<RecordField> recordFields = getRecordFields(); final int numFieldNames = recordFields.size(); for (final CSVRecord csvRecord : csvParser) { final Map<String, Object> values = new LinkedHashMap<>(recordFields.size() * 2); for (int i = 0; i < csvRecord.size(); i++) { final String rawValue = csvRecord.get(i); final String rawFieldName; final DataType dataType; if (i >= numFieldNames) { if (!dropUnknownFields) { values.put("unknown_field_index_" + i, rawValue); } continue; } else { final RecordField recordField = recordFields.get(i); rawFieldName = recordField.getFieldName(); dataType = recordField.getDataType(); } final Object value; if (coerceTypes) { value = convert(rawValue, dataType, rawFieldName); } else { value = convertSimpleIfPossible(rawValue, dataType, rawFieldName); } values.put(rawFieldName, value); } return new MapRecord(schema, values, coerceTypes, dropUnknownFields); } return null; } CSVRecordReader(final InputStream in, final ComponentLog logger, final RecordSchema schema, final CSVFormat csvFormat, final boolean hasHeader, final boolean ignoreHeader,
                           final String dateFormat, final String timeFormat, final String timestampFormat, final String encoding); }
CSVRecordReader extends AbstractCSVRecordReader { @Override public Record nextRecord(final boolean coerceTypes, final boolean dropUnknownFields) throws IOException, MalformedRecordException { final RecordSchema schema = getSchema(); final List<RecordField> recordFields = getRecordFields(); final int numFieldNames = recordFields.size(); for (final CSVRecord csvRecord : csvParser) { final Map<String, Object> values = new LinkedHashMap<>(recordFields.size() * 2); for (int i = 0; i < csvRecord.size(); i++) { final String rawValue = csvRecord.get(i); final String rawFieldName; final DataType dataType; if (i >= numFieldNames) { if (!dropUnknownFields) { values.put("unknown_field_index_" + i, rawValue); } continue; } else { final RecordField recordField = recordFields.get(i); rawFieldName = recordField.getFieldName(); dataType = recordField.getDataType(); } final Object value; if (coerceTypes) { value = convert(rawValue, dataType, rawFieldName); } else { value = convertSimpleIfPossible(rawValue, dataType, rawFieldName); } values.put(rawFieldName, value); } return new MapRecord(schema, values, coerceTypes, dropUnknownFields); } return null; } CSVRecordReader(final InputStream in, final ComponentLog logger, final RecordSchema schema, final CSVFormat csvFormat, final boolean hasHeader, final boolean ignoreHeader,
                           final String dateFormat, final String timeFormat, final String timestampFormat, final String encoding); @Override Record nextRecord(final boolean coerceTypes, final boolean dropUnknownFields); @Override void close(); }
CSVRecordReader extends AbstractCSVRecordReader { @Override public Record nextRecord(final boolean coerceTypes, final boolean dropUnknownFields) throws IOException, MalformedRecordException { final RecordSchema schema = getSchema(); final List<RecordField> recordFields = getRecordFields(); final int numFieldNames = recordFields.size(); for (final CSVRecord csvRecord : csvParser) { final Map<String, Object> values = new LinkedHashMap<>(recordFields.size() * 2); for (int i = 0; i < csvRecord.size(); i++) { final String rawValue = csvRecord.get(i); final String rawFieldName; final DataType dataType; if (i >= numFieldNames) { if (!dropUnknownFields) { values.put("unknown_field_index_" + i, rawValue); } continue; } else { final RecordField recordField = recordFields.get(i); rawFieldName = recordField.getFieldName(); dataType = recordField.getDataType(); } final Object value; if (coerceTypes) { value = convert(rawValue, dataType, rawFieldName); } else { value = convertSimpleIfPossible(rawValue, dataType, rawFieldName); } values.put(rawFieldName, value); } return new MapRecord(schema, values, coerceTypes, dropUnknownFields); } return null; } CSVRecordReader(final InputStream in, final ComponentLog logger, final RecordSchema schema, final CSVFormat csvFormat, final boolean hasHeader, final boolean ignoreHeader,
                           final String dateFormat, final String timeFormat, final String timestampFormat, final String encoding); @Override Record nextRecord(final boolean coerceTypes, final boolean dropUnknownFields); @Override void close();  }