C3 AI Documentation Home

Stream Processing

C3 AI provides you with options to handle large and complex data efficiently. Use data integration options provided by the C3 Agentic AI Platform to support data aggregation from multiple disparate sources.

Stream Processing

Stream processing is the data integration path for ingesting external data continuously, as it arrives, rather than in scheduled collections like batch processing. You model a consumer by defining a type that mixes in CloudInboundMessage and connecting it to a streaming source such as Apache Kafka, AWS Kinesis, or Azure Event Hubs. The platform subscribes to the source, dispatches each incoming message to the consumer's process() method, and manages connection handling, acknowledgment, and fault tolerance for you — so your application can act on events the moment they occur. This topic covers that external-data ingestion path; it is not a general-purpose queue-processing or message-broker service.

Examples in C3 AI

  • Integrate with streaming systems and message queues such as Apache Kafka, AWS Kinesis, and Azure Event Hubs to process events and transactions in real time.

  • Receive external data feeds like news and financial data, allowing for immediate processing and updates.

Example: Process Real-Time Kafka Messages

The following example models a Kafka consumer that ingests messages from an Orders topic and persists them to a target type. For messages of the form:

JSON
{
  "ordertime": 1507163076488,
  "orderid": 46,
  "itemid": "Item_53",
  "orderunits": 8.553269159575795,
  "address": {
    "city": "City_41",
    "state": "State_83",
    "zipcode": 55197
  }
}

Note This example uses a CloudInboundMessage consumer, which dispatches each message to your own process() method. A CloudMessageSourceCollection is a separate, codeless ingestion path that processes messages through a source → transform → target pipeline without custom code. The two are independent subscriber mechanisms — if both are registered on the same broker/topic, CloudMessageDispatcher.dispatchToAll fans each message out to both independently. For the codeless approach and the full broker/credential setup, see Connect C3 AI Application to Apache Kafka.

Step 1: Define the Source Types

Define source types that model the structure of the incoming messages. Nested objects must be modeled as their own named type, so the address is defined separately as SourceOrdersAddress. This enables the platform to automatically deserialize and validate incoming JSON messages.

Type
type SourceOrdersJSON mixes Source {
  ordertime: int
  orderid: int
  itemid: string
  orderunits: double
  address: SourceOrdersAddress
}
Type
type SourceOrdersAddress {
  city: string
  state: string
  zipcode: int
}

Step 2: Create the CloudInboundMessage Consumer Type

Create a consumer type by mixing in CloudInboundMessage<SourceOrdersJSON>. Mixing in CloudInboundMessage indicates the type is a Kafka consumer; the platform dispatches incoming messages to its process() method and manages connection handling, acknowledgment, and fault tolerance for you.

Type
type SourceOrdersInboundQueue mixes CloudInboundMessage<SourceOrdersJSON> {
  process: ~ js-server
}

Step 3: Implement Stream Processing Logic

Implement process() to map each incoming message to the target OrdersData type and persist it. Message delivery guarantees at-least-once semantics, so process() must be idempotent — use upsertBatch rather than createBatch so reprocessing the same message does not create duplicates. upsertBatch matches existing rows by id, so set id to a value derived from the message (here, orderid) rather than letting OrdersData.make() generate one; otherwise a replayed message gets a new id and creates a duplicate row.

JavaScript
// SourceOrdersInboundQueue.js
function process(messages) {
  var array = OrdersData.array();
  var meas;

  messages.forEach(function (msg) {
    meas = OrdersData.make({
      id: "Order_" + msg.orderid,
      ordertime: msg.ordertime,
      orderid: msg.orderid,
      itemid: msg.itemid,
      orderunits: msg.orderunits,
      city: msg.address.city,
      state: msg.address.state,
      zipcode: msg.address.zipcode
    });
    array.push(meas);
  });

  OrdersData.upsertBatch(array);
  log.info("Processed " + messages.length + " messages");
}

Step 4: Register the Consumer with the Kafka Topic

Associate the consumer with the Orders topic and register it. After registration, the platform subscribes to the topic and dispatches arriving messages to SourceOrdersInboundQueue.process() in real time.

JavaScript
SourceOrdersInboundQueue.setBrokerName("Orders", "ApacheKafkaTopic");
SourceOrdersInboundQueue.setBatchSize(10);
SourceOrdersInboundQueue.setTimeoutMillis(5000);

CloudMessageDispatcherConfig.clearCache();
SourceOrdersInboundQueue.register();

See also

Was this page helpful?