Flume MongoDB User Guide

MongoDb Sink

Writes events to a MongoDB collection. The sink is provided by the Flume MongoDB project (artifact org.apache.flume:flume-mongodb-sink), which must be added to the agent’s class path usually by adding it as a Maven or Gradle dependency.

Each event becomes one MongoDB document:

  • The event body is decoded as UTF-8 and parsed as a JSON document. If the body is not valid JSON, the document has a single message field holding the body as a string.

  • If mongodb.includeHeaders is true, the event headers are added to the document as a sub-document named headers.

The events of a batch are grouped by target collection and inserted with one unordered insertMany call per collection. Documents rejected only because they violate a unique index, for example because an event was redelivered after a failure, are logged and skipped, and the rest of the batch is committed. Any other error rolls back the channel transaction, so the whole batch is retried.

By default every event is written to mongodb.collection. To route events to different collections, set mongodb.collectionHeader to the name of an event header and map each of its expected values to a collection with mongodb.collectionMap.<headerValue>. Events that do not have the header are written to the default collection. Events whose header value has no mapping are written to the default collection too, unless mongodb.collectionMapFallback is false, in which case the batch fails.

Required properties are in bold.

Property Name Default Description

channel

 — 

type

 — 

The component type name, needs to be mongodb.

mongodb.uri

 — 

The MongoDB connection string, for example mongodb://host1,host2/?replicaSet=rs0. Credentials, TLS and the other client options are configured here.

mongodb.database

the database of mongodb.uri

The database the documents are written to. Required if the connection string does not name a database.

mongodb.collection

the collection of mongodb.uri

The default collection the documents are written to. Required if the connection string does not name a collection, as in mongodb://host/database.collection.

mongodb.collectionHeader

 — 

The name of the event header whose value selects the target collection through mongodb.collectionMap.

mongodb.collectionMap.<headerValue>

 — 

The collection that events whose mongodb.collectionHeader header has the value <headerValue> are written to. Repeat it for each header value.

mongodb.collectionMapFallback

true

Whether events whose header value has no mapping are written to the default collection. If false, such an event fails the batch.

mongodb.includeHeaders

false

Whether the event headers are stored in the headers field of the document.

mongodb.writeConcern

the write concern of mongodb.uri

The write concern of the inserts: one of ACKNOWLEDGED, W1, W2, W3, UNACKNOWLEDGED, JOURNALED or MAJORITY.

batchSize

100

The maximum number of events taken from the channel and written in one transaction.

Example for agent named a1, which writes to the events collection of the Events database:

  • Properties

  • YAML

  • JSON

a1.channels = c1
a1.sinks = k1
a1.sinks.k1.type = mongodb
a1.sinks.k1.channel = c1
a1.sinks.k1.mongodb.uri = mongodb://localhost:27017
a1.sinks.k1.mongodb.database = Events
a1.sinks.k1.mongodb.collection = events
a1:
  channels: c1
  sinks:
    k1:
      type: mongodb
      channel: c1
      mongodb:
        uri: mongodb://localhost:27017
        database: Events
        collection: events
"a1": {
  "channels": "c1",
  "sinks": {
    "k1": {
      "type": "mongodb",
      "channel": "c1",
      "mongodb": {
        "uri": "mongodb://localhost:27017",
        "database": "Events",
        "collection": "events"
      }
    }
  }
}

The following example writes events to the audit or metrics collection depending on their eventType header, and every other event to events, together with its headers:

  • Properties

  • YAML

  • JSON

a1.sinks.k1.type = mongodb
a1.sinks.k1.channel = c1
a1.sinks.k1.mongodb.uri = mongodb://localhost:27017/Events
a1.sinks.k1.mongodb.collection = events
a1.sinks.k1.mongodb.collectionHeader = eventType
a1.sinks.k1.mongodb.collectionMap.Audit = audit
a1.sinks.k1.mongodb.collectionMap.Metric = metrics
a1.sinks.k1.mongodb.includeHeaders = true
a1.sinks.k1.mongodb.writeConcern = MAJORITY
a1:
  sinks:
    k1:
      type: mongodb
      channel: c1
      mongodb:
        uri: mongodb://localhost:27017/Events
        collection: events
        collectionHeader: eventType
        collectionMap:
          Audit: audit
          Metric: metrics
        includeHeaders: true
        writeConcern: MAJORITY
"a1": {
  "sinks": {
    "k1": {
      "type": "mongodb",
      "channel": "c1",
      "mongodb": {
        "uri": "mongodb://localhost:27017/Events",
        "collection": "events",
        "collectionHeader": "eventType",
        "collectionMap": {
          "Audit": "audit",
          "Metric": "metrics"
        },
        "includeHeaders": true,
        "writeConcern": "MAJORITY"
      }
    }
  }
}

A MongoDB Sink cannot write faster than a single insertMany call per batch. To increase throughput, drain several channels with one MongoDB Sink each, as the native/mongodb sample of the Flume samples does with four File Channels fed by a load-balancing Avro Source.