Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add static fields, now can be added some predefined columns to event stream #217

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -15,6 +15,7 @@

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -52,11 +53,20 @@ public class CSVToJSONDataConverter implements IDataConverter {
private final List<String> fieldNames;
private final String delimiter;
private final IJSONPrinter jsonProducer;
private static String STATIC_FIELDS_KEY = "staticFields";
private final Map<String, Object> staticFields;

public CSVToJSONDataConverter(Configuration config) {
fieldNames = config.readList(FIELDS_KEY, String.class);
delimiter = config.readString(DELIMITER_KEY, ",");
jsonProducer = ProcessingUtilsFactory.getPrinter(config);

if(config.containsKey(STATIC_FIELDS_KEY)){
Configuration configuration = config.readConfiguration(STATIC_FIELDS_KEY);
staticFields = configuration.getConfigMap();
} else {
staticFields = new HashMap<String, Object>();
}
}

@Override
Expand All @@ -81,7 +91,11 @@ public ByteBuffer convert(ByteBuffer data) throws DataConversionException {
throw new DataConversionException("Unable to create the column map", e);
}
}


for (String key : staticFields.keySet()) {
recordMap.put(key, staticFields.get(key));
}

String dataJson = jsonProducer.writeAsString(recordMap) + NEW_LINE;

return ByteBuffer.wrap(dataJson.getBytes(StandardCharsets.UTF_8));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.nio.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class DataConverterTest {

Expand Down Expand Up @@ -66,7 +67,63 @@ public void testCSVToJSONDataConverter() throws Exception {
final String expectedStrMoreThanColumns = "{\"column1\":\"value1\",\"column2\":\"value2\",\"column3\":\"value3\",\"column4\":\"value4\"}\n";
verifyDataConversion(converter, dataStrMoreThanColumns.getBytes(), expectedStrMoreThanColumns.getBytes());
}


@SuppressWarnings("serial")
@Test
public void testCSVToJSONStaticFields() throws Exception {

final Map<String, String> hashmap = new HashMap<String, String>();
hashmap.put("host", "server1.com");
hashmap.put("host2", "server2.com");

final Configuration config = new Configuration(new HashMap<String, Object>() {{
put("optionName", "CSVTOJSON");
put("staticFields", hashmap);
put("customFieldNames", Arrays.asList("column1", "column2", "column3", "column4"));
}});

final IDataConverter converter = new CSVToJSONDataConverter(config);
final String dataStr = "value1,value2,value3,value4\n";
final String expectedStr = "{\"column1\":\"value1\",\"column2\":\"value2\",\"column3\":\"value3\",\"column4\":\"value4\",\"host2\":\"server2.com\",\"host\":\"server1.com\"}\n";
verifyDataConversion(converter, dataStr.getBytes(), expectedStr.getBytes());

}

@SuppressWarnings("serial")
@Test
public void testCSVToJSONStaticFieldsNotDefined() throws Exception {

final Configuration config = new Configuration(new HashMap<String, Object>() {{
put("optionName", "CSVTOJSON");
put("customFieldNames", Arrays.asList("column1", "column2", "column3", "column4"));
}});

final IDataConverter converter = new CSVToJSONDataConverter(config);
final String dataStr = "value1,value2,value3,value4\n";
final String expectedStr = "{\"column1\":\"value1\",\"column2\":\"value2\",\"column3\":\"value3\",\"column4\":\"value4\"}\n";
verifyDataConversion(converter, dataStr.getBytes(), expectedStr.getBytes());

}

@SuppressWarnings("serial")
@Test
public void testCSVToJSONStaticFieldsEmpty() throws Exception {

final Map<String, String> hashmap = new HashMap<String, String>();

final Configuration config = new Configuration(new HashMap<String, Object>() {{
put("optionName", "CSVTOJSON");
put("staticFields", hashmap);
put("customFieldNames", Arrays.asList("column1", "column2", "column3", "column4"));
}});

final IDataConverter converter = new CSVToJSONDataConverter(config);
final String dataStr = "value1,value2,value3,value4\n";
final String expectedStr = "{\"column1\":\"value1\",\"column2\":\"value2\",\"column3\":\"value3\",\"column4\":\"value4\"}\n";
verifyDataConversion(converter, dataStr.getBytes(), expectedStr.getBytes());

}

@SuppressWarnings("serial")
@Test
public void testCSVToJSONDataConverterConvertingTSV() throws Exception {
Expand Down