<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Abhinav Nath</title>
    <description>The latest articles on DEV Community by Abhinav Nath (@abhinav_nath).</description>
    <link>https://dev.to/abhinav_nath</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F375027%2Fca0d7015-42b7-4148-9c65-b87e44961a3e.jpeg</url>
      <title>DEV Community: Abhinav Nath</title>
      <link>https://dev.to/abhinav_nath</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abhinav_nath"/>
    <language>en</language>
    <item>
      <title>Building a Simple Kafka Producer and Consumer using Python</title>
      <dc:creator>Abhinav Nath</dc:creator>
      <pubDate>Sat, 01 Jun 2024 06:53:34 +0000</pubDate>
      <link>https://dev.to/abhinav_nath/building-a-simple-kafka-producer-and-consumer-using-python-4o5h</link>
      <guid>https://dev.to/abhinav_nath/building-a-simple-kafka-producer-and-consumer-using-python-4o5h</guid>
      <description>&lt;p&gt;Let's create a simple mini project to interact with Kafka using Python.&lt;/p&gt;

&lt;p&gt;We will build a simple message Producer and Consumer. Mini projects like this are helpful when you quickly want to test some behaviour of Kafka. So let's get started.&lt;/p&gt;

&lt;p&gt;First off, let's spin up all the required components using this &lt;a href="https://github.com/abhinav-nath/all-about-kafka/blob/master/code/python-producer-and-consumer/docker-compose.yml"&gt;docker-compose.yml&lt;/a&gt;. It will start Zookeeper, Kafka and Kafdrop containers in your system.&lt;/p&gt;




&lt;p&gt;Let's create our message Producer now.&lt;/p&gt;

&lt;p&gt;We need to import &lt;code&gt;KafkaProducer&lt;/code&gt; from the kafka library.&lt;/p&gt;

&lt;p&gt;We have to specify the address of our Kafka server (which we created in the above step) while creating a &lt;code&gt;KafkaProducer&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We need to pass these minimum parameters in the &lt;code&gt;producer.send()&lt;/code&gt; method:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Topic name&lt;/strong&gt; : we will use the name &lt;code&gt;my_test_topic&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;value&lt;/strong&gt; : the message itself&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;key (optional)&lt;/strong&gt; : the key of the message
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;kafka&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;KafkaProducer&lt;/span&gt;

&lt;span class="n"&gt;bootstrap_servers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;localhost:29092&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;topic_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;my_test_topic&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;

&lt;span class="n"&gt;producer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;KafkaProducer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bootstrap_servers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bootstrap_servers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;future&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;producer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;topic_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;1&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;This is message 1&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;metadata&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;future&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;message sent successfully on topic %s partition %s&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;metadata&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;metadata&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;partition&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we run our Producer, the specified Kafka topic (&lt;code&gt;my_test_topic&lt;/code&gt;) gets created automatically and the message is sent to the topic.&lt;/p&gt;




&lt;p&gt;Let's create the Consumer now. We need the server and topic name as above.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;kafka&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;KafkaConsumer&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;

&lt;span class="n"&gt;bootstrap_servers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;localhost:29092&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;topic_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;my_test_topic&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;

&lt;span class="n"&gt;consumer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;KafkaConsumer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                         &lt;span class="n"&gt;topic_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                         &lt;span class="n"&gt;bootstrap_servers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bootstrap_servers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                         &lt;span class="n"&gt;auto_offset_reset&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;earliest&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                         &lt;span class="n"&gt;enable_auto_commit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                         &lt;span class="n"&gt;group_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;my-group-1&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
                        &lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;consumer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Message received - %s:%d:%d: key=%s value=%s&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;partition&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;KeyboardInterrupt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the Consumer and it will receive the message sent by the Producer.&lt;/p&gt;




