Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Kafka parameters from a properties file?

What I am doing currently is as below:

val topic = "mytopic"
val zkhosts = "localhost"
val zkports = "2181"

Setting it in my code and then sending it to kafkastream function works, but I want to read it from .properties file. Is there any possible solution?

like image 300
HIREN GALA Avatar asked Jun 07 '17 11:06

HIREN GALA


People also ask

Where is Kafka properties file?

The Kafka configuration files are located at the /opt/bitnami/kafka/config/ directory.

What does ACKS =- 1 mean in Kafka?

With acks = -1, the producer waits for the ack. This ack is sent by the broker as above but only after having the messages replicated to all the replica followers on the other brokers.

How do I change Kafka server properties?

connect property and change it as per your needs. The Kafka broker will connect to this ZooKeeper instance. Go to the Kafka home directory and execute the command ./bin/kafka-server-start.sh config/server. properties .

Which is the configuration file for setting up Kafka properties in Kafka?

Apache Kafka is bundled with Log Analysis. The sample configuration files for Apache Kafka are in the <HOME>/IBM/LogAnalysis/kafka/test-configs/kafka-configs directory. Create one partition per topic for every two physical processors on the server where the broker is installed.


1 Answers

Given this property file at /tmp/sample.properties

kafka.topic = "mytopic"
kafka.zkhost = "localhost"
kafka.zkports = 2191

We could use the plain-old java Property API to load the properties:

import java.io.FileReader
val configFile = new java.io.File("/tmp/sample.properties")
val reader = new FileReader(configFile)
val props = new Properties()
props.load(reader)
reader.close()

You could also use your favorite config library to load the property file as you would on any other program.

For example, you could use the popular typesafe config lib. There're many wrappers for Scala, but in its raw form you could do something like:

import com.typesafe.config.ConfigFactory
val configFile = new java.io.File("/tmp/sample.properties")
val kafkaConfig = ConfigFactory.parseFile(configFile)

import java.util.Properties
val kafkaProperties = new Properties()
kafkaProperties.put("zookeeper.hosts", kafkaConfig.getString("kafka.zkhost"))
kafkaProperties.put("zookeeper.port", kafkaConfig.getInt("kafka.zkports"):java.lang.Integer)
kafkaProperties.put("kafka.topic", kafkaConfig.getString("kafka.topic"))

(There're many ways to make that nice and compact. Here I'm using the most common form)

like image 184
maasg Avatar answered Oct 11 '22 15:10

maasg