• Latest
TopicRecordNameStrategy in Kafka – DZone

TopicRecordNameStrategy in Kafka – DZone

March 10, 2023
The Rise of MKBHD: Marques Brownlee's Journey to YouTube Stardom #shorts #marquesbrownlee #mkbhd

The Rise of MKBHD: Marques Brownlee's Journey to YouTube Stardom #shorts #marquesbrownlee #mkbhd

June 9, 2023
How to watch Tom Holland in The Crowded Room on Apple TV

How to watch Tom Holland in The Crowded Room on Apple TV

June 9, 2023
Sonic Superstars Offering Free “Modern” Amy Outfit To Newsletter Subscribers

Sonic Superstars Offering Free “Modern” Amy Outfit To Newsletter Subscribers

June 9, 2023
(For Southeast Asia) “FINAL FANTASY VII REBIRTH” New Trailer Revealed! – PlayStation.Blog

(For Southeast Asia) “FINAL FANTASY VII REBIRTH” New Trailer Revealed! – PlayStation.Blog

June 9, 2023
Sonic Superstars Includes “New Character” By OG Designer Naoto Ohshima

Sonic Superstars Includes “New Character” By OG Designer Naoto Ohshima

June 9, 2023
9to5Mac Happy Hour 423: iOS 16.4 beta 2, 2018 iPad Pro in hindsight, MLS Season Pass experience

9to5Mac Happy Hour 437: iOS 17, watchOS 10, 15-inch MacBook Air, and more WWDC impressions

June 9, 2023
Two Switch Games Will “No Longer Be Available” On The eShop

Two Switch Games Will “No Longer Be Available” On The eShop

June 9, 2023
Marvel Snap to Get Tournament Mode Aimed at High-Skilled Players

Marvel Snap to Get Tournament Mode Aimed at High-Skilled Players

June 9, 2023
FAKEFRIEND#attitude @adazionofficial @UncleDane @mkbhd @Officialblessingceo

FAKEFRIEND#attitude @adazionofficial @UncleDane @mkbhd @Officialblessingceo

June 9, 2023
Summer Game Fest: Baldur’s Gate 3 Will Feature Jason Isaacs’ Menacing Brand of Villainy

Summer Game Fest: Baldur’s Gate 3 Will Feature Jason Isaacs’ Menacing Brand of Villainy

June 9, 2023
Noob prank on @fizagamingofficial 😂 1vs4 clutch 💪 #freefire #shorts #viral #youtubeshorts

Noob prank on @fizagamingofficial 😂 1vs4 clutch 💪 #freefire #shorts #viral #youtubeshorts

June 9, 2023
1Password teases passkey support coming to its app with iOS 17

1Password teases passkey support coming to its app with iOS 17

June 9, 2023
Advertise with us
Friday, June 9, 2023
Bookmarks
  • Login
  • Register
GetUpdated
  • Game Updates
  • Mobile Gaming
  • Playstation News
  • Xbox News
  • Switch News
  • MMORPG
  • Game News
  • IGN
  • Retro Gaming
  • Tech News
  • Apple Updates
  • Jailbreak News
  • Mobile News
  • Software Development
  • Photography
  • Contact
No Result
View All Result
GetUpdated
No Result
View All Result
GetUpdated
No Result
View All Result
ADVERTISEMENT

TopicRecordNameStrategy in Kafka – DZone

March 10, 2023
in Software Development
Reading Time:5 mins read
0 0
0
Share on FacebookShare on WhatsAppShare on Twitter


Apache Kafka is a widely used streaming platform. Kafka works based on the pub-sub model. An application that publishes a message called Producer. On the other side, the application consuming it is called a Consumer.

Topics are used to exchange messages between the systems. In a distributed environment, there could be multiple messages which are to be exchanged. There can be a situation the number of topics that are needed can grow.

To normalize the way how the topics are created, one can take advantage of TopicRecordNameStrategy where one single topic can be used for a given entity for example User can be used for UserCreated and UserUpdated.

Events like create, update, and delete of a user can be published into the same topic.

