<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Streams on Samarthya</title><link>https://samarthya.me/tags/streams/</link><description>Recent content in Streams on Samarthya</description><generator>Hugo -- gohugo.io</generator><language>en</language><copyright>© Samarthya 2018-2026. All rights reserved.</copyright><lastBuildDate>Sat, 18 Jan 2025 00:00:00 +0000</lastBuildDate><atom:link href="https://samarthya.me/tags/streams/index.xml" rel="self" type="application/rss+xml"/><item><title>Kafka Cluster Fundamentals: Brokers, Topics, and Partitions</title><link>https://samarthya.me/posts/kafka-cluster-fundamentals/</link><pubDate>Sat, 18 Jan 2025 00:00:00 +0000</pubDate><guid>https://samarthya.me/posts/kafka-cluster-fundamentals/</guid><description>&lt;p&gt;I spent a fair amount of time looking at Kafka command output before the vocabulary settled in my head. Once I understood the difference between a broker, a topic, and a partition, the cluster stopped looking like one large mysterious system.&lt;/p&gt;</description></item><item><title>Kafka Streams</title><link>https://samarthya.me/posts/kafka-streams/</link><pubDate>Mon, 26 Feb 2024 12:00:00 +0000</pubDate><guid>https://samarthya.me/posts/kafka-streams/</guid><description>&lt;figure&gt;&lt;img&#10; class="my-0 rounded-md"&#10; loading="lazy"&#10; decoding="async"&#10; fetchpriority="auto"&#10; alt=""&#10; width="600"&#10; height="600"&#10; src="https://samarthya.me/posts/kafka-streams/stream-1.jpg"&#10; srcset="https://samarthya.me/posts/kafka-streams/stream-1.jpg 800w, https://samarthya.me/posts/kafka-streams/stream-1.jpg 1280w"&#10; sizes="(min-width: 768px) 50vw, 65vw"&#10; data-zoom-src="https://samarthya.me/posts/kafka-streams/stream-1.jpg"&gt;&lt;/figure&gt;&#10;&lt;p&gt;In today’s data-driven world, the ability to process information in real-time is crucial. &lt;strong&gt;Kafka Streams&lt;/strong&gt; emerges as a powerful tool for building &lt;strong&gt;real-time applications and microservices&lt;/strong&gt; that leverage the &lt;strong&gt;Apache Kafka&lt;/strong&gt; distributed streaming platform.&lt;/p&gt;</description></item><item><title>Java Stream: Examples</title><link>https://samarthya.me/posts/java-stream-examples/</link><pubDate>Mon, 26 Apr 2021 09:07:19 +0000</pubDate><guid>https://samarthya.me/posts/java-stream-examples/</guid><description>&lt;p&gt;Streams are convenient way to reduce the line of codes, but an understanding them is equally challenging. I write this blog, after realising a desperate attempt at solving a trivial problem just took overwhelming time. (Could be that I am getting old, but never late to learn things again)&lt;/p&gt;</description></item><item><title>Nth Prime</title><link>https://samarthya.me/posts/nth-prime/</link><pubDate>Tue, 15 Dec 2020 08:21:32 +0000</pubDate><guid>https://samarthya.me/posts/nth-prime/</guid><description>&lt;p&gt;Prime number is an age old problem. I am trying to solve it via streams.&lt;/p&gt;&#10;&lt;div class="highlight-wrapper"&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;class PrimeCalculator {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; public int nth(int nth) {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; if (nth &amp;lt;= 0)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; throw new IllegalArgumentException();&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; // Will skip the nth - 1&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; // 2 to n-1&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; return IntStream.iterate(2, i -&amp;gt; i + 1).filter(PrimeCalculator::isPrime).skip(nth - 1).findFirst().getAsInt();&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; public static boolean isPrime(int x) {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; // till half of x&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; return IntStream.rangeClosed(2, (int) (x/2) ).dropWhile(i -&amp;gt; x % i != 0).findFirst().isEmpty();&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&#10;&#10;&lt;h2 class="relative group"&gt;Explanation&#10; &lt;div id="explanation" class="anchor"&gt;&lt;/div&gt;&#10; &#10; &lt;span&#10; class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;&#10; &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#explanation" aria-label="Anchor"&gt;#&lt;/a&gt;&#10; &lt;/span&gt;&#10; &#10;&lt;/h2&gt;&#10;&lt;div class="highlight-wrapper"&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;public int nth(int nth) {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; if (nth &amp;lt;= 0)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; throw new IllegalArgumentException();&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; // Will skip the nth - 1&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; // 2 to n-1&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; return IntStream.iterate(2, i -&amp;gt; i + 1).filter(PrimeCalculator::isPrime).skip(nth - 1).findFirst().getAsInt();&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&#10;&lt;p&gt;The method &lt;code&gt;nth(int)&lt;/code&gt; allows you to return the nth prime starting at &lt;code&gt;2, 3, 5....&lt;/code&gt;&lt;/p&gt;</description></item><item><title>JavaStream: Characters</title><link>https://samarthya.me/posts/javastream-characters/</link><pubDate>Fri, 13 Nov 2020 10:20:02 +0000</pubDate><guid>https://samarthya.me/posts/javastream-characters/</guid><description>&lt;p&gt;Continuing from the last blog how about checking a pangram using streams.&lt;/p&gt;&#10;&lt;div class="highlight-wrapper"&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;public class PangramChecker {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; private String allChars = &amp;#34;abcdefghijklmnopqrstuvwxyz&amp;#34;;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; public boolean isPangram(String input) {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; if (input.length() &amp;lt;= 0 || input.isBlank()){&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; return false;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; return (allChars.chars().map(x -&amp;gt; (char) x).filter(x -&amp;gt; input.toLowerCase().indexOf(x) &amp;gt;= 0).count() == 26);&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&#10;&lt;p&gt;All the for loops have been squeezed into a simple line&lt;/p&gt;</description></item><item><title>Java Streams</title><link>https://samarthya.me/posts/java-streams/</link><pubDate>Thu, 12 Nov 2020 11:47:49 +0000</pubDate><guid>https://samarthya.me/posts/java-streams/</guid><description>&lt;p&gt;&lt;em&gt;Stream&lt;/em&gt; is an new abstraction that lets one process data in a declarative way.&lt;/p&gt;&#10;&lt;div class="lead text-neutral-500 dark:text-neutral-400 !mb-9 text-xl"&gt;&#10; &lt;strong&gt;A stream does not store data and, in that sense, is not a data structure. It also never modifies the underlying data source.&lt;/strong&gt;&#10;&lt;/div&gt;&#10;&#10;&lt;p&gt;The easiest way to learn a new API or a construct is by trying it. So here is an &lt;a href="https://github.com/samarthya/javaex/tree/local/grains" target="_blank" rel="noreferrer"&gt;exercise&lt;/a&gt; that I tried to learn the Stream API. This is an exercise from &lt;a href="https://exercism.io/my/solutions/428fcd2c497c4098b5bd3121a1d66110" target="_blank" rel="noreferrer"&gt;exercism.io&lt;/a&gt;.&lt;/p&gt;</description></item></channel></rss>