florin-akermann commented on a change in pull request #11456:
URL: https://github.com/apache/kafka/pull/11456#discussion_r785328925



##########
File path: core/src/main/scala/kafka/tools/ConsoleProducer.scala
##########
@@ -264,39 +278,92 @@ object ConsoleProducer {
     var reader: BufferedReader = null
     var parseKey = false
     var keySeparator = "\t"
+    var parseHeader = false
+    var headersDelimiter = "\t"
+    var headersSeparator = ","
+    var headerKeySeparator = ":"
     var ignoreError = false
     var lineNumber = 0
     var printPrompt = System.console != null
+    var headerSeparatorPattern: Pattern = _
 
     override def init(inputStream: InputStream, props: Properties): Unit = {
       topic = props.getProperty("topic")
       if (props.containsKey("parse.key"))
         parseKey = props.getProperty("parse.key").trim.equalsIgnoreCase("true")
       if (props.containsKey("key.separator"))
         keySeparator = props.getProperty("key.separator")
+      if (props.containsKey("parse.headers"))
+        parseHeader = 
props.getProperty("parse.headers").trim.equalsIgnoreCase("true")
+      if (props.containsKey("headers.delimiter"))
+        headersDelimiter = props.getProperty("headers.delimiter")
+      if (props.containsKey("headers.separator"))
+        headersSeparator = props.getProperty("headers.separator")
+        headerSeparatorPattern = Pattern.compile(headersSeparator)
+      if (props.containsKey("headers.key.separator"))
+        headerKeySeparator = props.getProperty("headers.key.separator")
       if (props.containsKey("ignore.error"))
         ignoreError = 
props.getProperty("ignore.error").trim.equalsIgnoreCase("true")
+      if (headersDelimiter.equals(headersSeparator))
+        throw new KafkaException("headers.delimiter and headers.separator may 
not be equal")
+      if (headersDelimiter.equals(headerKeySeparator))
+        throw new KafkaException("headers.delimiter and headers.key.separator 
may not be equal")
+      if (headersSeparator.equals(headerKeySeparator))
+        throw new KafkaException("headers.separator and headers.key.separator 
may not be equal")
       reader = new BufferedReader(new InputStreamReader(inputStream, 
StandardCharsets.UTF_8))
     }
 
-    override def readMessage() = {
+    override def readMessage(): ProducerRecord[Array[Byte], Array[Byte]] = {
       lineNumber += 1
-      if (printPrompt)
-        print(">")
-      (reader.readLine(), parseKey) match {
-        case (null, _) => null
-        case (line, true) =>
-          line.indexOf(keySeparator) match {
-            case -1 =>
-              if (ignoreError) new ProducerRecord(topic, 
line.getBytes(StandardCharsets.UTF_8))
-              else throw new KafkaException(s"No key found on line 
$lineNumber: $line")
-            case n =>
-              val value = (if (n + keySeparator.size > line.size) "" else 
line.substring(n + keySeparator.size)).getBytes(StandardCharsets.UTF_8)
-              new ProducerRecord(topic, line.substring(0, 
n).getBytes(StandardCharsets.UTF_8), value)
+      if (printPrompt) print(">")
+      val line = reader.readLine()
+      line match {
+        case null => null
+        case line =>
+
+          val headers = parse(parseHeader, line, headersDelimiter, "headers 
delimiter")
+          val key = parse(parseKey, line.substring(offset(headers)), 
keySeparator, "key separator")

Review comment:
       Thanks, I do not follow. Would you prefer to pass the index of the 
starting char as the second parameter for parse(..., index, ... )? In this case 
parse() would need to have acces to 'line'? Which in turn would mean an 
additional parameter to parse()? Moreover, wouldn't this then mean the start 
index needs to passed as well.
   
   An adavantage of this might be a more detailed error messages as the full 
malformed line can be added to the exception instead of just the segment of the 
line and line number.
   
   I guess an other option could be to completley inline parse() at the cost of 
a 'somewhat' redundant match expression.
   
   Anyhow, before we go any further on this: As it is 'just' a 'nit' I dare 
asking whether it is ok to leave as is?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to