How to read the value from properties file in Scala to make a Generic Application ?
Hello Friends,
Due to request of many friends, today I'm going to explain how to read the value from properties file in Scala to make a Application generic, where we can pass the value based upon requirement and Environment without any changes in code. This is really very helpful to maintain the project and we can use the same application with few configuration level changes for a similar requirement.
Same Application we can use/test in different environments to perform the testing and the environment level details we can provide into properties file.
For example, let's say I've a Scala project config-file-test-in-scala which inserts data into hive table hive_test_1.
With the same logic again if we have to work for different module/data and have to insert the data in hive table hive_test_2. then hive table name we can change/get from properties file and reuse the same application without any code change.
In properties file we provide the data in key=value pair, where we get the value in Project/Application using it's key.
To create a properties file we have to go in src/main/resources folder of Scala project and there we can create folders for each environment and create a properties file (generic.properties) under each with proper details we need to access in Scala code.
in Below snap, you can see that I've created 4 folders for each environment, where we need to provide the required details in key=value pair.
In Above snap I am giving key=value as below :
Due to request of many friends, today I'm going to explain how to read the value from properties file in Scala to make a Application generic, where we can pass the value based upon requirement and Environment without any changes in code. This is really very helpful to maintain the project and we can use the same application with few configuration level changes for a similar requirement.
Same Application we can use/test in different environments to perform the testing and the environment level details we can provide into properties file.
For example, let's say I've a Scala project config-file-test-in-scala which inserts data into hive table hive_test_1.
With the same logic again if we have to work for different module/data and have to insert the data in hive table hive_test_2. then hive table name we can change/get from properties file and reuse the same application without any code change.
In properties file we provide the data in key=value pair, where we get the value in Project/Application using it's key.
To create a properties file we have to go in src/main/resources folder of Scala project and there we can create folders for each environment and create a properties file (generic.properties) under each with proper details we need to access in Scala code.
in Below snap, you can see that I've created 4 folders for each environment, where we need to provide the required details in key=value pair.
In Above snap I am giving key=value as below :
configTestValue=I am testing to pass config value into Scala Method
Where I'll read the value using key configTestValue like below code.
First We need to import these below :
import java.util.Properties
import org.apache.log4j.PropertyConfigurator
Then write the below to create connection to read properties file.
Here I am using my Development Environment (DIT) properties file.
val connectionParam = new Properties
connectionParam.load(getClass().getResourceAsStream("/DIT/generic.properties"))
PropertyConfigurator.configure(connectionParam)
Now using below code we will read the required Key from properties file
val configTestValue = connectionParam.getProperty("configTestValue")
Below is a sample Method to use this config value read from properties file
def printConfigValue(): Unit = {
println(configTestValue)
}
Now using below println, we can verify the value which we are able to read from properties file.
println(printConfigValue)
Below is the snap for the above code with output (printing the value of printConfigValue)
Hope you enjoyed the session. If you like and find it informative, please like and share it !!!
Thank You!!
Comments
Post a Comment