Apache kafka 在卡夫卡中,如何从生产者到消费者消费对象?

Apache kafka 在卡夫卡中,如何从生产者到消费者消费对象?,apache-kafka,kafka-consumer-api,kafka-producer-api,Apache Kafka,Kafka Consumer Api,Kafka Producer Api,以下是生产者和消费者的课程。当我能够生成数据而不能使用以下代码使用它时。有人能帮我吗?我在编码上有没有做错什么?我的目标是从消费者那里读取CustomMessage对象,并将数据存储在DB中 在我的cmd提示符中,我打开了5个实例:1个用于zookeeper,1个用于kafka,1个用于producer,1个用于consumer。我真的不明白。当我运行producer和consumer类时,是否需要保持所有实例的运行 这里的任何提示都会非常有用 提前谢谢 producer class:::

以下是生产者和消费者的课程。当我能够生成数据而不能使用以下代码使用它时。有人能帮我吗?我在编码上有没有做错什么?我的目标是从消费者那里读取
CustomMessage
对象,并将数据存储在DB中

在我的cmd提示符中,我打开了5个实例:1个用于zookeeper,1个用于kafka,1个用于producer,1个用于consumer。我真的不明白。当我运行producer和consumer类时,是否需要保持所有实例的运行

这里的任何提示都会非常有用

提前谢谢

