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
messagefield holding the body as a string. -
If
mongodb.includeHeadersistrue, the event headers are added to the document as a sub-document namedheaders.
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.uri |
— |
The MongoDB connection string, for example |
mongodb.database |
the database of |
The database the documents are written to. Required if the connection string does not name a database. |
mongodb.collection |
the collection of |
The default collection the documents are written to. Required if the connection string does not name a collection, as in |
mongodb.collectionHeader |
— |
The name of the event header whose value selects the target collection through |
mongodb.collectionMap.<headerValue> |
— |
The collection that events whose |
mongodb.collectionMapFallback |
true |
Whether events whose header value has no mapping are written to the default collection. If |
mongodb.includeHeaders |
false |
Whether the event headers are stored in the |
mongodb.writeConcern |
the write concern of |
The write concern of the inserts: one of |
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.