Configure a script consumer
Use a script to import and process data from your Kafka environment.
始める前に
- Role required: integration_hub_admin
- This consumer requires a Stream Connect subscription. For more information, see https://www.servicenow.com/now-platform/workflow-data-fabric.html.
- The ServiceNow Stream Connect Installer [com.glide.hub.stream_connect.installer] plugin is required.
このタスクについて
To configure a consumer, you need to create two records.
- The consumer record, which specifies how to import and process data.
- A record for the Kafka stream, which defines the stream of data to your consumer.
手順
例
- timestampEpoch, data type: long
- partition, data type: int
- offset, data type: long
- dataCenter, data type: string
(function process(messages) {
for (var i = 0; i < messages.length; i++) {
var m = messages[i];
var headerParts = [];
for (var j = 0; j < m.headers.length; j++) {
headerParts.push('header[' + j + ']_key=' + m.headers[j].key + ' | header[' + j + ']_value=' + m.headers[j].value);
}
var line = [
'message_key=' + m.key,
'message_body=' + m.message,
'partition=' + m.metadata.partition,
'offset=' + m.metadata.offset,
'datacenter_id=' + m.metadata.datacenter_id,
'utc_timestamp=' + m.utc_timestamp,
'timestamp_epoch=' + m.metadata.timestamp_epoch
].concat(headerParts).join(' | ');
gs.info('[Kafka Message] ' + line);
}
})(messages);
To convert the utc_timestamp field to GlideDateTime, see the following examples.
// Example 1
var timestamp = new GlideDateTime("2025-08-19 23:31:45");
console.log(timestamp.getValue());
// Example 2
var timestamp2 = new GlideDateTime();
timestamp2.setNumericValue(1755646305218);
console.log(timestamp2.getValue());
// Example 3
var timestamp3 = new GlideDateTime("1970-01-01 00:00:00");
timestamp3.add(1755646305218);
console.log(timestamp3.getValue());
次のタスク
Create a Kafka stream for this consumer. After the stream is activated, you can start receiving messages from your Kafka environment.