&lt;p&gt;Thanks for reading! The source code is available in this &lt;a href="https://github.com/abhinav-nath/all-about-kafka/tree/master/code/python-producer-and-consumer"&gt;GitHub repo&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://buymeacoffee.com/abhinav_nath"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsy4jq995umabrwecxqrl.png" alt="Support me if you liked this post" width="170" height="37"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>kafka</category>
      <category>pubsub</category>
      <category>eventdriven</category>
    </item>
    <item>
      <title>Java Tidbits: Compare your Strings wisely!</title>
      <dc:creator>Abhinav Nath</dc:creator>
      <pubDate>Sat, 25 May 2024 07:29:54 +0000</pubDate>
      <link>https://dev.to/abhinav_nath/java-tidbits-compare-your-strings-wisely-2g2</link>
      <guid>https://dev.to/abhinav_nath/java-tidbits-compare-your-strings-wisely-2g2</guid>
      <description>&lt;p&gt;Welcome to the first installment of &lt;strong&gt;Java Tidbits&lt;/strong&gt;, a series where I share small and simple Java tips to help with day-to-day development and help developers avoid common mistakes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Let's begin
&lt;/h3&gt;

&lt;p&gt;In Java, string comparison is a fundamental task, yet it often leads to subtle bugs if not done correctly. A common mistake among both new and experienced Java developers is using the &lt;code&gt;.equals()&lt;/code&gt; method improperly during String comparisons.&lt;/p&gt;

&lt;p&gt;In this blog, we'll explore why &lt;code&gt;"SOME_STRING".equals(someObj.getText())&lt;/code&gt; is a safer and more reliable way of comparing strings than &lt;code&gt;someObj.getText().equals("SOME_STRING")&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Understanding &lt;code&gt;.equals()&lt;/code&gt; in Java
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;equals()&lt;/code&gt; method in Java is used to compare the content of two strings for equality. Unlike the &lt;code&gt;==&lt;/code&gt; operator, which checks if two references point to the same object, &lt;code&gt;.equals()&lt;/code&gt; compares the actual characters in the strings.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;str1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;str2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;str2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;str2&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The Common Pitfall
&lt;/h3&gt;

&lt;p&gt;A common pattern you might see is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;someObj&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getText&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"SOME_STRING"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// do something&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At first glance, this seems perfectly fine. However, this approach has a hidden danger: if &lt;code&gt;someObj.getText()&lt;/code&gt; returns &lt;code&gt;null&lt;/code&gt;, it will throw a &lt;code&gt;NullPointerException&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;To avoid the risk of a &lt;code&gt;NullPointerException&lt;/code&gt;, it is better to write the comparison as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"SOME_STRING"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;someObj&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getText&lt;/span&gt;&lt;span class="o"&gt;()))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// do something&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's why this is better:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Null Safety:&lt;/strong&gt; When &lt;code&gt;"SOME_STRING"&lt;/code&gt; is a string literal, it is guaranteed to be non-null. Therefore, calling &lt;code&gt;.equals()&lt;/code&gt; on it ensures that the method is always invoked on a valid object, even if &lt;code&gt;someObj.getText()&lt;/code&gt; returns &lt;code&gt;null&lt;/code&gt;. This prevents the dreaded &lt;code&gt;NullPointerException&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Readability and Intent:&lt;/strong&gt; Writing &lt;code&gt;"SOME_STRING".equals(someObj.getText())&lt;/code&gt; makes it immediately clear that you are comparing a constant value against a variable value. This enhances readability and intent of the code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Consistency:&lt;/strong&gt; Adopting this pattern consistently across your codebase helps in maintaining uniformity and reduces the chances of null-related bugs.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Both new and experienced developers can overlook this simple yet crucial detail. It's an easy mistake to make, especially when you're confident that the method (like &lt;code&gt;someObj.getText()&lt;/code&gt;) won't return &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Facing a &lt;code&gt;NullPointerException&lt;/code&gt; because of this mistake can lead to the coding equivalent of slipping on a banana peel. It is an embarrassing stumble that can be easily avoided with a bit of defensive programming!&lt;/p&gt;




&lt;p&gt;&lt;a href="https://buymeacoffee.com/abhinav_nath"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsy4jq995umabrwecxqrl.png" alt="Support me if you liked this post" width="170" height="37"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>coding</category>
      <category>programming</category>
      <category>cleancode</category>
    </item>
  </channel>
</rss>
