[
https://issues.apache.org/jira/browse/KAFKA-7326?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16599673#comment-16599673
]
ASF GitHub Bot commented on KAFKA-7326:
---------------------------------------
guozhangwang closed pull request #5579: KAFKA-7326: KStream.print() should
flush on each line for PrintStream
URL: https://github.com/apache/kafka/pull/5579
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/docs/streams/developer-guide/dsl-api.html
b/docs/streams/developer-guide/dsl-api.html
index beb83a3970a..da1b78dafcb 100644
--- a/docs/streams/developer-guide/dsl-api.html
+++ b/docs/streams/developer-guide/dsl-api.html
@@ -631,6 +631,7 @@ <h2><a class="toc-backref" href="#id7">Overview</a><a
class="headerlink" href="#
caveats.
(<a class="reference external"
href="../../../javadoc/org/apache/kafka/streams/kstream/KStream.html#print--">details</a>)</p>
<p>Calling <code class="docutils literal"><span
class="pre">print()</span></code> is the same as calling <code class="docutils
literal"><span class="pre">foreach((key,</span> <span class="pre">value)</span>
<span class="pre">-></span> <span class="pre">System.out.println(key</span>
<span class="pre">+</span> <span class="pre">",</span> <span
class="pre">"</span> <span class="pre">+</span> <span
class="pre">value))</span></code></p>
+ <p><code class="docutils literal"><span
class="pre">print</span></code> is mainly for debugging/testing purposes, and
it will try to flush on each record print. Hence it <strong>should not</strong>
be used for production usage if performance requirements are concerned.</p>
<div class="last highlight-java"><div
class="highlight"><pre><span></span><span class="n">KStream</span><span
class="o"><</span><span class="kt">byte</span><span class="o">[],</span>
<span class="n">String</span><span class="o">></span> <span
class="n">stream</span> <span class="o">=</span> <span class="o">...;</span>
<span class="c1">// print to sysout</span>
<span class="n">stream</span><span class="o">.</span><span
class="na">print</span><span class="o">();</span>
diff --git
a/streams/src/main/java/org/apache/kafka/streams/kstream/KStream.java
b/streams/src/main/java/org/apache/kafka/streams/kstream/KStream.java
index ae3b28a35ce..cf2ce75450e 100644
--- a/streams/src/main/java/org/apache/kafka/streams/kstream/KStream.java
+++ b/streams/src/main/java/org/apache/kafka/streams/kstream/KStream.java
@@ -359,6 +359,9 @@
/**
* Print the records of this KStream using the options provided by {@link
Printed}
+ * Note that this is mainly for debugging/testing purposes, and it will
try to flush on each record print.
+ * It <em>SHOULD NOT</em> be used for production usage if performance
requirements are concerned.
+ *
* @param printed options for printing
*/
void print(final Printed<K, V> printed);
diff --git
a/streams/src/main/java/org/apache/kafka/streams/kstream/internals/PrintForeachAction.java
b/streams/src/main/java/org/apache/kafka/streams/kstream/internals/PrintForeachAction.java
index 174319fec4b..861dfd3b84e 100644
---
a/streams/src/main/java/org/apache/kafka/streams/kstream/internals/PrintForeachAction.java
+++
b/streams/src/main/java/org/apache/kafka/streams/kstream/internals/PrintForeachAction.java
@@ -51,6 +51,9 @@
public void apply(final K key, final V value) {
final String data = String.format("[%s]: %s", label, mapper.apply(key,
value));
printWriter.println(data);
+ if (!closable) {
+ printWriter.flush();
+ }
}
public void close() {
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> Let KStream.print() to flush on each printed line
> -------------------------------------------------
>
> Key: KAFKA-7326
> URL: https://issues.apache.org/jira/browse/KAFKA-7326
> Project: Kafka
> Issue Type: Improvement
> Components: streams
> Affects Versions: 2.0.0
> Reporter: Guozhang Wang
> Assignee: huxihx
> Priority: Major
> Labels: newbie++
>
> Today, {{KStream.print()}} is implemented as a special "foreach" function as
> below:
> {code}
> @Override
> public void apply(final K key, final V value) {
> final String data = String.format("[%s]: %s", label,
> mapper.apply(key, value));
> printWriter.println(data);
> }
> {code}
> Note that since {{this.printWriter = new PrintWriter(new
> OutputStreamWriter(outputStream, StandardCharsets.UTF_8));}}, without
> flushing the writer we do not guarantee that printed lines are written to the
> underlying `outputStream` in time.
> Since {{KStream.print()}} is mainly for debugging / testing / demoing
> purposes, not for performance, I think it is okay to enforce auto flushing.
> This would include:
> 1. set {{autoFlush}} in the constructor of printWriter.
> 2. document in java-docs of {{KStream.print}} that this is for debug /
> testing purposes only, and it will try to flush on each record print, and
> hence should not be used for production usage if performance requirement is
> key.
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)