Skip to content

Commit

Permalink
changed the processing order - now: spel, regex, filters
Browse files Browse the repository at this point in the history
  • Loading branch information
jruaux committed Feb 9, 2021
1 parent edde42b commit c3bfbb1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,17 @@ public class KeyValueProcessingOptions {

@Option(arity = "1..*", names = "--spel", description = "SpEL expression to produce a field", paramLabel = "<f=exp>")
private Map<String, String> spelFields = new HashMap<>();
@Option(arity = "1..*", names = "--filter", description = "SpEL expression to filter records", paramLabel = "<exp>")
private List<String> filters = new ArrayList<>();
@Option(arity = "1..*", names = "--var", description = "Register a variable in the SpEL processor context", paramLabel = "<v=exp>")
private Map<String, String> variables = new HashMap<>();
@Option(names = "--date", description = "Processor date format (default: ${DEFAULT-VALUE})", paramLabel = "<string>")
private String dateFormat = new SimpleDateFormat().toPattern();
@Option(arity = "1..*", names = "--regex", description = "Extract named values from source field using regex", paramLabel = "<f=exp>")
private Map<String, String> regexes = new HashMap<>();
@Option(arity = "1..*", names = "--filter", description = "SpEL expression to filter records", paramLabel = "<exp>")
private List<String> filters = new ArrayList<>();

public ItemProcessor<Map<String, Object>, Map<String, Object>> processor(StatefulConnection<String, String> connection) {
List<ItemProcessor<Map<String, Object>, Map<String, Object>>> processors = new ArrayList<>();
if (!filters.isEmpty()) {
processors.add(new FilteringProcessor(filters));
}
if (!spelFields.isEmpty()) {
processors.add(new SpelProcessor(connection, new SimpleDateFormat(dateFormat), variables, spelFields));
}
Expand All @@ -40,6 +37,9 @@ public ItemProcessor<Map<String, Object>, Map<String, Object>> processor(Statefu
regexes.forEach((f, r) -> fields.put(f, RegexNamedGroupsExtractor.builder().regex(r).build()));
processors.add(new MapProcessor(fields));
}
if (!filters.isEmpty()) {
processors.add(new FilteringProcessor(filters));
}
if (processors.isEmpty()) {
return null;
}
Expand Down
39 changes: 26 additions & 13 deletions docs/processing.adoc
Original file line number Diff line number Diff line change
@@ -1,31 +1,44 @@
The following processors can be applied to records in that order:

* Filters
* Transforms
* Regular expressions
==== Filter

Keep records that match a https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#expressions[Spring Expression Language] (SpEL) boolean expression

`--filter "name matches '[a-zA-Z\\s]+'"`
* Filters
==== Transform

Produce key/value pairs using SpEL: `field1=<exp>`, `field2=<exp>`, ...
Produce key/value pairs using https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#expressions[Spring Expression Language] (SpEL): `field1=<exp>`, `field2=<exp>`, ...

For example `--spel field1=' generates a field named `field1` with always the same value `foo`.
For example `--spel "field1='foo'"` generates a field named `field1` with always the same value `foo`.

Input fields are accessed by name (e.g. `field3=field1+field2`).

The processor also exposes the following variables that can be called with the `#` prefix:

* `redis` : Redis connection to issue any command, e.g. `name=#redis.hgetall('person1').lastName`
* `date` : date parser/formatter, e.g. `epoch=#date.parse(mydate).getTime()`
* `index` : sequence number of the item being generated, e.g. `id=#index`
===== redis
Redis connection to issue any command:

`--spel "name=#redis.hgetall('person1').lastName"`

===== date
Date parser/formatter:

`--spel "epoch=#date.parse(mydate).getTime()"`

===== index
Sequence number of the item being generated:

`--spel "id=#index"`

==== Regex

Extract patterns from source fields using regular expressions:

`--regex "name=(?<first>\w+)\/(?<last>\w+)"`
`--regex "name=(?<first>\w+)\/(?<last>\w+)"`

==== Filter

Keep records that match a SpEL boolean expression.

`--filter "value matches '\\d+'"` will only keep records where the `value` field is a series of digits.


0 comments on commit c3bfbb1

Please sign in to comment.