簡體   English   中英

在Java中將Json Object發送到Kafka主題

[英]Sending Json Object to kafka topic in java

我想將Json Object發送到我的kafka主題,但是我遇到了一些問題

我使用單實例變量作為文件名的pojo,在其中設置文件名並發送給Kafka Topic。

 KafkaJsonSend objSend= new KafkaJsonSend();
            objSend.setFileName(filename);

          //Configure the Producer
            Properties configProperties = new Properties();
            configProperties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"10.10.51.10:9092");
            configProperties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
            configProperties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,JsonSerializer.class);

            Producer<String, JsonNode> producer = new KafkaProducer<String, JsonNode>(configProperties);

            JsonNode  jsonNode = objectMapper.valueToTree(objSend);
            ProducerRecord<String, JsonNode> rec = new 

            ProducerRecord<String, JsonNode>("BlueShifts",jsonNode);
            producer.send(rec);

            producer.close();

但是,當我運行此代碼時,我遇到了異常,該異常不斷記錄在控制台中。

Error: Uncaught error in kafka producer I/O thread

IllegalStateException: No entry found for connection 0

Java中的Kafka錯誤

我也嘗試過春季卡夫卡,但我在控制台中得到了這個

2019-02-04 16:43:13.938  INFO 4432 --- [nio-6020-exec-1]
o.a.k.clients.producer.ProducerConfig    : ProducerConfig values:
        acks = 1
        batch.size = 16384
        block.on.buffer.full = false
        bootstrap.servers = [10.0.2.15:9092]
        buffer.memory = 33554432
        client.id =
        compression.type = none
        connections.max.idle.ms = 540000
        interceptor.classes = null
        key.serializer = class org.apache.kafka.common.serialization.StringSerializer
        linger.ms = 0
        max.block.ms = 60000
        max.in.flight.requests.per.connection = 5
        max.request.size = 1048576
        metadata.fetch.timeout.ms = 60000
        metadata.max.age.ms = 300000
        metric.reporters = []
        metrics.num.samples = 2
        metrics.sample.window.ms = 30000
        partitioner.class = class org.apache.kafka.clients.producer.internals.DefaultPartitioner
        receive.buffer.bytes = 32768
        reconnect.backoff.ms = 50
        request.timeout.ms = 30000
        retries = 0
        retry.backoff.ms = 100
        sasl.jaas.config = null
        sasl.kerberos.kinit.cmd = /usr/bin/kinit
        sasl.kerberos.min.time.before.relogin = 60000
        sasl.kerberos.service.name = null
        sasl.kerberos.ticket.renew.jitter = 0.05
        sasl.kerberos.ticket.renew.window.factor = 0.8
        sasl.mechanism = GSSAPI
        security.protocol = PLAINTEXT
        send.buffer.bytes = 131072
        ssl.cipher.suites = null
        ssl.enabled.protocols = [TLSv1.2, TLSv1.1, TLSv1]
        ssl.endpoint.identification.algorithm = null
        ssl.key.password = null
        ssl.keymanager.algorithm = SunX509
        ssl.keystore.location = null
        ssl.keystore.password = null
        ssl.keystore.type = JKS
        ssl.protocol = TLS
        ssl.provider = null
        ssl.secure.random.implementation = null
        ssl.trustmanager.algorithm = PKIX
        ssl.truststore.location = null
        ssl.truststore.password = null
        ssl.truststore.type = JKS
        timeout.ms = 30000
        value.serializer = class org.springframework.kafka.support.serializer.JsonSerializer

2019-02-04 16:43:14.158  INFO 4432 --- [nio-6020-exec-1]
o.a.kafka.common.utils.AppInfoParser     : Kafka  version : 0.10.2.0
2019-02-04 16:43:14.160  INFO 4432 --- [nio-6020-exec-1]
o.a.kafka.common.utils.AppInfoParser     : Kafka  commitId :
576d93a8dc0cf421 2019-02-04 16:44:14.206 ERROR 4432 ---
[nio-6020-exec-1] o.s.k.support.LoggingProducerListener    : Exception thrown when sending a message with key='null' and
payload='KafkaJsonSend [fileName=a0a7caf336e8481fb
6db2de70d39029e_1549278789987.mp3]' to topic BlueShifts:
org.apache.kafka.common.errors.TimeoutException: Failed to update metadata after 60000 ms.

工作在hosts文件中的C:\\Windows\\System32\\drivers\\etc中添加了10.10.51.10 kafka ,並且它以兩種方式都起作用。...您能說出為什么它與kafka映射了,為什么它無法通過ip找到嗎

您需要為您的網絡正確配置Kafka代理: https : //rmoff.net/2018/08/02/kafka-listeners-explained/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM