Configure a script consumer

  • Release version: Washingtondc
  • Updated February 1, 2024
  • 1 minute to read
  • Use a script to import and process data from your Kafka environment.

    Before you begin

    • Role required: integration_hub_admin
    • This consumer requires a Stream Connect subscription. For more information, see https://www.servicenow.com/products/automation-engine.html.
    • The following plugins are required.
      • The ServiceNow IntegrationHub Kafka Consumer [com.glide.hub.kafka_consumer] plugin.
      • The ServiceNow IntegrationHub ETL Consumer - Kafka [com.glide.hub.etl_consumer.kafka] plugin.

    About this task

    To configure a consumer, you need to create two records.
    1. The consumer record, which specifies how to import and process data.
    2. A record for the Kafka stream, which defines the stream of data to your consumer.
    This task covers the consumer creation. For instructions on creating a Kafka stream, see Create a Kafka stream.

    Procedure

    1. Navigate to All > IntegrationHub > Consumers > Script Consumer.
    2. Select New.
    3. In the form, fill in the fields.
      Table 1. Kafka script consumer
      Field Description
      Name Name of the Script consumer.
      Delivery guarantee If there's a node failure, option to specify the delivery guarantee for incoming messages. Select one of the following.
      • No lost but duplicates: Ensures that all messages are delivered at least once. Some messages might be delivered more than once.
      • Once or not at all: Ensures that a message isn’t delivered more than once. Some messages might not be delivered at all.
      Event consumer Script to use to consume the messages received from the Kafka topic.
      Application Application scope for the Transform Map Consumer.
    4. Select Save.

    Example

    This example shows a sample script for processing messages.
    (function process(messages) {
     // Add your code here to consume kafka messages 
     // sample message [ { 'key' : 'message_key' , 'message' : 'message' , 'headers' : [ { 'key' : 'header_key' , 'value' : 'header_value' } ] } ] 
    
     for (var i = 0; i < messages.length; i++) {
         var message = JSON.parse(messages[i].message);
         gs.info('Number ' + message.number + ', short description ' + message.short_description +
             ', headers ' + JSON.stringify(messages[i].headers));
     }
    })(messages);

    What to do next

    Create a Kafka stream for this consumer. After the stream is activated, you can start receiving messages from your Kafka environment.