Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache kafka Sarama生产至压实主题未压实_Apache Kafka_Sarama - Fatal编程技术网

Apache kafka Sarama生产至压实主题未压实

Apache kafka Sarama生产至压实主题未压实,apache-kafka,sarama,Apache Kafka,Sarama,高层问题 我在本地运行卡夫卡,并使用压缩主题。当我运行命令行producer和consumer时,我可以验证是否正在进行压缩,但当我使用sarama(“github.com/Shopify/sarama”)producer时,似乎不会进行日志压缩 验证日志压缩 首先,我使用以下命令创建了一个主题: bin/kafka-topics.sh --zookeeper localhost:2181 \ --create --topic andrew.topic \ --config "clean

高层问题

我在本地运行卡夫卡,并使用压缩主题。当我运行命令行producer和consumer时,我可以验证是否正在进行压缩,但当我使用sarama(“github.com/Shopify/sarama”)producer时,似乎不会进行日志压缩

验证日志压缩

首先,我使用以下命令创建了一个主题:

bin/kafka-topics.sh --zookeeper localhost:2181 \
  --create --topic andrew.topic \
  --config "cleanup.policy=compact" \
  --config "delete.retention.ms=100" \
  --config "segment.ms=100" \
  --config "min.cleanable.dirty.ratio=0.01" \
  --partitions 1 \
  --replication-factor 1
接下来,我将使用以下命令向其发送几条消息:

  for i in $(seq 0 10); do \
  echo "sameKey123:differentMessage$i" | bin/kafka-console-producer.sh \
  --broker-list localhost:9091 \
  --topic andrew.topic \
  --property "parse.key=true" \
  --property "key.separator=:"; \
done
最后验证是否发生了日志压缩:

bin/kafka-console-consumer.sh --bootstrap-server localhost:9091 \
  --topic andrew.topic \
  --property print.key=true \
  --property key.separator=" : " \
  --from-beginning
其中打印:

sameKey123 : differentMessage9
sameKey123 : differentMessage10
因此,andrew.topic主题的日志压缩正在进行

现在使用Sarama

现在,我使用sarama生成指向同一主题的消息,如下所示:

package main

import (
    "fmt"
    "github.com/Shopify/sarama"
    "os"
    "os/signal"
)

func main() {
    sendMessages()

}

func sendMessages() {
    producer, err := sarama.NewSyncProducer([]string{"localhost:9091"}, nil)
    if err != nil {
        panic(err)
    }
    defer func() {
        if err := producer.Close(); err != nil {
            panic(err)
        }
    }()

    for i := 0; i <= 10; i++ {
        pm := &sarama.ProducerMessage{
            Topic: "andrew.topic",
            Key:   sarama.StringEncoder("sameSaramaKey123"),
            Value: sarama.StringEncoder(fmt.Sprintf("differentMessage%v", i)),
        }
        _, _, err := producer.SendMessage(pm)
        if err != nil {
            panic(err)
        }
    }
}
此处未进行原木压实。无论我重新启动消费者多少次,或者使用sarama日志压缩生成多少条消息,似乎都不会发生

更多的古怪

如果在使用sarama生成消息后,我在命令行生成更多消息,则会发生日志压缩

在运行terminal producer之后,在运行sarama producer之后,我得到以下输出

sameSaramaKey123 : differentMessage10
sameKey123 : differentMessage9
sameKey123 : differentMessage10
在终端运行producer后,所有消息(包括之前由sarama生成的消息)都会进行日志压缩


为什么会这样?我如何解决它?

我在处理保留期时遇到了类似的问题。保留期不包括活动段,仅对旧段有效。我想压实也是一样的。您可以将
segment.ms
segment.byte
调整为较低的值,以强制更频繁地进行分段。当卡夫卡收到一条新消息并且超出了其中任何一个属性时,卡夫卡将创建一个新的段。您通过生成更多消息描述的行为是正确的。在上面的示例中,我在创建topic.yes时将segment.ms设置为100。你所经历的是正常的行为。当卡夫卡主题收到消息时,就会发生分段。卡夫卡没有定期计划程序来检查分段,它需要被触发。
sameSaramaKey123 : differentMessage10
sameKey123 : differentMessage9
sameKey123 : differentMessage10