VOOZH about

URL: https://dzone.com/articles/creating-apache-kafka-topics-dynamically-as-part-o

โ‡ฑ Creating Apache Kafka Topics Dynamically as Part of a DataFlow


Related

  1. DZone
  2. Coding
  3. Tools
  4. Creating Apache Kafka Topics Dynamically as Part of a DataFlow

Creating Apache Kafka Topics Dynamically as Part of a DataFlow

Creating Kafka topics programmatically as part of streaming.

By Aug. 13, 19 ยท Tutorial
Likes
Comment
Save
27.2K Views

Join the DZone community and get the full member experience.

Join For Free

Apache Kafka is becoming the cornerstone for everything from event-driven microservices to IoT to CDC to log ingestion. Using Apache Kafka with proper enterprise tooling and Apache NiFi will let you do this with agility, stability, scalability, security, performance, and traceability. Using Apache NiFi's provenance/lineage will allow you to see everything going on with your pipelines from source to sinks, even if that includes 1,000 Kafka Topics.

Sometimes when you are ingesting data at scale, whether it is from a Data Warehouse, Logs, REST API, IoT, Social Media or other sources, you may need to create new Apache Kafka topics, depending on the type, variation, newness, schema, schema version, etc.

Instead of having to manually create an Apache Kafka topic with Cloudera Streams Messaging Manager or Apache Kafka command line (kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test), I would like to create it mid-stream based on names that are relevant to arriving data. This could be the name of the schema from the data, the table name of the original date, some unique name generated with the data, or another source. For my example, I am generating a unique name via Apache NiFi Expression Language:

nifi${now():format('yyyyMMddmmss')}${UUID()}

This is a Proof of Concept; there are more features I would add if I wanted this for production use cases, such as adding fields for Number Of Partitions and Number of Replicas. If this processor looks useful to you, or if you just want to try it out, the full Apache-licensed source code is included at the end of this article. Enjoy, fork, enhance and deploy!


Example Run Of Our New Processor

๐Ÿ‘ Generating new processor


๐Ÿ‘ Required fields

The processor is very easy to use; you merely enter your Kafka Broker URL, such as demo.hortonworks.com:6667. Then, you can enter the name of your Kafka topic. The processor will validate to make sure you have a valid name, which should be Alphanumeric, with only the addition of periods, dashes, and underscores. It will run quickly. When completed, you can check out the results. Your flowfile will be unchanged, but you will get new attributes as seen below.

๐Ÿ‘ New attributes

๐Ÿ‘ New attributes


You will get kafka.bootstrap (your Broker URL), kafka.client.id (a generate one time use client id), kafka.topic.<TOPIC_NAME>  โ€” with one for each Kafka topic that exists, kafka.topic.creation.success, a status of flag, kafka.topic.message โ€” a message, kafka.topic.YourNewNamed one. 

๐Ÿ‘ Image

๐Ÿ‘ Image


In IntelliJ I quickly developed this program using the Apache Kafka Admin API and some JUnit tests. For a production use case, I would probably just use the Cloudera SMM REST API to create topics.

See: https://www.datainmotion.dev/2019/04/streams-messaging-manager-smm-rest-api.html

Call is here: https://docs.hortonworks.com/HDPDocuments/SMM/SMM-1.2.0/rest-api-reference/index.html#!/Topic_metadata_related_operations/createTopics

๐Ÿ‘ Topic metadata related operations

It is trivial to call a REST API from Apache NiFi, so I can use an Apache NiFi flow to orchestrate an entire Kafka lifecycle with management and monitoring for real-time interaction.

Source Code for Custom Apache NiFi Processor

https://github.com/tspannhw/kafkaadmin-processor

Source Code for Apache Kafka Shell Scripts

https://github.com/tspannhw/nifi-kafka-admin

kafka Apache NiFi

Opinions expressed by DZone contributors are their own.

Related

  • NiFi In-Memory Processing
  • Real-Time Analytics: All Data, Any Data, Any Scale, at Any Time
  • Parallel Kafka Batch Processing With Kotlin Coroutines in Spring Boot
  • Combining Temporal and Kafka for Resilient Distributed Systems

Partner Resources

ร—

Comments

The likes didn't load as expected. Please refresh the page and try again.

Let's be friends: