Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class CSVMessageDecoder implements StreamMessageDecoder<byte[]> {
private static final String CONFIG_COMMENT_MARKER = "commentMarker";
private static final String CONFIG_CSV_ESCAPE_CHARACTER = "escapeCharacter";
private static final String CONFIG_CSV_MULTI_VALUE_DELIMITER = "multiValueDelimiter";
public static final String NULL_STRING_VALUE = "nullStringValue";

private CSVFormat _format;
private CSVRecordExtractor _recordExtractor;
Expand Down Expand Up @@ -111,6 +112,11 @@ public void init(Map<String, String> props, Set<String> fieldsToRead, String top
format = format.withEscape(props.get(CONFIG_CSV_ESCAPE_CHARACTER).charAt(0));
}

String nullString = props.get(NULL_STRING_VALUE);
if (nullString != null) {
format = format.withNullString(nullString);
}

_format = format;

_recordExtractor = new CSVRecordExtractor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;


public class CSVMessageDecoderTest {
Expand Down Expand Up @@ -129,6 +130,27 @@ public void testEscapeCharacter()
assertEquals(destination.getValue("subjects"), "mat;hs");
}

@Test
public void testNullString()
throws Exception {
Map<String, String> decoderProps = getStandardDecoderProps();
decoderProps.put("header", "name;age;gender;subjects");
decoderProps.put("delimiter", ";");
decoderProps.put("nullStringValue", "null");
CSVMessageDecoder messageDecoder = new CSVMessageDecoder();
messageDecoder.init(decoderProps, ImmutableSet.of("name", "age", "gender", "subjects"), "");
String incomingRecord = "Alice;null;F;null";
GenericRow destination = new GenericRow();
messageDecoder.decode(incomingRecord.getBytes(StandardCharsets.UTF_8), destination);
assertNotNull(destination.getValue("name"));
assertNull(destination.getValue("age"));
assertNotNull(destination.getValue("gender"));
assertNull(destination.getValue("subjects"));

assertEquals(destination.getValue("name"), "Alice");
assertEquals(destination.getValue("gender"), "F");
}

@Test
public void testDefaultProps()
throws Exception {
Expand Down