1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Projectxmlns= "http://maven.apache.org/POM/4.0.0"3 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4 xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">5 <modelversion>4.0.0</modelversion>6 7 <groupId>Com.ht</groupId>8 <Artifactid>Kafkatest</Artifactid>9 <version>1.0-snapshot</version>Ten <Build> One <Plugins> A <plugin> - <groupId>Org.apache.maven.plugins</groupId> - <Artifactid>Maven-compiler-plugin</Artifactid> the <Configuration> - <Source>1.7</Source> - <Target>1.7</Target> - </Configuration> + </plugin> - </Plugins> + </Build> A at - <Dependencies> - <Dependency> - <groupId>Org.apache.kafka</groupId> - <Artifactid>Kafka-clients</Artifactid> - <version>0.10.0.0</version> in </Dependency> - </Dependencies> to </Project>
Java code
1 ImportOrg.apache.kafka.clients.CommonClientConfigs;2 ImportOrg.apache.kafka.clients.consumer.ConsumerRecord;3 Importorg.apache.kafka.clients.consumer.ConsumerRecords;4 ImportOrg.apache.kafka.clients.consumer.KafkaConsumer;5 6 Importjava.util.Collections;7 Importjava.util.Properties;8 9 Import Staticorg.apache.kafka.clients.consumer.consumerconfig.*;Ten One /** A * @authorSunzq - * @since2017/8/29 - */ the Public classApplication { - Public Static voidMain (string[] args) { - -Properties props =NewProperties (); +Props.put (Bootstrap_servers_config, "node1:6667,node2:6667,node3:6667,node4:6667"); -Props.put (Enable_auto_commit_config, "true"); +Props.put (Group_id_config, "test08291103"); A //props.put (Consumerconfig.client_id_config, "test0829"); atProps.put (Auto_commit_interval_ms_config, 1000); -Props.put (Auto_offset_reset_config, "earliest"); -Props.put (Key_deserializer_class_config, "Org.apache.kafka.common.serialization.StringDeserializer"); -Props.put (Value_deserializer_class_config, "Org.apache.kafka.common.serialization.StringDeserializer"); -Props.put (Commonclientconfigs.security_protocol_config, "Sasl_plaintext"); - inkafkaconsumer<string, string> consumer =NewKafkaconsumer<>(props); - //Topic Name:test9 toConsumer.subscribe (Collections.singleton ("Test9")); + while(true) { -Consumerrecords<string, string> records = Consumer.poll (100); the for(Consumerrecord<string, string>record:records) *System.out.printf ("offset =%d, key =%s, value =%s%n", Record.offset (), Record.key (), Record.value ()); $ }Panax Notoginseng } -}
Startup parameters
-djava.security.krb5.conf=c:\\app\\conf\\krb5.conf-djava.security.auth. login. config=c:\\app\\conf\\kafka_jaas.conf
Under Windows, remember to use \ \
Java access to Kerberos certified Kafka