producer class:::
            
    package com.kafka.test.demo;
    
    import java.io.IOException;
    import java.util.Properties;
    
    import javax.xml.parsers.ParserConfigurationException;
    
    import org.apache.kafka.clients.producer.KafkaProducer;
    import org.apache.kafka.clients.producer.ProducerRecord;
    import org.xml.sax.SAXException;
    
    public class KafkaaProducer {
        public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
            Properties props = new Properties();
//customMessage is a pojo object which should be send to the consumer..
            CustomMessage  customMessage= new CustomMessage();
            customMessage.setMessage("hello kafka");
            customMessage.setFan("1234213123");
            customMessage.setSourceSystem("Dmap");
            customMessage.setStatus("Unenrolled");
            customMessage.setMessageTyep("Simple Message");
            customMessage.setCreatedTime("5");
            customMessage.setProcessedTime("6");
            customMessage.setRetryCount("3");
            props.put("metadata.broker.list", "localhost:9092");
            props.put("serializer.class", "kafka.serializer.StringEncoder");
            props.put("request.required.acks", "1");
            props.put("bootstrap.servers", "localhost:9092,localhost:9093");
            //CustomMessageSerializer
props.put("key.serializer","com.kafka.test.demo.CustomMessageSerializer"); 
            props.put("value.serializer", "com.kafka.test.demo.CustomMessageSerializer");
            try {
                KafkaProducer<String, CustomMessage> producer = new KafkaProducer<String, CustomMessage>(props);
                producer.send(new ProducerRecord<String, CustomMessage>("NewMessageTopic", "customMessage",customMessage));
                //producer.send(new ProducerRecord<String, CustomMessage>("NewMessageTopic", customMessage));
                System.out.println("Message " + "" + " sent !!");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    consumer class::
    package com.kafka.test.demo;
    
    import java.net.UnknownHostException;
    import java.util.Collections;
    import java.util.Properties;
    
    import org.apache.kafka.clients.consumer.ConsumerRecord;
    import org.apache.kafka.clients.consumer.ConsumerRecords;
    import org.apache.kafka.clients.consumer.KafkaConsumer;
    
    import com.mongodb.BasicDBObject;
    import com.mongodb.DB;
    import com.mongodb.DBCollection;
    import com.mongodb.DBObject;
    import com.mongodb.MongoClient;
    
    public class KafkaaConsumer {
        public static void main(String[] args) throws InterruptedException {
            Properties props = new Properties();
            props.put("zookeeper.connect", "localhost:2181");
            props.put("group.id", "testgroup");
            props.put("zookeeper.session.timeout.ms", "4000");
            props.put("zookeeper.sync.time.ms", "300");
            props.put("rebalance.backoff.ms", "40000");
            props.put("bootstrap.servers", "localhost:9092,localhost:9093");
            props.put("value.deserializer", "com.kafka.test.demo.CustomMessageDeserializer");
            props.put("key.deserializer", "com.kafka.test.demo.CustomMessageDeserializer");
            //perisitMessage();
            try{
                KafkaConsumer<String,CustomMessage> consumer = new KafkaConsumer<String, CustomMessage>(props);
                consumer.subscribe(Collections.singletonList("NewMessageTopic"));
                while (true) {
                    ConsumerRecords<String, CustomMessage> messages = consumer.poll(100);
                    for (ConsumerRecord<String, CustomMessage> message : messages) {
                      System.out.println("Message received " + message);
                    }
                  perisitMessage();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        private static void perisitMessage() {
            // TODO Auto-generated method stub
            CustomMessage  customMessage = new CustomMessage();
            customMessage.setMessage("hello kafka");
            customMessage.setFan("1234213123");
            customMessage.setSourceSystem("Dmap");
            customMessage.setStatus("Unenrolled");
            customMessage.setMessageTyep("Simple Message");
            customMessage.setCreatedTime("5");
            customMessage.setProcessedTime("6");
            customMessage.setRetryCount("3");
            try {
                 MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
                 DB db = mongoClient.getDB("DeviceTrack");
                 DBCollection msgCollection = db.getCollection("messages");
                 BasicDBObject document = new BasicDBObject();
                 document.put("message", customMessage.getMessage());
                 document.put("fan", customMessage.getFan());
                 document.put("SourceSystem", customMessage.getSourceSystem());
                 document.put("RetryCount", customMessage.getRetryCount());
                 document.put("ProcessedTime", customMessage.getProcessedTime());
                 document.put("CreatedTime", customMessage.getCreatedTime());
                 document.put("MessageTyep", customMessage.getMessageTyep());
                 document.put("Status", customMessage.getStatus());
                 msgCollection.insert(document);
                 System.out.println("Inserted in the data in DB succesfully");
    
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    
package com.kafka.test.demo;

import java.util.Map;

import org.apache.kafka.common.serialization.Deserializer;

import com.fasterxml.jackson.databind.ObjectMapper;

public class CustomMessageDeserializer implements Deserializer {

    public Object deserialize(String arg0, byte[] arg1) {
        ObjectMapper mapper = new ObjectMapper();
        System.out.println("arg1"+arg1);
        CustomMessage message = null;
        try {
            message = mapper.readValue(arg1, CustomMessage.class);
        } catch (Exception e) {

            e.printStackTrace();
        }
        System.out.println(""+message);
        return message;
    }



    public void close() {
        // TODO Auto-generated method stub

    }

    public void configure(Map arg0, boolean arg1) {
        // TODO Auto-generated method stub

    }

}
        
    package com.kafka.test.demo;
    
    import java.util.Map;
    
    import org.apache.kafka.common.serialization.Serializer;
    
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    public class CustomMessageSerializer implements Serializer {
    
        public byte[] serialize(String arg0, Object arg1) {
            byte[] retVal = null;
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                retVal = objectMapper.writeValueAsString(arg1).getBytes();
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("value ::::::"+retVal);
            return retVal;
        }
    
        public void close() {
            // TODO Auto-generated method stub
    
        }
    
        public void configure(Map arg0, boolean arg1) {
            // TODO Auto-generated method stub
    
        }
    }
    
package com.kafka.test.demo;

public class CustomMessage {
    
    private String messageId;
    private String parentMsgId;
    private String fan;
    private String message;
    private String sourceSystem;
    private String status;
    private String messageTyep;
    private String createdTime;
    private String processedTime;
    private String retryCount;
    
    /**
     * @return the messageId
     */
    public String getMessageId() {
        return messageId;
    }
    /**
     * @param messageId the messageId to set
     */
    public void setMessageId(String messageId) {
        this.messageId = messageId;
    }
    /**
     * @return the parentMsgId
     */
    public String getParentMsgId() {
        return parentMsgId;
    }
    /**
     * @param parentMsgId the parentMsgId to set
     */
    public void setParentMsgId(String parentMsgId) {
        this.parentMsgId = parentMsgId;
    }
    /**
     * @return the fan
     */
    public String getFan() {
        return fan;
    }
    /**
     * @param fan the fan to set
     */
    public void setFan(String fan) {
        this.fan = fan;
    }
    /**
     * @return the message
     */
    public String getMessage() {
        return message;
    }
    /**
     * @param message the message to set
     */
    public void setMessage(String message) {
        this.message = message;
    }
    /**
     * @return the sourceSystem
     */
    public String getSourceSystem() {
        return sourceSystem;
    }
    /**
     * @param sourceSystem the sourceSystem to set
     */
    public void setSourceSystem(String sourceSystem) {
        this.sourceSystem = sourceSystem;
    }
    /**
     * @return the status
     */
    public String getStatus() {
        return status;
    }
    /**
     * @param status the status to set
     */
    public void setStatus(String status) {
        this.status = status;
    }
    /**
     * @return the messageTyep
     */
    public String getMessageTyep() {
        return messageTyep;
    }
    /**
     * @param messageTyep the messageTyep to set
     */
    public void setMessageTyep(String messageTyep) {
        this.messageTyep = messageTyep;
    }
    /**
     * @return the createdTime
     */
    public String getCreatedTime() {
        return createdTime;
    }
    /**
     * @param createdTime the createdTime to set
     */
    public void setCreatedTime(String createdTime) {
        this.createdTime = createdTime;
    }
    /**
     * @return the processedTime
     */
    public String getProcessedTime() {
        return processedTime;
    }
    /**
     * @param processedTime the processedTime to set
     */
    public void setProcessedTime(String processedTime) {
        this.processedTime = processedTime;
    }
    /**
     * @return the retryCount
     */
    public String getRetryCount() {
        return retryCount;
    }
    /**
     * @param retryCount the retryCount to set
     */
    public void setRetryCount(String retryCount) {
        this.retryCount = retryCount;
    }
}
producer类::
包com.kafka.test.demo;
导入java.io.IOException;
导入java.util.Properties;
导入javax.xml.parsers.parserConfiguration异常;
导入org.apache.kafka.clients.producer.KafkaProducer;
导入org.apache.kafka.clients.producer.ProducerRecord;
导入org.xml.sax.SAXException;
公共级卡夫卡制作人{
公共静态void main(字符串[]args)抛出ParserConfiguration异常、SAXException、IOException{
Properties props=新属性();
//customMessage是应发送给使用者的pojo对象。。
CustomMessage CustomMessage=新建CustomMessage();
setMessage(“你好,卡夫卡”);
customMessage.setFan(“1234213123”);
customMessage.setSourceSystem(“Dmap”);
customMessage.setStatus(“未滚动”);
setMessageTyep(“简单消息”);
customMessage.setCreatedTime(“5”);
customMessage.setProcessedTime(“6”);
customMessage.setRetryCount(“3”);
put(“metadata.broker.list”,“localhost:9092”);
put(“serializer.class”、“kafka.serializer.StringEncoder”);
道具放置(“请求.要求.确认”,“1”);
put(“bootstrap.servers”,“localhost:9092,localhost:9093”);
//CustomMessageSerializer
put(“key.serializer”、“com.kafka.test.demo.CustomMessageSerializer”);
put(“value.serializer”、“com.kafka.test.demo.CustomMessageSerializer”);
试一试{
卡夫卡制作人=新卡夫卡制作人(道具);
producer.send(newproducerrecord(“NewMessageTopic”、“customMessage”、customMessage));
//producer.send(newproducerrecord(“NewMessageTopic”,customMessage));
System.out.println(“消息”+“+”已发送!!”;
}捕获(例外e){
e、 printStackTrace();
}
}
}
消费者类别::
包com.kafka.test.demo;
导入java.net.UnknownHostException;
导入java.util.Collections;
导入java.util.Properties;
导入org.apache.kafka.clients.consumer.ConsumerRecord;
导入org.apache.kafka.clients.consumer.ConsumerRecords;
导入org.apache.kafka.clients.consumer.KafkaConsumer;
导入com.mongodb.BasicDBObject;
导入com.mongodb.DB;
导入com.mongodb.DBCollection;
导入com.mongodb.DBObject;
导入com.mongodb.MongoClient;
公共类卡夫卡{
公共静态void main(字符串[]args)引发InterruptedException{
Properties props=新属性();
props.put(“zookeeper.connect”,“localhost:2181”);
props.put(“group.id”、“testgroup”);
props.put(“zookeeper.session.timeout.ms”,“4000”);
道具放置(“zookeeper.sync.time.ms”,“300”);
道具。放置(“再平衡。后退。ms”,“40000”);
put(“bootstrap.servers”,“localhost:9092,localhost:9093”);
put(“value.deserializer”、“com.kafka.test.demo.CustomMessageDeserializer”);
put(“key.deserializer”、“com.kafka.test.demo.CustomMessageDeserializer”);
//perisitMessage();
试一试{
卡夫卡消费者=新卡夫卡消费者(道具);
consumer.subscribe(Collections.singletonList(“NewMessageTopic”);
while(true){
ConsumerRecords messages=consumer.poll(100);
用于(消费者记录消息:消息){
System.out.println(“消息接收”+消息);
}
perisitMessage();
}
}捕获(例外e){
e、 printStackTrace();
}
}
私有静态void perisitMessage(){
//TODO自动生成的方法存根
CustomMessage CustomMessage=新建CustomMessage();
setMessage(“你好,卡夫卡”);
customMessage.setFan(“1234213123”);
customMessage.setSourceSystem(“Dmap”);
customMessage.setStatus(“未滚动”);
setMessageTyep(“简单消息”);
customMessage.setCreatedTime(“5”);
customMessage.setProcessedTime(“6”);
customMessage.setRetryCount(“3”);
试一试{
MongoClient MongoClient=新的MongoClient(“本地主机”,27017);
DB=mongoClient.getDB(“DeviceTrack”);
DBCollection msgCollection=db.getCollection(“消息”);
BasicDBObject文档=新的BasicDBObject();
document.put(“message”,customMessage.getMessage());
document.put(“fan”,customMessage.getFan());
document.put(“SourceSystem”,customMessage.getSourceSystem());
put(“RetryCount”,customMessage.getRetryCount());
put(“ProcessedTime”,customMessage.getProcessedTime());
document.put(“CreatedTime”,customMessage.getCreatedTime());
put(“MessageTyep”,customMessage.getMessageTyep());
document.put(“状态”),customMessage.getSta
 :startZK
echo Zookeeper wird gestartet
Start "Zookeper" C:\zookeeper-3.4.9\bin\zkServer.cmd
echo Bitte warten bis Zookeeper gestartet ist.
pause
echo Kafka Wird Gestartet
Start "Kafka" C:\kafka_2.11-0.10.2.0\bin\windows\kafka-server-start.bat C:\kafka_2.11-0.10.2.0\config\server.properties

goto Top
            while (true) {
                ConsumerRecords<String, CustomMessage> messages = consumer.poll(100);
                for (ConsumerRecord<String, CustomMessage> message : messages) {
                  System.out.println("Message received " + message);<-- just a syso not more :/
                }
              perisitMessage(); <-- maybe give him the message ?
            }