Cloud

Metadata

In Redpanda Connect each message has raw contents and metadata, which is a map of key/value pairs representing an arbitrary amount of complementary data.

When an input protocol supports attributes or metadata they will automatically be added to your messages, refer to the respective input documentation for a list of metadata keys. When an output supports attributes or metadata, it can send the metadata key/value pairs of a message (subject to service limits). Which keys it sends depends on the output. See Restricting metadata.

Editing metadata

Redpanda Connect allows you to add and remove metadata using the mapping processor. For example, you can do something like this in your pipeline:

pipeline:
  processors:
  - mapping: |
      # Remove all existing metadata from messages
      meta = deleted()

      # Add a new metadata field `time` from the contents of a JSON
      # field `event.timestamp`
      meta time = event.timestamp

You can also use Bloblang to delete individual metadata keys with:

meta foo = deleted()

Or do more interesting things like remove all metadata keys with a certain prefix:

meta = @.filter(kv -> !kv.key.has_prefix("kafka_"))

Using metadata

Metadata values can be referenced in any field that supports interpolation functions. For example, you can route messages to Kafka topics using interpolation of metadata keys:

output:
  redpanda:
    seed_brokers: [ TODO ]
    topic: ${! meta("target_topic") }

Redpanda Connect also allows you to conditionally process messages based on their metadata with the switch processor:

pipeline:
  processors:
  - switch:
    - check: '@doc_type == "nested"'
      processors:
        - sql_insert:
            driver: mysql
            dsn: foouser:foopassword@tcp(localhost:3306)/foodb
            table: footable
            columns: [ foo, bar, baz ]
            args_mapping: |
              root = [
                this.document.foo,
                this.document.bar,
                @kafka_topic,
              ]
              # In:  {"document":{"foo":"value1","bar":"value2"}}

Restricting metadata

Sometimes it’s useful to refer to metadata fields at the output level without sending them with your data. Outputs that support metadata, headers, or some other variant of enriched fields on messages control which metadata keys they send in one of two ways:

  • Outputs such as redpanda and http_client send only the keys that match metadata.include_prefixes or metadata.include_patterns. Without a metadata block, they send no metadata.

  • Outputs such as aws_sqs and amqp_0_9 send every key by default. To leave keys out, list their prefixes in metadata.exclude_prefixes. The aws_sqs output sends at most 10 keys with each message.

For example, this configuration uses the metadata key target_topic to choose the topic, and sends only the keys foo and bar as headers. target_topic is not sent:

output:
  redpanda:
    seed_brokers: [ TODO ]
    topic: ${! meta("target_topic") }
    metadata:
      include_patterns: [ "^(foo|bar)$" ]

When there are many metadata keys you do not want to send, it can help to use a Bloblang mapping to give all of these "private" keys a common prefix, and then send only the keys without it:

pipeline:
  processors:
    # Has an explicit list of public metadata keys, and everything else is given
    # an underscore prefix.
    - mapping: |
        let allowed_meta = [
          "foo",
          "bar",
          "baz",
        ]
        meta = @.map_each_key(key -> if !$allowed_meta.contains(key) {
          "_" + key
        })

output:
  redpanda:
    seed_brokers: [ TODO ]
    topic: ${! meta("_target_topic") }
    metadata:
      include_patterns: [ "^[^_]" ]