The following is a way to convert a Configa Configafe object to a Java object. I just tested it in a simple case to create Kafka properties.
Given this configuration in application.conf
kafka-topics { my-topic { zookeeper.connect = "localhost:2181", group.id = "testgroup", zookeeper.session.timeout.ms = "500", zookeeper.sync.time.ms = "250", auto.commit.interval.ms = "1000" } }
You can create the corresponding Properties object as follows:
import com.typesafe.config.{Config, ConfigFactory} import java.util.Properties import kafka.consumer.ConsumerConfig object Application extends App { def propsFromConfig(config: Config): Properties = { import scala.collection.JavaConversions._ val props = new Properties() val map: Map[String, Object] = config.entrySet().map({ entry => entry.getKey -> entry.getValue.unwrapped() })(collection.breakOut) props.putAll(map) props } val config = ConfigFactory.load() val consumerConfig = { val topicConfig = config.getConfig("kafka-topics.my-topic") val props = propsFromConfig(topicConfig) new ConsumerConfig(props) }
The propsFromConfig function is what you are most interested in, and the key points are to use entrySet to get a smoothing list of properties and an expanded record value that gives an object whose type depends on the configuration value.
Christian perez-llamas
source share