Skip to content

Receiving Custom Text Events via Kafka


This application demonstrates how to configure WSO2 Streaming Integrator to receive events to the SweetProductionStream via Kafka transport in Text format using custom mapping and log the events in LowProductionAlertStream to the output console.


  1. Setup Kafka.
    • Kafka libs to be added and converted to OSGI from {KafkaHome}/libs are as follows.
      • kafka_2.11-
      • kafka-clients-
      • metrics-core-2.2.0.jar
      • scala-library-2.11.8.jar
      • zkclient-0.8.jar
      • zookeeper-3.4.6.jar
    • Add the OSGI converted kafka libs to {WSO2SIHome}/lib.
    • Add the kafka libs to {WSO2SIHome}/samples/sample-clients/lib.
  2. Save this sample.
  3. If there is no syntax error, the following message is shown on the console:
    * Siddhi App PublishKafkaInJsonFormat successfully deployed.


To convert Kafka libs to OSGI, 1. Create a folder (eg: kafka) and copy Kafka libs to be added from {KafkaHome}/libs. 2. Create another folder (eg: kafka-osgi, This folder will have the libs that converted to OSGI). 3. Navigate to {WSO2SIHome}/bin and issue the following command. - For Linux:

./ <path/kafka> <path/kafka-osgi>
- For Windows:
./jartobundle.bat <path/kafka> <path/kafka-osgi>
4. If converted successfully then for each lib, following messages would be shown on the terminal.
- INFO: Created the OSGi bundle <kafka-lib-name>.jar for JAR file <absolute_path>/kafka/<kafka-lib-name>.jar
5. You can find the osgi converted libs in kafka-osgi folder. You can copy that to {WSO2SIHome}/lib.

Executing the Sample:

  1. Navigate to {KafkaHome} and start zookeeper node using bin/ config/
  2. Navigate to {KafkaHome} and start kafka server node using bin/ config/
  3. Start the Siddhi application by clicking on 'Run'.
  4. If the Siddhi application starts successfully, the following messages are shown on the console:
    - ReceiveKafkaInTextFormatWithCustomMapping.siddhi - Started Successfully!
    - Kafka version :
    - Kafka commitId : b8642491e78c5a13
    - Adding partition 0 for topic: kafka_sample_topic
    - Adding partitions [0] for topic: kafka_sample_topic
    - Subscribed for topics: [kafka_sample_topic]
    - Kafka Consumer thread starting to listen on topic/s: [kafka_sample_topic] with partition/s: [0]
    - Discovered coordinator (id: 2147483647 rack: null) for group group

Testing the Sample:

Navigate to {WSO2SIHome}/samples/sample-clients/kafka-producer and run ant command as follows:

ant -Dtype=text -DcustomMapping=true

Viewing the Results:

Messages similar to the following would be shown on the console.

- INFO {} - ReceiveKafkaInTextFormatWithCustomMapping: LowProductionAlertStream : Event{timestamp=1513282182570, data=["Cupcake", 1665.0], isExpired=false}


  • Stop this Siddhi application, once you are done with the execution.
  • Stop Kafka server and Zookeeper server individually by executing Ctrl+C.
@App:description('Receive events via Kafka transport in Text format with custom mapping and view the output on the console')

 @map(type='text',fail.on.missing.attribute='true', regex.A='(id):(.*)', regex.B='(amount):([-.0-9]+)',
                @attributes(id = 'A[2]', amount = 'B[2]')))
define stream SweetProductionStream(id string, amount double);

define stream LowProductionAlertStream(id string, amount double);

from SweetProductionStream
select *
insert into LowProductionAlertStream;