您好,登錄后才能下訂單哦!
[TOC]
下面的測試代碼使用的都是下面的topic:
$ kafka-topics.sh --describe hadoop --zookeeper uplooking01:2181,uplooking02:2181,uplooking03:2181
Topic:hadoop PartitionCount:3 ReplicationFactor:3 Configs:
Topic: hadoop Partition: 0 Leader: 103 Replicas: 103,101,102 Isr: 103,101,102
Topic: hadoop Partition: 1 Leader: 101 Replicas: 101,102,103 Isr: 101,102,103
Topic: hadoop Partition: 2 Leader: 102 Replicas: 102,103,101 Isr: 102,103,101
關于producer API的使用說明,可以查看org.apache.kafka.clients.producer.KafkaProducer
這個類的代碼注釋,有非常詳細的說明,下面就直接給出程序代碼及測試。
package com.uplooking.bigdata.kafka.producer;
import com.uplooking.bigdata.kafka.constants.Constants;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Random;
/**
* 通過這個KafkaProducerOps向Kafka topic中生產相關的數據
* <p>
* Producer
*/
public class KafkaProducerOps {
public static void main(String[] args) throws IOException {
/**
* 專門加載配置文件
* 配置文件的格式:
* key=value
*
* 在代碼中要盡量減少硬編碼
* 不要將代碼寫死,要可配置化
*/
Properties properties = new Properties();
InputStream in = KafkaProducerOps.class.getClassLoader().getResourceAsStream("producer.properties");
properties.load(in);
/**
* 兩個泛型參數
* 第一個泛型參數:指的就是kafka中一條記錄key的類型
* 第二個泛型參數:指的就是kafka中一條記錄value的類型
*/
String[] girls = new String[]{"姚慧瑩", "劉向前", "周 新", "楊柳"};
Producer<String, String> producer = new KafkaProducer<String, String>(properties);
String topic = properties.getProperty(Constants.KAFKA_PRODUCER_TOPIC);
String key = "1";
String value = "今天的姑娘們很美";
ProducerRecord<String, String> producerRecord =
new ProducerRecord<String, String>(topic, key, value);
producer.send(producerRecord);
producer.close();
}
}
package com.uplooking.bigdata.kafka.constants;
public interface Constants {
/**
* 生產的key對應的常量
*/
String KAFKA_PRODUCER_TOPIC = "producer.topic";
}
############################# Producer Basics #############################
# list of brokers used for bootstrapping knowledge about the rest of the cluster
# format: host1:port1,host2:port2 ...
bootstrap.servers=uplooking01:9092,uplooking02:9092,uplooking03:9092
# specify the compression codec for all data generated: none, gzip, snappy, lz4
compression.type=none
# name of the partitioner class for partitioning events; default partition spreads data randomly
# partitioner.class=
# the maximum amount of time the client will wait for the response of a request
#request.timeout.ms=
# how long `KafkaProducer.send` and `KafkaProducer.partitionsFor` will block for
#max.block.ms=
# the producer will wait for up to the given delay to allow other records to be sent so that the sends can be batched together
#linger.ms=
# the maximum size of a request in bytes
#max.request.size=
# the default batch size in bytes when batching multiple records sent to a partition
#batch.size=
# the total bytes of memory the producer can use to buffer records waiting to be sent to the server
#buffer.memory=
#####設置自定義的topic
producer.topic=hadoop
key.serializer=org.apache.kafka.common.serialization.StringSerializer
value.serializer=org.apache.kafka.common.serialization.StringSerializer
其實這個配置文件就是kafka conf目錄下的配置文件,只是這里要做相應的修改,關于每個字段的含義,可以查看org.apache.kafka.clients.producer.KafkaProducer
這個類的代碼注釋。
在終端中啟動消費者監聽topic的消息:
[uplooking@uplooking02 ~]$ kafka-console-consumer.sh --topic hadoop --zookeeper uplooking01:2181
然后執行生產者程序,再查看終端輸出:
[uplooking@uplooking02 ~]$ kafka-console-consumer.sh --topic hadoop --zookeeper uplooking01:2181
今天的姑娘們很美
package com.uplooking.bigdata.kafka.consumer;
import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.Properties;
public class KafkaConsumerOps {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
InputStream in = KafkaConsumerOps.class.getClassLoader().getResourceAsStream("consumer.properties");
properties.load(in);
Consumer<String, String> consumer = new KafkaConsumer<String, String>(properties);
Collection<String> topics = Arrays.asList("hadoop");
// 消費者訂閱topic
consumer.subscribe(topics);
ConsumerRecords<String, String> consumerRecords = null;
while (true) {
// 接下來就要從topic中拉取數據
consumerRecords = consumer.poll(1000);
// 遍歷每一條記錄
for (ConsumerRecord consumerRecord : consumerRecords) {
long offset = consumerRecord.offset();
int partition = consumerRecord.partition();
Object key = consumerRecord.key();
Object value = consumerRecord.value();
System.out.format("%d\t%d\t%s\t%s\n", offset, partition, key, value);
}
}
}
}
# Zookeeper connection string
# comma separated host:port pairs, each corresponding to a zk
# server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002"
zookeeper.connect= uplooking01:2181,uplooking02:2181,uplooking03:2181
bootstrap.servers=uplooking01:9092,uplooking02:9092,uplooking03:9092
# timeout in ms for connecting to zookeeper
zookeeper.connection.timeout.ms=6000
#consumer group id
group.id=test-consumer-group
#consumer timeout
#consumer.timeout.ms=5000
key.deserializer=org.apache.kafka.common.serialization.StringDeserializer
value.deserializer=org.apache.kafka.common.serialization.StringDeserializer
先執行消費者的代碼,然后再執行生產者的代碼,在消費者終端可以看到如下輸出:
2 0 1 今天的姑娘們很美
(分別是:offset partition key value)
可以通過自定義partitioner來決定我們的消息應該存到哪個partition上,只需要在我們的代碼上實現Partitioner接口即可。
package com.uplooking.bigdata.kafka.partitioner;
import org.apache.kafka.clients.producer.Partitioner;
import org.apache.kafka.common.Cluster;
import java.util.Map;
import java.util.Random;
/**
* 創建自定義的分區,根據數據的key來進行劃分
* <p>
* 可以根據key或者value的hashCode
* 還可以根據自己業務上的定義將數據分散在不同的分區中
* 需求:
* 根據用戶輸入的key的hashCode值和partition個數求模
*/
public class MyKafkaPartitioner implements Partitioner {
public void configure(Map<String, ?> configs) {
}
/**
* 根據給定的數據設置相關的分區
*
* @param topic 主題名稱
* @param key key
* @param keyBytes 序列化之后的key
* @param value value
* @param valueBytes 序列化之后的value
* @param cluster 當前集群的元數據信息
*/
public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes, Cluster cluster) {
Integer partitionNums = cluster.partitionCountForTopic(topic);
int targetPartition = -1;
if (key == null || keyBytes == null) {
targetPartition = new Random().nextInt(10000) % partitionNums;
} else {
int hashCode = key.hashCode();
targetPartition = hashCode % partitionNums;
System.out.println("key: " + key + ", value: " + value + ", hashCode: " + hashCode + ", partition: " + targetPartition);
}
return targetPartition;
}
public void close() {
}
}
package com.uplooking.bigdata.kafka.producer;
import com.uplooking.bigdata.kafka.constants.Constants;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Random;
/**
* 通過這個KafkaProducerOps向Kafka topic中生產相關的數據
* <p>
* Producer
*/
public class KafkaProducerOps {
public static void main(String[] args) throws IOException {
/**
* 專門加載配置文件
* 配置文件的格式:
* key=value
*
* 在代碼中要盡量減少硬編碼
* 不要將代碼寫死,要可配置化
*/
Properties properties = new Properties();
InputStream in = KafkaProducerOps.class.getClassLoader().getResourceAsStream("producer.properties");
properties.load(in);
/**
* 兩個泛型參數
* 第一個泛型參數:指的就是kafka中一條記錄key的類型
* 第二個泛型參數:指的就是kafka中一條記錄value的類型
*/
String[] girls = new String[]{"姚慧瑩", "劉向前", "周 新", "楊柳"};
Producer<String, String> producer = new KafkaProducer<String, String>(properties);
Random random = new Random();
int start = 1;
for (int i = start; i <= start + 9; i++) {
String topic = properties.getProperty(Constants.KAFKA_PRODUCER_TOPIC);
String key = i + "";
String value = "今天的<--" + girls[random.nextInt(girls.length)] + "-->很美很美哦~";
ProducerRecord<String, String> producerRecord =
new ProducerRecord<String, String>(topic, key, value);
producer.send(producerRecord);
}
producer.close();
}
}
繼續使用前面的消費者的代碼,同時需要在producer.properties中指定我們定義的partitioner,如下:
partitioner.class=com.uplooking.bigdata.kafka.partitioner.MyKafkaPartitioner
先執行消費者代碼,然后再執行生產者代碼,查看終端輸出。
生產者終端輸出(主要是自定義partitioner中的輸出):
key: 1, value: 今天的<--劉向前-->很美很美哦~, hashCode: 49, partition: 1
key: 2, value: 今天的<--楊柳-->很美很美哦~, hashCode: 50, partition: 2
key: 3, value: 今天的<--姚慧瑩-->很美很美哦~, hashCode: 51, partition: 0
key: 4, value: 今天的<--周 新-->很美很美哦~, hashCode: 52, partition: 1
key: 5, value: 今天的<--劉向前-->很美很美哦~, hashCode: 53, partition: 2
key: 6, value: 今天的<--周 新-->很美很美哦~, hashCode: 54, partition: 0
key: 7, value: 今天的<--周 新-->很美很美哦~, hashCode: 55, partition: 1
key: 8, value: 今天的<--劉向前-->很美很美哦~, hashCode: 56, partition: 2
key: 9, value: 今天的<--楊柳-->很美很美哦~, hashCode: 57, partition: 0
key: 10, value: 今天的<--姚慧瑩-->很美很美哦~, hashCode: 1567, partition: 1
消費者終端輸出:
3 0 3 今天的<--姚慧瑩-->很美很美哦~
4 0 6 今天的<--周 新-->很美很美哦~
5 0 9 今天的<--楊柳-->很美很美哦~
0 2 2 今天的<--楊柳-->很美很美哦~
1 2 5 今天的<--劉向前-->很美很美哦~
2 2 8 今天的<--劉向前-->很美很美哦~
1 1 1 今天的<--劉向前-->很美很美哦~
2 1 4 今天的<--周 新-->很美很美哦~
3 1 7 今天的<--周 新-->很美很美哦~
4 1 10 今天的<--姚慧瑩-->很美很美哦~
(分別是:offset partition key value)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。