Database Reference
In-Depth Information
recompile the project. In this simple example, the Properties object is
constructed. These Properties are then passed to the constructor of the
ProducerConfig object:
public static void main(String[] args) throws
Exception {
Properties props = new Properties();
props.put("metadata.broker.list",
"localhost:6001,localhost:6002,localhost:6003");
props.put("serializer.class",
"kafka.serializer.StringEncoder");
props.put("request.required.acks", "1");
ProducerConfig config = new ProducerConfig(props);
This configuration object is then passed on to the Producer object, which
takes a type for both its Key and its Message type. Although the Key is
generally optional, it is important that the serializer.class defined in
the configuration properties can successfully encode the objects used for the
Key and the Message . In this case, Strings are being used for both Key and
Message :
String topic = args[0];
Producer<String,String> producer = new
Producer<String,String>(config);
This simple example reads lines from the console and sends them to Kafka.
The Producer itself takes KeyedMessage objects either singly or as part of
a List collection. These messages are constructed with a topic, a key, and a
message. If the key is not required, only the topic and the message can be
used as in this example:
BufferedReader reader = new BufferedReader(
new InputStreamReader(System. in ));
while (reader.ready()) {
String toSend = reader.readLine();
producer.send( new KeyedMessage<String,String>(topic,
toSend));
}
Search WWH ::




Custom Search