[英]Does the partition id of the Kafka Topic to which message was sent maches with partition id of open method in ForeachWriter?
我有一個要求,我需要為每個分區創建一個特定的文件。 我看到發布消息的分區與 hdfs 中的確切文件不對應。 如何在消費者中分配分區?
制作人:
def send(key: String, value: String): Unit = {
val recordMetadataFuture = kafkaProducer.send(new ProducerRecord[String, String](topic, key, value))
try {
val recordMetadata = recordMetadataFuture.get
println("Topic " + recordMetadata.topic + " Offset " + recordMetadata.offset + "Partition " + recordMetadata.partition + " timeStamp" + recordMetadata.timestamp)
} catch {
case ex: Exception => {
ex.printStackTrace
}
}
}
消費者 :
streamedEvents.writeStream.foreach(new ForeachWriter[Message] {
var fSDataOutputStream: FSDataOutputStream = _
var partition: Long = _
def open(partitionId: Long, version: Long): Boolean = {
println("Partition id ::" + partitionId + " version :: " + version)
val configuration = new Configuration();
val hdfs = FileSystem.get(new URI("URI"), configuration);
val path = new Path("Path" + partitionId + ".txt");
val fs = path.getFileSystem(configuration)
if (fs.exists(path)) {
fSDataOutputStream = fs.append(path)
} else {
fSDataOutputStream = fs.create(path)
}
partition = partitionId
true
}
def process(e: Message) = {
val message = e.message
val messageKey = e.messageKey
val partition = e.partition
val offset = e.offset
val eventData = e.getMessage();
var eventMessage = getObjectMapper.readValue(eventData, classOf[EventMessage])
fSDataOutputStream.write(getObjectMapper.writeValueAsString(eventMessage.getBytes)
}
def close(errorOrNull: Throwable): Unit = {
fSDataOutputStream.close()
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.