Invalid value earliest for configuration auto.commit.interval.ms: Not a number of type INT 以及 Failed to construct kafka consumer
kafka消费者的api操作
代码如下
package com.qf.kafka.day1;
import org.apache.kafka.clients.consumer.*;
import scala.Int;
import java.util.Collections;
import java.util.Properties;
public class KafkaConsumerDemo {
public static void main(String[] args) {
Properties props = new Properties();
props.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "hadoop01:9092,hadoop02:9092,hadoop03:9092");
props.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
props.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
props.setProperty(ConsumerConfig.GROUP_ID_CONFIG, "g2");
props.setProperty(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, "earliest");
//自动提交偏移量,默认true
//当设置为false,consumer不再自动提交offset
// props.setProperty(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,"false");
//报错信息指向的位置
KafkaConsumer consumer = new KafkaConsumer<String, String>(props);
consumer.subscribe(Collections.singletonList("hadoop"));
while (true) {
ConsumerRecords<String, String> consumerRecord = consumer.poll(1000);//consumer会在Kafka停(猫)一秒
for (ConsumerRecord<String, String> record : consumerRecord) {
System.out.println(String.format("key=%s value=%s topic=%s partition=%d offset=%d timestamp=%d"
, record.key(), record.value(), record.topic(), record.partition(), record.offset(), record.timestamp()));
}
}
}
}
(1)报错信息如下
"C:\Program Files\Java\jdk1.8.0_241\bin\java.exe" "-javaagent:D:\大数据\Big--Data软件\IDEA\IntelliJ IDEA 2021.1.1\lib\idea_rt.jar=11555:D:\大数据\Big--Data软件\IDEA\IntelliJ IDEA 2021.1.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_241\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\rt.jar;E:\IDEA__workstation\kafka-2102\target\classes;F:\apache-maven-3.6.3\repository\org\apache\kafka\kafka_2.11\2.3.0\kafka_2.11-2.3.0.jar;F:\apache-maven-3.6.3\repository\org\apache\kafka\kafka-clients\2.3.0\kafka-clients-2.3.0.jar;F:\apache-maven-3.6.3\repository\com\github\luben\zstd-jni\1.4.0-1\zstd-jni-1.4.0-1.jar;F:\apache-maven-3.6.3\repository\org\lz4\lz4-java\1.6.0\lz4-java-1.6.0.jar;F:\apache-maven-3.6.3\repository\org\xerial\snappy\snappy-java\1.1.7.3\snappy-java-1.1.7.3.jar;F:\apache-maven-3.6.3\repository\com\fasterxml\jackson\core\jackson-databind\2.9.9\jackson-databind-2.9.9.jar;F:\apache-maven-3.6.3\repository\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;F:\apache-maven-3.6.3\repository\com\fasterxml\jackson\core\jackson-core\2.9.9\jackson-core-2.9.9.jar;F:\apache-maven-3.6.3\repository\com\fasterxml\jackson\module\jackson-module-scala_2.11\2.9.9\jackson-module-scala_2.11-2.9.9.jar;F:\apache-maven-3.6.3\repository\com\fasterxml\jackson\module\jackson-module-paranamer\2.9.9\jackson-module-paranamer-2.9.9.jar;F:\apache-maven-3.6.3\repository\com\thoughtworks\paranamer\paranamer\2.8\paranamer-2.8.jar;F:\apache-maven-3.6.3\repository\com\fasterxml\jackson\dataformat\jackson-dataformat-csv\2.9.9\jackson-dataformat-csv-2.9.9.jar;F:\apache-maven-3.6.3\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.9.9\jackson-datatype-jdk8-2.9.9.jar;F:\apache-maven-3.6.3\repository\net\sf\jopt-simple\jopt-simple\5.0.4\jopt-simple-5.0.4.jar;F:\apache-maven-3.6.3\repository\com\yammer\metrics\metrics-core\2.2.0\metrics-core-2.2.0.jar;F:\apache-maven-3.6.3\repository\org\scala-lang\scala-library\2.11.12\scala-library-2.11.12.jar;F:\apache-maven-3.6.3\repository\org\scala-lang\scala-reflect\2.11.12\scala-reflect-2.11.12.jar;F:\apache-maven-3.6.3\repository\com\typesafe\scala-logging\scala-logging_2.11\3.9.0\scala-logging_2.11-3.9.0.jar;F:\apache-maven-3.6.3\repository\org\slf4j\slf4j-api\1.7.26\slf4j-api-1.7.26.jar;F:\apache-maven-3.6.3\repository\com\101tec\zkclient\0.11\zkclient-0.11.jar;F:\apache-maven-3.6.3\repository\org\apache\zookeeper\zookeeper\3.4.14\zookeeper-3.4.14.jar;F:\apache-maven-3.6.3\repository\com\github\spotbugs\spotbugs-annotations\3.1.9\spotbugs-annotations-3.1.9.jar;F:\apache-maven-3.6.3\repository\com\google\code\findbugs\jsr305\3.0.2\jsr305-3.0.2.jar;F:\apache-maven-3.6.3\repository\org\apache\yetus\audience-annotations\0.5.0\audience-annotations-0.5.0.jar" com.qf.kafka.day1.KafkaConsumerDemo
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" org.apache.kafka.common.config.ConfigException: Invalid value earliest for configuration auto.commit.interval.ms: Not a number of type INT
at org.apache.kafka.common.config.ConfigDef.parseType(ConfigDef.java:718)
at org.apache.kafka.common.config.ConfigDef.parseValue(ConfigDef.java:473)
at org.apache.kafka.common.config.ConfigDef.parse(ConfigDef.java:466)
at org.apache.kafka.common.config.AbstractConfig.<init>(AbstractConfig.java:108)
at org.apache.kafka.common.config.AbstractConfig.<init>(AbstractConfig.java:129)
at org.apache.kafka.clients.consumer.ConsumerConfig.<init>(ConsumerConfig.java:544)
at org.apache.kafka.clients.consumer.KafkaConsumer.<init>(KafkaConsumer.java:664)
at org.apache.kafka.clients.consumer.KafkaConsumer.<init>(KafkaConsumer.java:644)
at com.qf.kafka.day1.KafkaConsumerDemo.main(KafkaConsumerDemo.java:23)
Process finished with exit code 1
解决办法 粗心导致
props.setProperty(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, "earliest");
改为:
props.setProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); // auto.offset.reset 默认是 latest 当指定的组是一个未知的组,应该从哪个位置开始消费
#(2) Failed to construct kafka consumer 报错信息
"C:\Program Files\Java\jdk1.8.0_241\bin\java.exe" "-javaagent:D:\大数据\Big--Data软件\IDEA\IntelliJ IDEA 2021.1.1\lib\idea_rt.jar=9495:D:\大数据\Big--Data软件\IDEA\IntelliJ IDEA 2021.1.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_241\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_241\jre\lib\rt.jar;E:\IDEA__workstation\kafka-2102\target\classes;F:\apache-maven-3.6.3\repository\org\apache\kafka\kafka_2.11\2.3.0\kafka_2.11-2.3.0.jar;F:\apache-maven-3.6.3\repository\org\apache\kafka\kafka-clients\2.3.0\kafka-clients-2.3.0.jar;F:\apache-maven-3.6.3\repository\com\github\luben\zstd-jni\1.4.0-1\zstd-jni-1.4.0-1.jar;F:\apache-maven-3.6.3\repository\org\lz4\lz4-java\1.6.0\lz4-java-1.6.0.jar;F:\apache-maven-3.6.3\repository\org\xerial\snappy\snappy-java\1.1.7.3\snappy-java-1.1.7.3.jar;F:\apache-maven-3.6.3\repository\com\fasterxml\jackson\core\jackson-databind\2.9.9\jackson-databind-2.9.9.jar;F:\apache-maven-3.6.3\repository\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;F:\apache-maven-3.6.3\repository\com\fasterxml\jackson\core\jackson-core\2.9.9\jackson-core-2.9.9.jar;F:\apache-maven-3.6.3\repository\com\fasterxml\jackson\module\jackson-module-scala_2.11\2.9.9\jackson-module-scala_2.11-2.9.9.jar;F:\apache-maven-3.6.3\repository\com\fasterxml\jackson\module\jackson-module-paranamer\2.9.9\jackson-module-paranamer-2.9.9.jar;F:\apache-maven-3.6.3\repository\com\thoughtworks\paranamer\paranamer\2.8\paranamer-2.8.jar;F:\apache-maven-3.6.3\repository\com\fasterxml\jackson\dataformat\jackson-dataformat-csv\2.9.9\jackson-dataformat-csv-2.9.9.jar;F:\apache-maven-3.6.3\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.9.9\jackson-datatype-jdk8-2.9.9.jar;F:\apache-maven-3.6.3\repository\net\sf\jopt-simple\jopt-simple\5.0.4\jopt-simple-5.0.4.jar;F:\apache-maven-3.6.3\repository\com\yammer\metrics\metrics-core\2.2.0\metrics-core-2.2.0.jar;F:\apache-maven-3.6.3\repository\org\scala-lang\scala-library\2.11.12\scala-library-2.11.12.jar;F:\apache-maven-3.6.3\repository\org\scala-lang\scala-reflect\2.11.12\scala-reflect-2.11.12.jar;F:\apache-maven-3.6.3\repository\com\typesafe\scala-logging\scala-logging_2.11\3.9.0\scala-logging_2.11-3.9.0.jar;F:\apache-maven-3.6.3\repository\org\slf4j\slf4j-api\1.7.26\slf4j-api-1.7.26.jar;F:\apache-maven-3.6.3\repository\com\101tec\zkclient\0.11\zkclient-0.11.jar;F:\apache-maven-3.6.3\repository\org\apache\zookeeper\zookeeper\3.4.14\zookeeper-3.4.14.jar;F:\apache-maven-3.6.3\repository\com\github\spotbugs\spotbugs-annotations\3.1.9\spotbugs-annotations-3.1.9.jar;F:\apache-maven-3.6.3\repository\com\google\code\findbugs\jsr305\3.0.2\jsr305-3.0.2.jar;F:\apache-maven-3.6.3\repository\org\apache\yetus\audience-annotations\0.5.0\audience-annotations-0.5.0.jar" com.qf.kafka.day1.KafkaConsumerDemo
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" org.apache.kafka.common.KafkaException: Failed to construct kafka consumer
at org.apache.kafka.clients.consumer.KafkaConsumer.<init>(KafkaConsumer.java:827)
at org.apache.kafka.clients.consumer.KafkaConsumer.<init>(KafkaConsumer.java:664)
at org.apache.kafka.clients.consumer.KafkaConsumer.<init>(KafkaConsumer.java:644)
at com.qf.kafka.day1.KafkaConsumerDemo.main(KafkaConsumerDemo.java:25)
Caused by: org.apache.kafka.common.KafkaException: org.apache.kafka.common.serialization.StringSerializer is not an instance of org.apache.kafka.common.serialization.Deserializer
at org.apache.kafka.common.config.AbstractConfig.getConfiguredInstance(AbstractConfig.java:372)
at org.apache.kafka.clients.consumer.KafkaConsumer.<init>(KafkaConsumer.java:713)
... 3 more
Process finished with exit code 1
这是包导错了,以上述代码为例
props.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
props.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
改为:
props.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
props.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
|