Skip to main content
Version: Current

Variable

Mode: Streaming Mode: Request-Response

Description​

A Variable component is used to set or unset variables in context.

As the record moves through the scenario, it can be extended with additional information. This additional information is stored in variables - named placeholders that are attached to the record during scenario execution. Variables make it possible to retain and reuse intermediate results or derived data later in the scenario.

The SpEL expression defining the value of the variable must evaluate to one of the SpEL data types.

Operations​

  • SET (default): declares a new variable or updates an existing one, using Variable Name and Expression.
  • UNSET: removes one or more selected local variables from context so they are not propagated further.

Building a record with the Data Mapper​

To build a record without writing the SpEL by hand, open the Data Mapper with the magic-wand button at the end of the Expression field. Drag context variables onto target fields, or load the fields from a sample or a topic schema; Apply writes the resulting SpEL record literal into Expression. The Data Mapper replaces an expression that is not a record literal.

Examples​

Let's assume that:

  • the name of the scenario which is being authored is example-scenario.
  • the processing mode is streaming
  • events in the in the Kafka topic ingested by the scenario contain just one field name (type string) - as in the example scenario created when to sign up to Nussknacker Cloud.
  • the last event payload is "John"
ExpressionValue
#meta.processName"example-scenario"
#meta.processName.length16
#meta.processName +" " + #input.name"example-scenario John"
#input.name.length > 4FALSE

You can find many more examples of SpEL expression in the SpEL cheetsheet.

RecordVariable​

To declare a record, use a plain Variable component whose Expression is a SpEL record literal, and edit it field by field in the Data Mapper. Example:

{name: #input.name, surname: 'Doe'}

The specialized record-variable component, which declares a record as a list of named fields, produces the same outcome, but it is deprecated and will be removed: use a plain Variable with a record literal instead.

Internally, Nussknacker converts SpEL record into a JSON object.