kafka 0.10.0 producer java代碼實現

來源:互聯網
上載者:User

首先匯入包 將kafka目錄下的libs中的jar包匯入 用maven建立

<dependency>        <groupId>org.apache.kafka</groupId>        <artifactId>kafka-clients</artifactId>        <version>0.10.0.0</version>    </dependency>

寫好properties設定檔
一下為項目結構

#kafka叢集地址bootstrap.servers = 192.168.222.131:9092,192.168.222.130:9092,192.168.222.132:9092,192.168.222.133:9092client.id = testProducerkey.serializer = org.apache.kafka.common.serialization.IntegerSerializervalue.serializer = org.apache.kafka.common.serialization.StringSerializer

然後上代碼

package kafka.producer;import java.io.IOException;import java.util.Properties;import java.util.concurrent.ExecutionException;import org.apache.kafka.clients.producer.Callback;import org.apache.kafka.clients.producer.KafkaProducer;import org.apache.kafka.clients.producer.ProducerRecord;import org.apache.kafka.clients.producer.RecordMetadata;public class ProducerTest extends Thread{     private final KafkaProducer<Integer, String> producer;        private final String topic;        private final Boolean isAsync;/*isAsync同步、非同步*/        public ProducerTest(String topic, Boolean isAsync) {            Properties properties = new Properties();            /*載入設定檔*/            try {properties.load(ProducerTest.class.getClassLoader().getResourceAsStream("conf/kafka.producer.properties"));            } catch (IOException e) {                e.printStackTrace();            }            producer = new KafkaProducer<>(properties);            this.topic = topic;            this.isAsync = isAsync;        }        public void run() {            int messageNo = 1;            while (true) {                String messageStr = "Message_" + messageNo;                long startTime = System.currentTimeMillis();                if (isAsync) { // Send asynchronously                    producer.send(new ProducerRecord<>(topic,                        messageNo,                        messageStr), new DemoCallBack(startTime, messageNo, messageStr));                } else { // Send synchronously                    try {                        producer.send(new ProducerRecord<>(topic,                            messageNo,                            messageStr)).get();                        System.out.println("Sent message: (" + messageNo + ", " + messageStr + ")");                    } catch (InterruptedException | ExecutionException e) {                        e.printStackTrace();                    }                }                ++messageNo;            }        }    }    class DemoCallBack implements Callback {        private final long startTime;        private final int key;        private final String message;        public DemoCallBack(long startTime, int key, String message) {            this.startTime = startTime;            this.key = key;            this.message = message;        }        /**         * @param metadata  The metadata for the record that was sent (i.e. the partition and offset). Null if an error         *                  occurred.         * @param exception The exception thrown during processing of this record. Null if no error occurred.         */        public void onCompletion(RecordMetadata metadata, Exception exception) {            long elapsedTime = System.currentTimeMillis() - startTime;            if (metadata != null) {                System.out.println(                    "message(" + key + ", " + message + ") sent to partition(" + metadata.partition() +                        "), " +                        "offset(" + metadata.offset() + ") in " + elapsedTime + " ms");            } else {                exception.printStackTrace();            }        }}

測試代碼

package kafka.producer;public class Main {    public static void main(String[] args) {        ProducerTest test = new ProducerTest("tp_hw", true);        test.start();    }}

運行結果

message(51215, Message_51215) sent to partition(0), offset(161830) in 3497 msmessage(51216, Message_51216) sent to partition(0), offset(161831) in 3497 msmessage(51217, Message_51217) sent to partition(0), offset(161832) in 3497 msmessage(51218, Message_51218) sent to partition(0), offset(161833) in 3497 msmessage(51219, Message_51219) sent to partition(0), offset(161834) in 3497 msmessage(51220, Message_51220) sent to partition(0), offset(161835) in 3497 msmessage(51221, Message_51221) sent to partition(0), offset(161836) in 3497 msmessage(51222, Message_51222) sent to partition(0), offset(161837) in 3497 msmessage(51223, Message_51223) sent to partition(0), offset(161838) in 3497 msmessage(51224, Message_51224) sent to partition(0), offset(161839) in 3497 msmessage(51225, Message_51225) sent to partition(0), offset(161840) in 3497 msmessage(51226, Message_51226) sent to partition(0), offset(161841) in 3497 msmessage(51227, Message_51227) sent to partition(0), offset(161842) in 3497 msmessage(51228, Message_51228) sent to partition(0), offset(161843) in 3497 ms.............

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.