# How to use the input kafka topic in the output elasticsearch with logstash?

**URL:** <https://discuss.elastic.co/t/how-to-use-the-input-kafka-topic-in-the-output-elasticsearch-with-logstash/176186>\
**Category:** Logstash\
**Created:** [April 10, 2019, 9:37am UTC](https://discuss.elastic.co/t/how-to-use-the-input-kafka-topic-in-the-output-elasticsearch-with-logstash/176186 "2019-04-10T09:37:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![evahsiao](https://avatars.discourse-cdn.com/v4/letter/e/d26b3c/32.png) [@evahsiao](https://discuss.elastic.co/u/evahsiao)\
**Post date:** [April 10, 2019, 9:37am UTC](https://discuss.elastic.co/t/how-to-use-the-input-kafka-topic-in-the-output-elasticsearch-with-logstash/176186/1 "2019-04-10T09:37:02Z")

</div>

Here is my use case:  
I use kafka messages is the input data and then output to the elasticsearch database. I consume multi topics as below setting:

```
input {
   kafka {
      bootstrap_servers => "127.0.0.1:9092"
      group_id => "im_group"
      auto_offset_reset => "latest" #automatically reset the offset to the latest offset
      consumer_threads => 3
      #decorate_events => true 
       topics_pattern => "im_(.*)" #subscribe topics with im_
      codec => json {
      charset => "UTF-8"
  }   
}

```

now i want to use the topic as the elasticsearch index, for example, a topic named im\_test, then insert into the elasticsearch im\_test index, and others will be insert into the exact index as their topic named.

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [April 10, 2019, 11:11am UTC](https://discuss.elastic.co/t/how-to-use-the-input-kafka-topic-in-the-output-elasticsearch-with-logstash/176186/2 "2019-04-10T11:11:26Z")

</div>

If you set decorate\_events =\> true then the topic name will be in [@metadata][kafka][topic]. You can then use

```
index => "%{[@metadata][kafka][topic]}"

```

on the elasticsearch output.

---

<div class="post-metadata">

**Author:** ![evahsiao](https://avatars.discourse-cdn.com/v4/letter/e/d26b3c/32.png) [@evahsiao](https://discuss.elastic.co/u/evahsiao)\
**Post date:** [April 10, 2019, 11:17am UTC](https://discuss.elastic.co/t/how-to-use-the-input-kafka-topic-in-the-output-elasticsearch-with-logstash/176186/3 "2019-04-10T11:17:25Z")

</div>

Thank you for your help. This works for me, 🍁🍁🍁

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 8, 2019, 11:18am UTC](https://discuss.elastic.co/t/how-to-use-the-input-kafka-topic-in-the-output-elasticsearch-with-logstash/176186/4 "2019-05-08T11:18:09Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
