Defined Type: kafka::topic

Defined in:
manifests/topic.pp

Summary

This defined type handles the creation of Kafka topics.

Overview

Examples:

Basic usage

kafka::topic { 'test':
  ensure             => present,
  zookeeper          => 'localhost:2181',
  replication_factor => 1,
  partitions         => 1,
}

Parameters:

  • ensure (String[1]) (defaults to: '')

    Should the topic be created.

  • zookeeper (String[1]) (defaults to: '')

    The connection string for the ZooKeeper connection in the form host:port. Multiple hosts can be given to allow fail-over.

  • replication_factor (Integer) (defaults to: 1)

    The replication factor for each partition in the topic being created. If not supplied, defaults to the cluster default.

  • partitions (Integer) (defaults to: 1)

    The number of partitions for the topic being created or altered. If not supplied for create, defaults to the cluster default.

  • bin_dir (String[1]) (defaults to: '/opt/kafka/bin')

    The directory where the file kafka-topics.sh is located.

  • config (Optional[Hash[String[1],String[1]]]) (defaults to: undef)

    A topic configuration override for the topic being created or altered. See the Kafka documentation for full details on the topic configs.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'manifests/topic.pp', line 34

define kafka::topic (
  String[1] $ensure                           = '',
  String[1] $zookeeper                        = '',
  Integer   $replication_factor               = 1,
  Integer   $partitions                       = 1,
  String[1] $bin_dir                          = '/opt/kafka/bin',
  Optional[Hash[String[1],String[1]]] $config = undef,
) {
  $_zookeeper          = "--zookeeper ${zookeeper}"
  $_replication_factor = "--replication-factor ${replication_factor}"
  $_partitions         = "--partitions ${partitions}"

  if $config {
    $_config_array = $config.map |$key, $value| { "--config ${key}=${value}" }
    $_config = join($_config_array, ' ')
  } else {
    $_config = ''
  }

  if $ensure == 'present' {
    exec { "create topic ${name}":
      path    => "/usr/bin:/usr/sbin/:/bin:/sbin:${bin_dir}",
      command => "kafka-topics.sh --create ${_zookeeper} ${_replication_factor} ${_partitions} --topic ${name} ${_config}",
      unless  => "kafka-topics.sh --list ${_zookeeper} | grep -x ${name}",
    }
  }
}