深入浅出Kafka
"深入浅出Kafka" 是一个非常广泛的主题,因为Kafka是一个复杂且功能丰富的系统。这里我会提供一些基本概念和示例代码,帮助你入门Kafka。
安装和配置Kafka:
确保你已经安装了Java,然后从Apache Kafka官网下载并解压Kafka。修改配置文件
config/server.properties
,之后启动Kafka服务器。- 创建一个Kafka主题:
bin/kafka-topics.sh --create --topic test --partitions 1 --replication-factor 1 --bootstrap-server localhost:9092
- 启动Kafka生产者客户端,发送消息:
bin/kafka-console-producer.sh --topic test --bootstrap-server localhost:9092
>Hello, Kafka!
- 启动Kafka消费者客户端,接收消息:
bin/kafka-console-consumer.sh --topic test --from-beginning --bootstrap-server localhost:9092
- 示例代码:使用Kafka Producer API发送消息
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import java.util.Properties;
public class KafkaProducerExample {
public static void main(String[] args) {
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("acks", "all");
props.put("retries", 0);
props.put("batch.size", 16384);
props.put("linger.ms", 1);
props.put("buffer.memory", 33554432);
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
Producer<String, String> producer = new KafkaProducer<>(props);
for (int i = 0; i < 10; i++)
producer.send(new ProducerRecord<String, String>("test", Integer.toString(i), "Hello, Kafka!" + i));
producer.close();
}
}
- 示例代码:使用Kafka Consumer API接收消息
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import java.util.Arrays;
import java.util.Properties;
public class KafkaConsumerExample {
public static void main(String[] args) {
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put
评论已关闭