Skip to main content
Version: Current

Variable

Mode: Streaming Mode: Request-Response

Description​

A Variable component is used to set, overwrite 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, using Variable Name and Expression. A name already in scope is refused.
  • OVERWRITE: gives a variable already in scope a new value, using Variable Name and Expression, for example a counter in the body of a While. Global variables, such as #meta, cannot be overwritten. The variable keeps its type, so one declared as a number cannot become text. A value of a narrower type is widened, e.g. an Integer given to a Long variable, also in the fields of a record; a wider one has to be converted in the expression, e.g. with toInteger. A variable declared as null has no known type, so it takes any value.
  • UNSET: removes one or more selected local variables from context so they are not propagated further.

OVERWRITE and UNSET are chosen in the Operation field under Advanced.

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.