title: Kafka streams: how to produce to a topic while aggregating?
body: <p>I currently have some code that builds a KTable using aggregate:</p>
  <pre><code>inputTopic.groupByKey().aggregate(
      Aggregator::new,
      (key, value, aggregate) -&gt; {
          someProcessingDoneHere;
          return aggregate;
      },
      Materialized.with(Serdes.String(), Serdes.String())
  );
          
  </code></pre>
  <p>Once a given number of messages have been received and aggregated for a single key, I would like to push the latest aggregation state to another topic and then delete the key in the table.</p>
  <p>I can obviously use a plain Kafka producer and have something like:</p>
  <pre><code>inputTopic.groupByKey().aggregate(
      Aggregator::new,
      (key, value, aggregate) -&gt; {
          someProcessingDoneHere;
          if (count &gt; threshold) {
                  producer.send(new ProducerRecord&lt;String,String&gt;(&quot;output-topic&quot;, 
                      key, aggregate));
                  return null;
          }
          return aggregate;
      },
      Materialized.with(Serdes.String(), Serdes.String())
  );
          
  </code></pre>
  <p>But I'm looking for a more &quot;Stream&quot; approach.</p>
  <p>Any hint ?</p>