florin-akermann commented on a change in pull request #11456: URL: https://github.com/apache/kafka/pull/11456#discussion_r763390871
########## File path: core/src/main/scala/kafka/tools/ConsoleProducer.scala ########## @@ -264,38 +282,113 @@ 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 = _ + var headerKeySeparatorPattern: 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") + headerKeySeparatorPattern = Pattern.compile(headerKeySeparator) if (props.containsKey("ignore.error")) ignoreError = props.getProperty("ignore.error").trim.equalsIgnoreCase("true") 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) - } - case (line, false) => - new ProducerRecord(topic, line.getBytes(StandardCharsets.UTF_8)) + if (printPrompt) print(">") + val line = reader.readLine() + try{ + (line, parseKey, parseHeader) match { + case (null, _, _) => null + case (line, true, true) => parseHeaderKeyValue(line) + case (line, true, false) => parseKeyValue(line) + case (line, false, true) => parseHeaderValue(line) + case (line, false, false) => parseValue(line) + } + } catch { + case _: Throwable => onMatchError(line) + } + } + + private def parseHeaderKeyValue(line: String): ProducerRecord[Array[Byte],Array[Byte]] = { + val headerIndex = line.indexOf(headersDelimiter) Review comment: Yes you got it right. I agree, a more precise error message will be useful. Moreover, if the user runs it with ignore.error=true it will be expensive to throw and catch frequently. So if the user sets ignore.error=true we will no longer throw and catch. Instead the last resort is to send the whole line as a value. If ignore.errors=false then we throw a single time with the most precise error message possible. Now I also realise that this principle is applied in the current version on trunk. In short, I'll adjust. Thanks again for the input. -- 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