public class KafkaConsumer2 {
public static void main(String[] args) {
Properties props = new Properties();
props.put("bootstrap.servers", "127.0.0.1:9092");
props.put("group.id", "one");
props.put("enable.auto.commit", true);
props.put("auto.commit.interval.ms", 5000);
props.put("session.timeout.ms", 50000);
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
//设置使用最开始的offset偏移量为该group.id的最早。如果不设置,则会是latest即该topic最新一个消息的offset
//如果采用latest,消费者只能得道其启动后,生产者生产的消息
// props.put("auto.offset.reset", "earliest");
props.put("auto.offset.reset", "latest");
KafkaConsumer<String, String> consumer = new KafkaConsumer(props);
try {
String topicName = "mykafka";
//重置offset,可同时设置多个topic和partition
Map<TopicPartition, OffsetAndMetadata> hashMaps = new HashMap<TopicPartition, OffsetAndMetadata>();
hashMaps.put(new TopicPartition(topicName, 0), new OffsetAndMetadata(2));
consumer.commitSync(hashMaps);
//启动订阅
consumer.subscribe(Arrays.asList(topicName));
while (true) {
ConsumerRecords<String, String> records = consumer.poll(Long.MAX_VALUE);
for (ConsumerRecord<String, String> record : records) {
String value = record.value();
long offset = record.offset();
int partition = record.partition();
String topic = record.topic();
String f="-------->>>topic=%s,offset=%s,partition=%s,value=%s";
System.out.println(String.format(f,topic,offset,partition,value));
}
consumer.commitSync();
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if(consumer != null){
consumer.close();
}
}
}
}
public class KafkaProudcer2 {
public static void main(String[] args) {
Properties props = new Properties();
props.put("bootstrap.servers", "127.0.0.1:9092");
props.put("retries", 3);
props.put("linger.ms", 1);
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
KafkaProducer<String, String> producer = new KafkaProducer<String, String>(props);
while (true){
String topicName = "mykafka";
ProducerRecord<String, String> record = new ProducerRecord<String, String>(
topicName, System.currentTimeMillis()+"", "今天天气不错哟yoyo=======>");
producer.send(record, new Callback() {
public void onCompletion(RecordMetadata metadata, Exception e) {
if (e != null)
System.out.println("the producer has a error:" + e.getMessage());
else {
String f="----------->>>partition=%s,offset=%s";
System.out.println(String.format(f,metadata.partition(),metadata.offset()));
}
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
// producer.close();
}
}
Kafka消费者重置offset读取数据
点赞
收藏