Each record in a Kafka topic consists of a key, a value, and a timestamp. Kafka provides various strategies for naming the records in a topic, including the topicRecordNameStrategy. In this article, we will explore how to use the topicRecordNameStrategy to publish messages of two different event types to the same Kafka topic.

1. Create a topic “users” by running the following command

bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic users

2. Configure the Kafka producer to use the topicRecordNameStrategy. Consider adding value.subject.name.strategy key to supporting TopicRecordNameStrategy.

Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.subject.name.strategy", "io.confluent.kafka.serializers.subject.TopicRecordNameStrategy");

KafkaProducer<String, String> producer = new KafkaProducer<>(props);

3.  Define two different classes for each event type and serialize them using the same producer instance. The topicRecordnameStrategy will automatically set the record name to the Kafka topic based on the className for the serialized message.

public class UserCreated {

  private String id;
  private String firstName;
  private String lastName;
  private String email;
  
  //getters and setters
}

public class UserUpdated {

  private String id;
  private String firstName;
  private String lastName;
  private String email;
  private Long updatedAt;
  
  //getters and setters
}

4. Define two Avro schemas for both the events UserCreated and userUpdated.

{
  "type": "record",
  "name": "UserCreated",
  "namespace": "com.messages.events.user",
  "fields": [
    {"name": "id", "type": "string"},
    {"name": "firstName", "type": "string"},
    {"name": "lastName", "type": "string"},
    {"name": "emailId", "type": "string"}
  ]
}

{
  "type": "record",
  "name": "UserUpdated",
  "namespace": "com.messages.events.user",
  "fields": [
    {"name": "id", "type": "string"},
    {"name": "firstName", "type": "string"},
    {"name": "lastName", "type": "string"},
    {"name": "email", "type": "string"},
    {"name": "updatedAt", "type": "long"}
  ]
}

5. Create a ProducerRecord to publish the UserCreated event to Kafka. Notice that we are using the topic users while publishing the message:

UserCreated userCreated = new UserCreated("1000", "userFirstName", "UserLastName","userOne@email.com");
ProducerRecord<String, Object> userCreatedRecord = new ProducerRecord<>("user", userCreated.getId(), userCreated);
producer.send(userCreatedRecord);

UserUpdated userUpdated = new UserUpdated("1000", "Updated UserFirstName", "Updated UserLastName" "userOne@email.com", Instant.now().toEpochMilli());
ProducerRecord<String, Object> userUpdatedRecord = new ProducerRecord<>("users", userUpdated.getId(), userUpdated);
producer.send(userUpdatedRecord);

In this example, we are setting the record name in the Kafka topic to the event type name by using the topicRecordNameStrategy.

Conclusion

In summary, messages of different event types are published to the same Kafka topic which custom record names. This allows you to avoid multiple Kafka topics for different event types for the same entity.



Source link

ShareSendTweet
Previous Post

Evolve your production! 🔎 -Marques Brownlee.

Next Post

RoboCop: Rogue City Shows Off Action-Packed Gameplay Trailer

Related Posts

Breaking Down the Monolith – DZone

June 8, 2023
0
0
Breaking Down the Monolith – DZone
Software Development

This is an article from DZone's 2023 Containers Trend Report.For more: Read the Report Conventionally, software applications were developed using...

Read more

Superior Stream Processing – DZone

June 8, 2023
0
0
Superior Stream Processing – DZone
Software Development

In the era of data-driven decision-making, the Data Lakehouse paradigm has emerged as a promising solution, bringing together the best...

Read more
Next Post
RoboCop: Rogue City Shows Off Action-Packed Gameplay Trailer

RoboCop: Rogue City Shows Off Action-Packed Gameplay Trailer

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

© 2021 GetUpdated – MW.

  • About
  • Advertise
  • Privacy & Policy
  • Terms & Conditions
  • Contact

No Result
View All Result
  • Game Updates
  • Mobile Gaming
  • Playstation News
  • Xbox News
  • Switch News
  • MMORPG
  • Game News
  • IGN
  • Retro Gaming
  • Tech News
  • Apple Updates
  • Jailbreak News
  • Mobile News
  • Software Development
  • Photography
  • Contact

Welcome Back!

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Fill the forms bellow to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In
Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?