Skip to the content.

« Back to index

Functions

Table of Contents

Introduction

Functions can be specified in the functions section of a KSML definition file. The layout typically looks like this:

functions:
  my_first_predicate:
    type: predicate
    expression: key=='Some string'

  compare_params:
    type: generic
    parameters:
      - name: firstParam
        type: string
      - name: secondParam
        type: int
    globalCode: |
      import something from somepackage
      globalVar = 3
    code: |
      print('Hello there!')
    expression: firstParam == str(secondParam)

Functions are defined by the following tags:

Parameter Value Type Default Description  
type string generic The type of the function defined  
parameters List of parameter definitions empty list A list of parameters, each of which contains the mandatory fields name and type. See example above.  
globalCode string empty Snippet of Python code that is executed once upon creation of the Kafka Streams topology. This section can contain statements like import to import function libraries used in the code and expression sections.  
code string empty Python source code, which will be included in the called function. Bla
expression string empty Python expression that contains the returned function result. Typically the code "return expression" is generated for the Python interpreter. For example expression: key would generate the Python code return key for the function.

See below for the list of supported function types.

Function Types

Type Returns Parameter Value Type Description
aggregator any key any The key of the message
    value any The value of the message
    aggregatedValue any The aggregated value thus far.
forEach none key any The key of the message
    value any The value of the message
initializer any none    
keyTransformer any key any The key of the message
    value any The value of the message
keyValueMapper any key any The key of the message
    value any The value of the message
keyValueToKeyValueListTransformer [ (any, any) ] key any The key of the message
    value any The value of the message
keyValueToValueListTransformer [ any ] key any The key of the message
    value any The value of the message
keyValueTransformer (any, any) key any The key of the message
    value any The value of the message
merger any key any The key of the message
    value1 any The first value to be merged
    value2 any The second value to be merged
predicate boolean key any The key of the message
    value any The value of the message
reducer any value1 any The first value to be reduced
    value2 any The second value to be reduced
streamPartitioner int topic String The topic of the message
    key any The key of the message
    value any The value of the message
    numPartitions int The number of partitions on the topic
topicNameExtractor string key any The key of the message
    value any The value of the message
valueTransformer any key any The key of the message
    value any The value of the message