How to list all producers of a kafka cluster? - java

I am able to list all Kafka Consumer with KafkaAdminClient:
AdminClient client = AdminClient.create(conf);
ListTopicsResult ltr = client.listTopics();
KafkaFuture<Set<String>> names = ltr.names();
ArrayList<ConsumerGroupListing> consumerGroups = new ArrayList<>(client.listConsumerGroups().all().get());
ConsumerGroupListing consumerGroup = consumerGroups.get(0);
Is it possible to list all registrated producers in a similar way?

In contrast to consumers, it is not possible to retrieve such information since Kafka brokers don't store any kind of information about the producers connected to them.

Related

Topic created in all kafka port

server.propereties setup:
listeners=PLAINTEXT://:29092, SSL://:29093
SSL related set too done.
so that we can connect 29092 for plaintext and 29093 along with SSL setup.
Here am trying to produce data into port 29093 as below
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, System.getProperty("kafkaPort", "localhost:29093"));
//SSL related setup too done in props
Producer<Long, String> producer = new KafkaProducer<>(props, new LongSerializer(), new KafkaSerializer());
final ProducerRecord<Long, String> record = new ProducerRecord<Long, String>(System.getProperty("kafkaTopic", "dqerror"),
content);
RecordMetadata metadata = producer.send(record).get();
After publishing dqerror topic created in both also data get published in both
Data is published into two topic.
Actually, am trying to find is any possible to restrict to drop data into a specific port ?
Data is not published in "both" ports. There is only one Kafka cluster that is listening on two ports. There is one set of disks that the data is written into on your one broker.
Also, from what I can tell, there is only one topic used in your code.
If you want to restrict TCP traffic on any port, that would be a firewall rule from the OS, rather than any Kafka settings or Java code.

How to produce messages to different Kafka topics and schema registries from the same producer

I'm trying to produce messages to different kafka topics from the same producer in my Java application.
This is how I create my producer and send a message to the topics.
#Bean
public Producer producer() {
Properties config = sdpProperties();
config.setProperty("schema.registry.url", "");
config.setProperty("client.id", "1"); ...
return new Producer(config);
}
producer.send(topic1, genericRecord, datasetId1);
producer.send(topic2, genericRecord, datasetId2);
However, these two different topics have different schema.registry.urls. Through research, I saw that you can set more than one registry url in the config but when I try to follow this, it only validates against the second url. Messages to topic2 are produced correctly but messages to topic1 are not. Messages to both topics are only validated against url2 instead of validated topic1 against url1 and topic2 against url2.
config.setProperty("schema.registry.url", "ur1,url2");
How can I use the same producer to send messages to these two different topics even though they have different schema.registry.urls? Am I setting this config incorrectly?
Ideally, you shouldn't have multiple registries.
But if you are aware of the architectural design behind that decision, then you must create two unique producers with different registry urls. The comma separation is for load balancing against one "registry cluster", not a loop over multiple, unique registries

kafka does not have messages that producer sent

I use spring framework and kafka that has 3 brokers clustered. I found out that consumer did not consume some messages (let say 0.01 percent between all sent messages) so in producer code I log message offset that returned by api:
ListenableFuture<SendResult<String, Message>> future = messageTemplate.sendDefault(id, message);
SendResult<String, Message> sendResult = future.get();
String offset = sendResult.getRecordMetadata().offset();
I use return offset to query kafka topic in all partition but it do not find the message (I test other offsets related to messages that consumers used and they are in kafka), whats the problem and how can I insure that message sent to kafka??
I also used messageTemplate.flush(); in producer
I find out that when topic leader of Kafka broker goes down Kafka will rebalance itself and another broker becomes the leader of that partition and if the ack config is not set to all there is a chance of losing some data in this procedure. so change the config to
ack=all
also, there is a chance of losing data if minimum in sync replica becomes less than 2, so set it at least to 2.
min.insync.replicas = 2

Kafka one consumer one partition

I have a use case where I have a single topic with 100 partitions where messages go in each partition with some logic and I have 100 consumers who reads this message. I want to map a specific partition to a specific consumer. How can I achieve that?
Checkout the Javadoc for the KafkaConsumer, specifically the section "Manual Partition Assignment".
TL/DR
You can manually assign specific partitions to a consumer as follows:
String topic = "foo";
TopicPartition partition0 = new TopicPartition(topic, 0);
TopicPartition partition1 = new TopicPartition(topic, 1);
consumer.assign(Arrays.asList(partition0, partition1));

Kafka consumer in Spark Streaming

Trying to write a Spark Streaming job that consumes messages from Kafka. Here’s what I’ve done so far:
Started Zookeeper
Started Kafka Server
Sent a few messages to the server. I can see them when I run the following:
bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic mytopic --from-beginning
Now trying to write a program to count # of messages coming in within 5 minutes.
The code looks something like this:
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("mytopic", new Integer(1));
JavaStreamingContext ssc = new JavaStreamingContext(
sparkUrl, " Spark Streaming", new Duration(60 * 5 * 1000), sparkHome, new String[]{jarFile});
JavaPairReceiverInputDStream tweets = KafkaUtils.createStream(ssc, "localhost:2181", "1", map);
Not sure what value to use for the 3rd argument (consumer group). When I run this I get Unable to connect to zookeeper server. But Zookeeper is running on port 2181; otherwise step #3 would not have worked.
Seems like I am not using KafkaUtils.createStream properly. Any ideas?
There is no such thing as default consumer group. You can use an arbitrary non-empty string there. If you have only one consumer, its consumer group doesn't really matter. If there are two or more consumers, they can either be a part of the same consumer group or belong to different consumer groups.
From http://kafka.apache.org/documentation.html :
Consumers
...
If all the consumer instances have the same consumer group, then this
works just like a traditional queue balancing load over the consumers.
If all the consumer instances have different consumer groups, then
this works like publish-subscribe and all messages are broadcast to
all consumers.
I think the problem may be in 'topics' parameter.
From Spark docs:
Map of (topic_name -> numPartitions) to consume. Each partition is consumed in its own thread
You only specified a single partition for your topic, namely '1'. Depending on broker's setting (num.partitions), there may be more than one partitions and your messages may be sent to other partitions which aren't read by your program.
Besides, I believe the partitionIds are 0 based. So if you have only one partition, it will have the id equal to 0.
I think you should specify the ip for zookeeper instead of localhost. Also, the third argument is for consumer group name. It can be any name you like. It is for the time when you have more than one consumer tied to the same group,topic partitions are distributed accordingly.Your tweets should be:
JavaPairReceiverInputDStream tweets = KafkaUtils.createStream(ssc, "x.x.x.x", "dummy-group", map);
I was facing the same issue. Here is the solution that worked for me.
The number of cores allocated to the Spark Streaming application must be more than the number of receivers. Otherwise the system will receive data, but not be able to process it.So Spark Streaming requires minimum of two cores . So in my spark-submit, I should mention at-least two cores.
kafka-clients-version.jar should be included in the list of dependent jars in spark-submit.
If zookeeper is running on the same machine as your streaming application then "localhost:2181" will work. Otherwise, you have to mention the address of the host where zookeeper is running and ensure that machine on which streaming app is running is able to talk to zookeeper host on port 2181.
I think, in your code, the second argument for the call
KafkaUtils.createStream, should be the host:port of the kafka server, not the zookeeper host and port. check that once.
EDIT:
Kafka Utils API Documentation
As per the document above, it should be the zookeeper quorum . So Zookeeper hostname and port should be used.
zkQuorum
Zookeeper quorum (hostname:port,hostname:port,..).

Categories

Resources