demo-SimpleConsumer

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 SimpleConsumer {

public static void main(String[] args)throws Exception {

String topicName ="abcd";

        Properties props =new Properties();

        props.put("bootstrap.servers", "192.168.2.128:9092");

        props.put("group.id", "1");

        props.put("client.id","2");

        props.put("enable.auto.commit", "true");

        props.put("auto.commit.interval.ms", "1000");

        props.put("session.timeout.ms", "30000");

        props.put("key.deserializer",

                "org.apache.kafka.common.serialization.StringDeserializer");

        props.put("value.deserializer",

                "org.apache.kafka.common.serialization.StringDeserializer");

        @SuppressWarnings("resource")

KafkaConsumer consumer =new KafkaConsumer(props);

        consumer.subscribe(Arrays.asList(topicName));

        System.out.println("Subscribed to topic "+ topicName);

        while (true) {

ConsumerRecords records = consumer.poll(100);

            for (ConsumerRecord record : records)

System.out.printf("offset = %d, key = %s, value = %s, creatTime = %d\n",

                        record.offset(), record.key(), record.value(),record.timestamp());

        }

}

}

你可能感兴趣的:(demo-SimpleConsumer)