一丶Kafka应用
鉴于kafka在实际使用时,绝大多数应用场景均为Producer和Consumer的API配合使用,故在此只介绍这两种API操作方法,其它的Connector和Streams还有admin可以视自身情况自行学习。
1.java版
-
实现步骤
-
创建maven项目(done)
-
加入kafka依赖
-
producer push message实现
-
consumer pull message实现
-
效果测试
-
-
加入依赖
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>2.0.0</version>
</dependency>
<!--kafka的日志组件依赖包 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
</dependency>
具体代码:
producer push message
import java.util.Properties;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
importorg.apache.kafka.common.serialization.StringSerializer;
/**
* kafka测试工具类
* @author tianliang
*/
public class KafkaProducerUtil {
// 生产者抽象对象
public KafkaProducer<String, String> producer;
// 传入brokerList,以hostname:port的方式,多个之间用,号隔开
public KafkaProducerUtil(String brokerList) {
Properties props = new Properties();
// 服务器ip:端口号,集群用逗号分隔
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, brokerList);
// key序列化指定类
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,
StringSerializer.class.getName());
// value序列化指定类
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,
StringSerializer.class.getName());
// 生产者对象
producer = new KafkaProducer<String, String>(props);
}
public void close(){
this.producer.close();
}
public static void main(String[] args) {
// 初始化broker列表
String brokerList = "cluster1.hadoop:6667,cluster0.hadoop:6667";
String topic="TestKafka";
// 初始化生产者工具类
KafkaProducerUtil kafkaProducerUtil = new KafkaProducerUtil(brokerList);
// 向test_topic发送hello, kafka
kafkaProducerUtil.producer.send(new ProducerRecord<String, String>(
topic, "hello,李英杰!"));
kafkaProducerUtil.close();
System.out.println("done!");
}
}
comsumer push message实现
import java.util.Arrays;
import java.util.Properties;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafk