This topic describes the syntax and format of flow control functions, along with relevant scenario examples.
c_if function
This function processes logs that meet certain conditions. If no condition is met, no actions will be taken.
- The conditions and actions in the function parameters must appear in pairs, and there must be at least one pair of condition and action combination.
- If there are multiple true conditions, only the action for the first true condition is executed.
Function syntax/format
c_if(condition 1, action 1, condition 2, action 2, ……)
The parameters are described below:
Function examples
- Scenario: When the original log contains the phone field, newly add the contact_type field.
c_if(f_has("phone"), f_set("contact_type", "telephone"))
"contact_type":"telephone"
Tip
The first log contains the phone field and adds the contact_type field after processing. The second log does not contain the phone field, so the phone field is not added after processing.
c_if_else function
Process different logic based on condition evaluation. If the condition is true, perform action 1; otherwise, perform action 2.
Function syntax/format
c_if_else(condition, action 1, action 2)
The parameters are described below:
Function examples
- Scenario: Determine whether a person is an adult based on the age field, and output the result to a new field is_adult.
c_if_else(op_ge(v("age"), 18), f_set("is_adult", True), f_set("is_adult", False))
c_switch function
Process different logic based on condition evaluation. Based on multiple branch conditions, execute the action corresponding to the first true condition. If none of the conditions are met, the default action will be executed.
Function syntax/format
c_switch(condition 1, action 1, condition 2, action 2, ..., default=default action )
The parameters are described below:
Function examples
- Scenario: Determine the region based on the city log field.
c_switch(op_eq(v("city"), "chengdu"), f_set("region", "West"), op_eq(v("city"), "shanghai"), f_set("region", "East"))
c_compose function
Used to combine multiple operations. All operations should be global operation functions.
- TLS will sequentially invoke the specified operations, transmitting, transforming, and finally returning the log data.
- For a given log record, if any global operation deletes the log, TLS will not execute subsequent operations on it.
Function syntax/format
c_compose(action 1, action 2...)
The parameters are described below:
Function examples
- Scenario: When the log contains a content field, update its value to value1 and rename it to content_modified.
c_if(f_has("content"),c_compose(f_set("content","value1"),f_rename("content","content_modified")))
"content_modified":"value1"