Kafka consumer in Spark Streaming - java

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,..).

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

Remove consumer group created from java spring Kafka listener

I'm using Spring kafka listener to read messages from kafka topic.
#KafkaListener( id = "member-group", topics = "member-updated" )
I used to create a new consumer group each time(by changing above group id) to consume all records.
Now I've around 50 consumer group and I want to delete them. I tried to delete from kafka windows client -
kafka-consumer-groups.bat --zookeeper <zookeeper-url> --delete --group <group-name>
but it failed with following output -
Note: This will only show information about consumers that use ZooKeeper (not those using the Java consumer API).
Error: Delete for group '<group-name>' failed because group does not exist.
So is there any way to delete these consumer group either by code or by some other tool.
And what is default retention policy for consumer group.
Use --bootstrap-server host:port instead of --zookeeper.
Run the script without any parameters to get help...
--bootstrap-server <String: server to REQUIRED (for consumer groups based on
connect to> the new consumer): The server to
connect to.
...
--zookeeper <String: urls> REQUIRED (for consumer groups based on
the old consumer): The connection
string for the zookeeper connection
in the form host:port. Multiple URLS
can be given to allow fail-over.

RabbitMQ Listening to a queue from multiple servers

I need to realize listening from a queue from two servers. The queue name is the same. The first server is the primary, the second is the backup.
When the main server is down, work with the backup server queue should continue.
My class:
#RabbitListener(queues = "to_client")
public class ClientRabbitService {
Now I use RoutingConnectionFactory:
#Bean
#Primary
public ConnectionFactory routingConnectionFactory() {
SimpleRoutingConnectionFactory rcf = new SimpleRoutingConnectionFactory();
Map<Object, ConnectionFactory> map = new HashMap<>();
map.put("[to_kernel]", mainConnectionFactory());
map.put("[to_kernel_reserve]", reserveConnectionFactory());
map.put("[to_client]", mainConnectionFactory());
rcf.setTargetConnectionFactories(map);
return rcf;
}
[to_kernel] and [to_kernel_reserve] - the queues for sending messages only, [to_client] - to receive them.
Any ideas please?
Is the queue on backup server populated only when primary server is down? If yes you may always listen to both queues (queue on secondary server will be empty when primary is up).
Note that your solution would be more reliable if you use RabbitMQ clustering.
Then, you connect to the cluster (you specify addresses of all machines in cluster).
It is explained in official documentation https://docs.spring.io/spring-amqp/reference/htmlsingle/#connections
Alternatively, if running in a clustered environment, use the
addresses attribute.
<rabbit:connection-factory id="connectionFactory" addresses="host1:5672,host2:5672"/>
When using cluster you will have single queue (replicated across cluster). Note that RabbitMQ suffers significant performance hit when using replication, be sure to read official documentation how to configure clustering https://www.rabbitmq.com/clustering.html

Tibco JMS (EMS) TimeToLive per client?

I haven't been able to figure this one out from Google alone. I am connecting to a non-durable EMS topic, which publishes updates to a set of data. If I skip a few updates, it doesn't matter, as the following update will overwrite it anyway.
The number of messages being published on the EMS topic is quite high, and occasionally for whatever reason the consumer lags behind. Is there a way, on the client connection side, to determine a 'time to live' for messages? I know there is on other brokers, but specifically on Tibco I have been unable to figure out whether it's possible or not, only that this parameter can definitely be set on the server side for all clients (this is not an option for me).
I am creating my connection factory and then creating an Apache Camel jms endpoint with the following code:
TibjmsConnectionFactory connectionFactory = new TibjmsConnectionFactory();
connectionFactory.setServerUrl(properties.getProperty(endpoints.getServerUrl()));
connectionFactory.setUserName(properties.getProperty(endpoints.getUsername()));
connectionFactory.setUserPassword(properties.getProperty(endpoints.getPassword()));
JmsComponent emsComponent = JmsComponent.jmsComponent(connectionFactory);
emsComponent.setAsyncConsumer(true);
emsComponent.setConcurrentConsumers(Integer.parseInt(properties.getProperty("jms.concurrent.consumers")));
emsComponent.setDeliveryPersistent(false);
emsComponent.setClientId("MyClient." + ManagementFactory.getRuntimeMXBean().getName() + "." + emsConnectionNumber.getAndIncrement());
return emsComponent;
I am using tibjms-6.0.1, tibjmsufo-6.0.1, and various other tib***-6.0.1.
The JMSExpiration property can be set per message or, more globally, at the destination level (in which case the JMSExpiration of all messages received in this destination is overridden). It cannot be set per consumer.
One option would be to create a bridge from the topic to a custom queue that only your consumer application will listen to, and set the "expiration" property of this queue to 0 (unlimited). All messages published on the topic will then be copied to this queue and won't ever expire, whatever their JMSExpiration value.

Categories

Resources