Solucionado (ver solução)
Solucionado
(ver solução)
2
respostas

o Tipo var não herda os atributos key, value, partition e offset da ConsumerRecords

Estou usando o IntelliJ 2019.3.3 com o Java 12. Quando sigo os passos do vídeo, ele consegue pegar a referencia do record, mas não consegue acessar os atributos que deveriam vir junto (key, value, partition e offset)

for (var record : records) {
            System.out.println("###########################################");
            System.out.println("Processing new order, checking for fraud");
            System.out.println(record.key()); //erro aqui  cannot find symbol, symbol:   method key(), location: variable record of type java.lang.Object
            System.out.println(record.value()); //erro aqui  cannot find symbol, symbol:   method value(), location: variable record of type java.lang.Object
            System.out.println(record.partition()); //erro aqui  cannot find symbol, symbol:   method partition(), location: variable record of type java.lang.Object
            System.out.println(record.offset()); //erro aqui  cannot find symbol, symbol:   method offset(), location: variable record of type java.lang.Object
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Order Processed");
            System.out.println("###########################################");

Tentei outras soluções como

records.foreach( record -> {
System.out.println("###########################################");
            System.out.println("Processing new order, checking for fraud");
            System.out.println(record.key()); //erro aqui  cannot find symbol, symbol:   method key(), location: variable record of type java.lang.Object
            System.out.println(record.value()); //erro aqui  cannot find symbol, symbol:   method value(), location: variable record of type java.lang.Object
            System.out.println(record.partition()); //erro aqui  cannot find symbol, symbol:   method partition(), location: variable record of type java.lang.Object
            System.out.println(record.offset()); //erro aqui  cannot find symbol, symbol:   method offset(), location: variable record of type java.lang.Object
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Order Processed");
            System.out.println("###########################################");
}

Mas todas apresentaram o mesmo erro. Podem me ajudar?

2 respostas

Em uma outra tentativa, fiz

for (ConsumerRecord<String, String> record : records) { // Error:(19, 54) java: incompatible types: java.lang.Object cannot be converted to org.apache.kafka.clients.consumer.ConsumerRecord<java.lang.String,java.lang.String>
            System.out.println("###########################################");
            System.out.println("Processing new order, checking for fraud");
            System.out.println(record.key()); //erro aqui  cannot find symbol, symbol:   method key(), location: variable record of type java.lang.Object
            System.out.println(record.value()); //erro aqui  cannot find symbol, symbol:   method value(), location: variable record of type java.lang.Object
            System.out.println(record.partition()); //erro aqui  cannot find symbol, symbol:   method partition(), location: variable record of type java.lang.Object
            System.out.println(record.offset()); //erro aqui  cannot find symbol, symbol:   method offset(), location: variable record of type java.lang.Object
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Order Processed");
            System.out.println("###########################################");
solução!

Testei com JDK 14 no IntelliJ 2020.1.3 e não tive problemas:

                for (var record : records) {
                    record.key();
                    record.value();
                    parse.consume(record);
                }

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software