我正在通过spark结构化流从kafka主题中读取日志行,分离日志行的字段,对字段执行一些操作,并将其存储在数据帧中,并为每个字段存储单独的列。我想将此数据帧写入kafka
下面是我的示例数据帧和将其写入kafka的writestream
val dfStructuredWrite = dfProcessedLogs.select(
dfProcessedLogs("result").getItem("_1").as("col1"),
dfProcessedLogs("result").getItem("_2").as("col2"),
dfProcessedLogs("result").getItem("_17").as("col3"))
dfStructuredWrite
.writeStream
.format("kafka")
.option("kafka.bootstrap.servers", "host1:port1,host2:port2")
.option("topic", "topic1")
.start()
上面的代码给我下面的错误
Required attribute 'value' not found
我相信这是因为我没有键/值格式的数据框。如何以最有效的方式将现有数据框写入kafka?
答案 0 :(得分:0)
要写入Kafka的数据框在模式中应包含以下列:
在您的情况下,没有value
列,并且引发了异常。
您必须对其进行修改以至少添加value列,例如:
import org.apache.spark.sql.functions.{concat, lit}
dfStructuredWrite.select(concat($"col1", lit(" "), $"col2", lit(" "), $"col3").alias("value"))
有关更多详细信息,您可以检查:https://spark.apache.org/docs/latest/structured-streaming-kafka-integration.html#writing-data-to-kafka