I am using the below code to produce Avro records of User
class into Kafka topic, and it is working fine;
Sender class
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.io.DecoderFactory;
import org.apache.avro.io.EncoderFactory;
import org.apache.avro.reflect.ReflectData;
import org.apache.avro.reflect.ReflectDatumWriter;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Properties;
import vo.User;
public class Sender8 {
public static void main(String[] args) {
User user = new User(10,"testName");
Schema schema = ReflectData.get().getSchema(user.getClass());
new GenericData.Record(schema);
Properties props = new Properties();
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "127.0.0.1:9092");
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,io.confluent.kafka.serializers.KafkaAvroSerializer.class);
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,io.confluent.kafka.serializers.KafkaAvroSerializer.class);
props.put("schema.registry.url", "http://127.0.0.1:8081");
KafkaProducer<String, GenericRecord> producer = new KafkaProducer<String, GenericRecord>(props);
ReflectDatumWriter<Object> reflectDatumWriter = new ReflectDatumWriter<>(schema);
GenericDatumReader<Object> genericRecordReader = new GenericDatumReader<>(schema);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
try {
reflectDatumWriter.write(user, EncoderFactory.get().directBinaryEncoder(bytes, null));
GenericRecord avroRecord2 = (GenericRecord) genericRecordReader.read(null, DecoderFactory.get().binaryDecoder(bytes.toByteArray(), null));
ProducerRecord<String, GenericRecord> record = new ProducerRecord<String, GenericRecord>("avrotesttopic1", avroRecord2);
producer.send(record);
producer.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
producer.close();
}
}
User class
public class User {
int id;
String name;
public User(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Sometimes, I might need to send a collection of objects as an arraylist, like;
ArrayList<User> users = new ArrayList<User>();
in this situation, what I does is, creates a loop to iterate through the list, pick individual records, and call send()
method, like;
Iterator iter = users.iterator();
while (iter.hasNext()) {
user = iter.next();
//all other stuff here
producer.send(record);
}
This works fine. But the problem is, if my arraylist has 50 records, producer.send(record)
will be triggered 50 times. I would like to know if there is any other more efficient method to handle this, like calling sender only once, for all 50 records.