如何使用 asp.net 将 kafka 消息保存到文件中
How to save kafka messages into a file using asp.net
您好,我已经为 kafka 消费者创建了一个用于接收消息的控制台应用程序。
class Program
{
static void Main(string[] args)
{
string topic = "IDGTestTopic";
Uri uri = new Uri("http://localhost:9092");
var options = new KafkaOptions(uri);
var router = new BrokerRouter(options);
var consumer = new Consumer(new ConsumerOptions(topic, router));
foreach (var message in consumer.Consume())
{
Console.WriteLine(Encoding.UTF8.GetString(message.Value));
//Saving messages in files
string lines = Encoding.UTF8.GetString(message.Value);
System.IO.File.WriteAllText(@"C:\Project\Kafka Research\Kafka_Consumer\Kafka_Consumer\KafkaMessages\Messages.txt", lines);
}
}
}
但它只存储当前消息。如果您看到控制台,则显示所有消息。
但是如果您看到文本文件,它只包含当前消息
如何将所有消息保存在一个文件中?
每次消费一条消息,都会覆盖整个文件:
System.IO.File.WriteAllText
您需要在消费循环之外执行此操作。
对于每封邮件,System.IO.File.WriteAllText
会覆盖文件,因此创建的文件将仅包含最新的邮件。
为了将所有消息保存在一个文件中,您可以将 System.IO.File.WriteAllText
替换为 System.IO.File.AppendAllText
,如下所示:
foreach (var message in consumer.Consume()) {
Console.WriteLine(Encoding.UTF8.GetString(message.Value));
//Saving messages in files
string lines = Encoding.UTF8.GetString(message.Value);
System.IO.File.AppendAllText(@"C:\Project\Kafka Research\Kafka_Consumer\Kafka_Consumer\KafkaMessages\Messages.txt", lines);
}
根据文档,
File.AppendAllText Method (String, String)
Opens a file, appends the specified string to the file, and then
closes the file. If the file does not exist, this method creates a
file, writes the specified string to the file, then closes the file.
和File.WriteAllText Method (String, String)
Creates a new file, writes the specified string to the file, and then
closes the file. If the target file already exists, it is overwritten.
@ Giorgos Myrianthous 你之前的第二选择在某些方面更好。附加到 stringbuilder 并在循环外只写入一次文件很可能比在每个循环中多次通过 IO 快得多。这是我的建议:
StringBuilder linebuilder = new StringBuilder(); //this line outside the loop
foreach (var message in consumer.Consume()) {
Console.WriteLine(Encoding.UTF8.GetString(message.Value));
//Saving messages in files
linebuilder.Append(Encoding.UTF8.GetString(message.Value)); //this line inside the loop
}
System.IO.File.AppendAllText(@"C:\Project\Kafka Research\Kafka_Consumer\Kafka_Consumer\KafkaMessages\Messages.txt", linebuilder.ToString(());
您好,我已经为 kafka 消费者创建了一个用于接收消息的控制台应用程序。
class Program
{
static void Main(string[] args)
{
string topic = "IDGTestTopic";
Uri uri = new Uri("http://localhost:9092");
var options = new KafkaOptions(uri);
var router = new BrokerRouter(options);
var consumer = new Consumer(new ConsumerOptions(topic, router));
foreach (var message in consumer.Consume())
{
Console.WriteLine(Encoding.UTF8.GetString(message.Value));
//Saving messages in files
string lines = Encoding.UTF8.GetString(message.Value);
System.IO.File.WriteAllText(@"C:\Project\Kafka Research\Kafka_Consumer\Kafka_Consumer\KafkaMessages\Messages.txt", lines);
}
}
}
但它只存储当前消息。如果您看到控制台,则显示所有消息。
但是如果您看到文本文件,它只包含当前消息
如何将所有消息保存在一个文件中?
每次消费一条消息,都会覆盖整个文件:
System.IO.File.WriteAllText
您需要在消费循环之外执行此操作。
对于每封邮件,System.IO.File.WriteAllText
会覆盖文件,因此创建的文件将仅包含最新的邮件。
为了将所有消息保存在一个文件中,您可以将 System.IO.File.WriteAllText
替换为 System.IO.File.AppendAllText
,如下所示:
foreach (var message in consumer.Consume()) {
Console.WriteLine(Encoding.UTF8.GetString(message.Value));
//Saving messages in files
string lines = Encoding.UTF8.GetString(message.Value);
System.IO.File.AppendAllText(@"C:\Project\Kafka Research\Kafka_Consumer\Kafka_Consumer\KafkaMessages\Messages.txt", lines);
}
根据文档,
File.AppendAllText Method (String, String)
Opens a file, appends the specified string to the file, and then closes the file. If the file does not exist, this method creates a file, writes the specified string to the file, then closes the file.
和File.WriteAllText Method (String, String)
Creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten.
@ Giorgos Myrianthous 你之前的第二选择在某些方面更好。附加到 stringbuilder 并在循环外只写入一次文件很可能比在每个循环中多次通过 IO 快得多。这是我的建议:
StringBuilder linebuilder = new StringBuilder(); //this line outside the loop
foreach (var message in consumer.Consume()) {
Console.WriteLine(Encoding.UTF8.GetString(message.Value));
//Saving messages in files
linebuilder.Append(Encoding.UTF8.GetString(message.Value)); //this line inside the loop
}
System.IO.File.AppendAllText(@"C:\Project\Kafka Research\Kafka_Consumer\Kafka_Consumer\KafkaMessages\Messages.txt", linebuilder.ToString(());