DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • NiFi In-Memory Processing
  • Real-Time Analytics: All Data, Any Data, Any Scale, at Any Time
  • Event-Driven Pipelines With Apache Pulsar and Go
  • Contract-First Integration: Building Scalable Systems With Flyway, OpenAPI, and Kafka

Trending

  • Kafka and Spark Structured Streaming in Enterprise: The Patterns That Hold Up Under Pressure
  • Master-Class: Understanding Database Replication (Single, Multi, and Leaderless)
  • How to Build and Optimize AI Models for Real-World Applications
  • Navigating the Complexities of AI-Driven Integration in Multi-Cloud Environments: A Veteran’s Insights
  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 
Tim Spann user avatar
Tim Spann
DZone Core CORE ·
Aug. 13, 19 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
27.1K 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. 

 

 


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
  • Event-Driven Pipelines With Apache Pulsar and Go
  • Contract-First Integration: Building Scalable Systems With Flyway, OpenAPI, and Kafka

Partner Resources

×

Comments

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

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook