Merge pull request #296 from apache/KARAF-7403
[KARAF-7403] Add a config param to use default key id in the split pa…
diff --git a/manual/src/main/asciidoc/user-guide/collectors.adoc b/manual/src/main/asciidoc/user-guide/collectors.adoc
index 1c69523..30c5ab5 100644
--- a/manual/src/main/asciidoc/user-guide/collectors.adoc
+++ b/manual/src/main/asciidoc/user-guide/collectors.adoc
@@ -185,6 +185,7 @@
----
separator=,
keys=first,second,third,fourth
+useDefaultKey=false
----
If the parser gets a line (collected by the file collector) like `this,is,a,test`, the line will be parsed as follows (the file collector will send the following data to the dispatcher):
@@ -196,7 +197,8 @@
fourth->test
----
-If the `keys` configuration is not set, then `key-0`, `key-1`, etc will be used.
+If the `keys` configuration is not set and `useDefaultKey` is set to `true`, then `key-0`, `key-1`, etc will be used.
+When the `useDefaultKey` is set to `false` and the line doesn't match the `keys` configuration the data will not be sent.
To use this parser in the file collector, you have to define it in the `parser.target` configuration (in `etc/org.apache.karaf.decanter.collector.file-XXXX.cfg`):
diff --git a/parser/split/src/main/cfg/org.apache.karaf.decanter.parser.split.cfg b/parser/split/src/main/cfg/org.apache.karaf.decanter.parser.split.cfg
index 4796b77..8dd59cb 100644
--- a/parser/split/src/main/cfg/org.apache.karaf.decanter.parser.split.cfg
+++ b/parser/split/src/main/cfg/org.apache.karaf.decanter.parser.split.cfg
@@ -22,4 +22,6 @@
#
separator=,
-# keys=key1,key2,key3
\ No newline at end of file
+# keys=key1,key2,key3
+
+useDefaultKey=false
\ No newline at end of file
diff --git a/parser/split/src/main/java/org/apache/karaf/decanter/parser/split/SplitParser.java b/parser/split/src/main/java/org/apache/karaf/decanter/parser/split/SplitParser.java
index 6f2c9b5..9195c57 100644
--- a/parser/split/src/main/java/org/apache/karaf/decanter/parser/split/SplitParser.java
+++ b/parser/split/src/main/java/org/apache/karaf/decanter/parser/split/SplitParser.java
@@ -38,6 +38,7 @@
private String separator;
private String keys = null;
+ private Boolean useDefaultKey;
@Activate
public void activate(ComponentContext componentContext) {
@@ -47,11 +48,13 @@
public void activate(Dictionary<String, Object> config) {
this.separator = (config.get("separator") != null) ? (String) config.get("separator") : ",";
this.keys = (config.get("keys") != null) ? (String) config.get("keys") : null;
+ this.useDefaultKey = (config.get("useDefaultKey") != null) ? (Boolean) config.get("useDefaultKey") : false;
}
@Override
public Map<String, Object> parse(String key, String line) {
Map<String, Object> map = new HashMap<>();
+ boolean isByPassed = false;
if (line != null) {
String[] valuesArray = line.split(separator);
String[] keysArray;
@@ -59,10 +62,15 @@
if (this.keys != null) {
keysArray = this.keys.split(",");
if (keysArray.length != valuesArray.length) {
- LOGGER.warn("keys count and values count don't match, using default keys ID");
- keysArray = new String[valuesArray.length];
- for (int i = 0; i < valuesArray.length; i++) {
- keysArray[i] = "key-" + i;
+ if (useDefaultKey) {
+ LOGGER.warn("keys count and values count don't match, using default key ID");
+ keysArray = new String[valuesArray.length];
+ for (int i = 0; i < valuesArray.length; i++) {
+ keysArray[i] = "key-" + i;
+ }
+ } else {
+ LOGGER.warn("keys count and values count don't match, bypassing default key ID");
+ isByPassed = true;
}
}
} else {
@@ -71,22 +79,25 @@
keysArray[i] = "key-" + i;
}
}
- for (int i = 0; i < valuesArray.length; i++) {
- try {
- map.put(keysArray[i], Integer.parseInt(valuesArray[i]));
- continue;
- } catch (Exception e) {
- // nothing to do
- }
- try {
- map.put(keysArray[i], Long.parseLong(valuesArray[i]));
- continue;
- } catch (Exception e) {
- // nothing to do
+ if (!isByPassed) {
+ for (int i = 0; i < valuesArray.length; i++) {
+ try {
+ map.put(keysArray[i], Integer.parseInt(valuesArray[i]));
+ continue;
+ } catch (Exception e) {
+ // nothing to do
+ }
+
+ try {
+ map.put(keysArray[i], Long.parseLong(valuesArray[i]));
+ continue;
+ } catch (Exception e) {
+ // nothing to do
+ }
+ // if not integer and long value
+ map.put(keysArray[i], valuesArray[i]);
}
- // if not integer and long value
- map.put(keysArray[i], valuesArray[i]);
}
}
return map;
diff --git a/parser/split/src/test/java/org/apache/karaf/decanter/parser/split/SplitParserTest.java b/parser/split/src/test/java/org/apache/karaf/decanter/parser/split/SplitParserTest.java
index c05149a..a2ad070 100644
--- a/parser/split/src/test/java/org/apache/karaf/decanter/parser/split/SplitParserTest.java
+++ b/parser/split/src/test/java/org/apache/karaf/decanter/parser/split/SplitParserTest.java
@@ -59,11 +59,29 @@
}
@Test
- public void invalidKeys() throws Exception {
+ public void invalidKeysWithoutDefaultKey() throws Exception {
SplitParser splitParser = new SplitParser();
Dictionary<String, Object> config = new Hashtable<>();
config.put("separator", "\\s+");
config.put("keys", "this,is");
+ config.put("useDefaultKey", true);
+ splitParser.activate(config);
+
+ Map<String, Object> result = splitParser.parse("line", TEST_LINE);
+ Assert.assertEquals(4, result.size());
+ Assert.assertEquals("this", result.get("key-0"));
+ Assert.assertEquals("is", result.get("key-1"));
+ Assert.assertEquals("a", result.get("key-2"));
+ Assert.assertEquals("test", result.get("key-3"));
+ }
+
+ @Test
+ public void invalidKeysWithDefaultKey() throws Exception {
+ SplitParser splitParser = new SplitParser();
+ Dictionary<String, Object> config = new Hashtable<>();
+ config.put("separator", "\\s+");
+ config.put("keys", "this,is");
+ config.put("useDefaultKey", true);
splitParser.activate(config);
Map<String, Object> result = splitParser.parse("line", TEST_LINE);