sau*_*bht 1 scala apache-spark
我正在尝试使用 Scala 中的类型安全配置库读取配置文件,但无法将 conf 文件放入资源文件夹中。
我的property/config文件格式如下
region=dev
numlines=2
Run Code Online (Sandbox Code Playgroud)
文件的名称是property.txt
代码如下所示
import com.typesafe.config._
val propertyFile = args(2)
val myConfigFile = new File(propertyFile)
val fileConfig = ConfigFactory.parseFile(myConfigFile)
val config = ConfigFactory.load(fileConfig)
val environment = config.getString("region")
val numberOfLinesToBeRemoved = config.getInt("numlines")
Run Code Online (Sandbox Code Playgroud)
案例 1 - 假设您有一个 sbt 项目,那么您的配置文件abc.conf应位于src/main/resources/abc.conf.
现在假设该文件abc.conf具有以下内容。
region=dev
numlines=2
Run Code Online (Sandbox Code Playgroud)
现在,您可以通过以下方式访问这些配置:
import com.typesafe.config._
val confFileName = "abc"
val config = ConfigFactory.load(confFileName)
val environment = config.getString("region")
val numberOfLinesToBeRemoved = config.getInt("numlines")
Run Code Online (Sandbox Code Playgroud)
情况 2 - 如果您无法将conf 文件作为资源包含在项目中,则可以将conf 文件路径作为参数传递给java 命令。
import com.typesafe.config._
val config = ConfigFactory.load()
val environment = config.getString("region")
val numberOfLinesToBeRemoved = config.getInt("numlines")
Run Code Online (Sandbox Code Playgroud)
现在,您必须在运行应用程序时传递配置文件路径,如下所示:
java -jar your_jar.jar -Dconfig.file=path/to/config-file
Run Code Online (Sandbox Code Playgroud)
情况 3 - 您想要使用某个指定路径中的配置
import com.typesafe.config._
import java.nio.file.Paths
// It should be absolute or full path
val confFilePath = "/home/your_username/something/abc.conf"
// just replace the above line by
// val confFilePath = args(2)
// and pass the full path as command line argument.
val confFile = Paths.get(confFilePath).toFile
val config = ConfigFactory.parseFile(confFile)
val environment = config.getString("region")
val numberOfLinesToBeRemoved = config.getInt("numlines")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2805 次 |
| 最近记录: |