[ https://issues.apache.org/jira/browse/NIFI-1971?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15485671#comment-15485671 ]
ASF GitHub Bot commented on NIFI-1971: -------------------------------------- Github user trixpan commented on a diff in the pull request: https://github.com/apache/nifi/pull/858#discussion_r78475823 --- Diff: nifi-nar-bundles/nifi-enrich-bundle/nifi-enrich-processors/src/main/java/org/apache/nifi/processors/enrich/AbstractEnrichProcessor.java --- @@ -152,10 +166,68 @@ // Fails to NONE default: // NONE was chosen, just appending the record result as group0 without further splitting - results.put("enrich." + schema + ".record" + String.valueOf(recordPosition) + ".group0", rawResult); + results.put("enrich." + schema + ".record" + recordPosition + ".group0", rawResult); break; } return results; } + /** + * This method returns the parsed record string in the form of + * a map of two strings, consisting of a iteration aware attribute + * names and its values + * + + * @param rawResult the raw query results to be parsed + * @param queryParser The parsing mechanism being used to parse the data into groups + * @param queryRegex The regex to be used to split the query results into groups. The regex MUST implement at least on named capture group "KEY" to be used to populate the table rows + * @param lookupKey The regular expression named capture group or number of the column of a split to be used for matching + * @return Table with attribute names and values where each Table row uses the value of the KEY named capture group specified in @param queryRegex + */ + protected Table<String, String, String> parseBatchResponse(String rawResult, String queryParser, String queryRegex, String lookupKey, String schema) { + // Note the hardcoded record0. + // Since iteration is done within the parser and Multimap is used, the record number here will always be 0. + // Consequentially, 0 is hardcoded so that batched and non batched attributes follow the same naming + // conventions + final String recordPosition = ".record0"; + + final Table<String, String, String> results = HashBasedTable.create(); + + switch (queryParser) { + case "Split": + Scanner scanner = new Scanner(rawResult); + while (scanner.hasNextLine()) { + String line = scanner.nextLine(); + // Time to Split the results... + String[] splitResult = line.split(queryRegex); + + for (int r = 0; r < splitResult.length; r++) { + results.put(splitResult[ Integer.valueOf(lookupKey) - 1 ], "enrich." + schema + recordPosition + ".group" + String.valueOf(r), splitResult[r]); + + } + } + break; + case "RegEx": + // prepare the regex + Pattern p; + // Regex is multiline. Each line should include a KEY for lookup + p = Pattern.compile(queryRegex, Pattern.MULTILINE); + + Matcher matcher = p.matcher(rawResult); + while (matcher.find()) { + // Note that RegEx matches capture group 0 is usually broad but starting with it anyway + // for the sake of purity + for (int r = 0; r <= matcher.groupCount(); r++) { + if (!StringUtils.isEmpty(matcher.group("KEY"))) { --- End diff -- Pierre, This KEY reference should have been removed so I guess I stuffed up something on my git... I rewrote the code to unify on regex capture group numbers. I also add code to catch IndexOutOfBoundsException (the equivalent to the IllegalArgumentException you mentioned above) > Create a batch capable pseudo-whois ("netcat") enrichment Processor > ------------------------------------------------------------------- > > Key: NIFI-1971 > URL: https://issues.apache.org/jira/browse/NIFI-1971 > Project: Apache NiFi > Issue Type: Improvement > Affects Versions: 1.0.0, 0.7.0 > Reporter: Andre > Assignee: Andre > Fix For: 1.1.0 > > > While the QueryDNS created can be used on low to medium volume enrichment and > to licensed DNS based lookups (e.g. commercial use of SpamHaus) many > enrichment providers prefer the use of bulk queries using pseudo whois API > (a.k.a. netcat interface). > as documented > [here|https://www.shadowserver.org/wiki/pmwiki.php/Services/IP-BGP#toc6] the > bulk interfaces work by connecting to port 43/TCP and sending a payload like: > {code} > begin origin > 4.5.4.3 > 17.112.152.32 > 208.77.188.166 > end > {code} -- This message was sent by Atlassian JIRA (v6.3.4#6332)