- 生产者api
第一个案例 使用的一个消费者
kafka-console-consumer.sh --bootstrap-server qianfeng01:9092,qianfeng02:9092,qianfeng03:9092 --topic food
先运行idea中的生产者,然后再去集群中国运行一个消费者 查看消费者中的消息
package com.qf.kafka;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import java.util.Properties;
public class Producer {
public static void main(String[] args) {
Properties properties = new Properties();
properties.setProperty("bootstrap.servers","qianfeng01:9092,qianfeng02:9092,qianfeng03:9092");
properties.setProperty("key.serializer","org.apache.kafka.common.serialization.StringSerializer");
properties.setProperty("value.serializer","org.apache.kafka.common.serialization.StringSerializer");
//获取一个具体的生产者对象
KafkaProducer kafkaProducer = new KafkaProducer(properties);
ProducerRecord producerRecord = new ProducerRecord("food", "you are best one"); //指定主题和消息内容
//调用send方法发送数据, 数据是ProducerRecord类型
kafkaProducer.send(producerRecord);
//释放资源
kafkaProducer.close();
}
}
在开启集群,打开zookeeper和kafka
在一个节点上 输入
kafka-console-consumer.sh --bootstrap-server qianfeng01:9092,qianfeng02:9092,qianfeng03:9092 --topic food
运行idea中的代码 在 那个消费者节点上查看有没有value
2、消费者的api
针对消费者的案例
首先是敲了一个消费者的代码,直接运行
在虚拟机的生产者那端就敲消息
kafka-console-producer.sh \
--broker-list qianfeng02:9092,qianfeng03:9092,qianfeng03:9092 \
--topic food
kafka-console-consumer.sh \
--bootstrap-server qianfeng01:9092,qianfeng02:9092,qianfeng03:9092 \
--topic food \
--partition 0 \
消费者代码之一:
package com.qf.kafka.consumer;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
public class Kafka_06 {
public static void main(String[] args) {
Properties properties = new Properties();
properties.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"qianfeng01:9092,qianfeng02:9092,qianfeng03:9092");
properties.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringDeserializer");
properties.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringDeserializer");
//消费者在消费数据时,必须指定消费者组属性。
properties.setProperty(ConsumerConfig.GROUP_ID_CONFIG,"g1");
//获取一个消费者对象
KafkaConsumer kafkaConsumer = new KafkaConsumer(properties);
//调用相关方法subscribe来消费主题
List<String> topics = new ArrayList<String>();
topics.add("pet");
topics.add("fruit");
kafkaConsumer.subscribe(topics);
//消费者是主动拉去集群上的消息的
while (true){
ConsumerRecords messages = kafkaConsumer.poll(5000);
Iterator<ConsumerRecord> iterator = messages.iterator();
while (iterator.hasNext()){
ConsumerRecord m = iterator.next();
System.out.println(m.topic()+"\t"+m.partition()+"\t"+m.value());
}
}
}
}
消费者代码之二
package com.qf.kafka.consumer;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.TopicPartition;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
/**
* 消费者进行指定offset消费
*/
public class Kafka_07 {
public static void main(String[] args) {
Properties properties = new Properties();
properties.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"qianfeng01:9092,qianfeng02:9092,qianfeng03:9092");
properties.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringDeserializer");
properties.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringDeserializer");
//消费者在消费数据时,必须指定消费者组属性。
properties.setProperty(ConsumerConfig.GROUP_ID_CONFIG,"g1");
//获取一个消费者对象
KafkaConsumer kafkaConsumer = new KafkaConsumer(properties);
//如果想要定位offset进行消费,必须使用方法assign
List<TopicPartition> tps = new ArrayList<>();
TopicPartition t1 = new TopicPartition("pet",0);
TopicPartition t2 = new TopicPartition("fruit",0);
tps.add(t1);
tps.add(t2);
//指定要分配的主题集合
kafkaConsumer.assign(tps);
//一定要先分配主题,然后再进行offset定位
kafkaConsumer.seek(t1,0);
kafkaConsumer.seek(t2,0);
//消费者是主动拉去集群上的消息的
while (true){
ConsumerRecords messages = kafkaConsumer.poll(5000);
Iterator<ConsumerRecord> iterator = messages.iterator();
while (iterator.hasNext()){
ConsumerRecord m = iterator.next();
System.out.println(m.topic()+"\t"+m.partition()+"\t"+m.value());
}
}
}