spark加载properties配置文件方法
2018-03-12 15:02
211 查看
首先我先介绍一下错误的写法,这个加载配置文件方法是错误的:
[java] view plain copy
val props = new Properties();
val loader = getClass.getClassLoader;
props.load(new FileInputStream(loader.getResource("config.properties").getFile()))
这个是把配置文件直接放在resource的目录下,去获得配置文件信息,这个写法在spark程序中会报找不到配置文件。
正确写法:
[java] view plain copy
val props = new Properties();
props.load(new FileInputStream("config.properties"));
val hdfspath = props.getProperty("hdfspath");
val mysqlpath = props.getProperty("mysql");
你可以在这些地方加载配置文件
1.
[java] view plain copy
kafkaStream.foreachRDD { rdd =>
rdd.foreachPartition { partition =>
val filePath = "config.properties"
LogUtil.info(filePath)
val props = new Properties()
props.load(new FileInputStream(filePath))
LogUtil.info("一")
props.keySet().toArray().foreach { x =>
LogUtil.info(x + "\t一" + props.getProperty(x.toString()))
}
[java] view plain copy
2. partition.foreach { x =>
LogUtil.info(x)
val filePath1 = "config.properties"
LogUtil.info(filePath1)
val props1 = new Properties()
props1.load(new FileInputStream(filePath1))
LogUtil.info("二")
props1.keySet().toArray().foreach { x =>
LogUtil.info(x + "\t二" + props1.getProperty(x.toString()))
}
}
3.
[java] view plain copy
def main(args: Array[String]): Unit = {
var kafkaZkQuorum = ""
var group = "EventETL_test_group"
var topics = ""
var numThreads = 1
var timeDuration = 3
var checkpointDir = "/Users/test/sparktemp"
println("Usage: configuration file")
val filePath = "config.properties"
LogUtil.info(filePath)
val props = new Properties()
props.load(new FileInputStream(filePath))
[java] view plain copy
val props = new Properties();
val loader = getClass.getClassLoader;
props.load(new FileInputStream(loader.getResource("config.properties").getFile()))
这个是把配置文件直接放在resource的目录下,去获得配置文件信息,这个写法在spark程序中会报找不到配置文件。
正确写法:
[java] view plain copy
val props = new Properties();
props.load(new FileInputStream("config.properties"));
val hdfspath = props.getProperty("hdfspath");
val mysqlpath = props.getProperty("mysql");
你可以在这些地方加载配置文件
1.
[java] view plain copy
kafkaStream.foreachRDD { rdd =>
rdd.foreachPartition { partition =>
val filePath = "config.properties"
LogUtil.info(filePath)
val props = new Properties()
props.load(new FileInputStream(filePath))
LogUtil.info("一")
props.keySet().toArray().foreach { x =>
LogUtil.info(x + "\t一" + props.getProperty(x.toString()))
}
[java] view plain copy
2. partition.foreach { x =>
LogUtil.info(x)
val filePath1 = "config.properties"
LogUtil.info(filePath1)
val props1 = new Properties()
props1.load(new FileInputStream(filePath1))
LogUtil.info("二")
props1.keySet().toArray().foreach { x =>
LogUtil.info(x + "\t二" + props1.getProperty(x.toString()))
}
}
3.
[java] view plain copy
def main(args: Array[String]): Unit = {
var kafkaZkQuorum = ""
var group = "EventETL_test_group"
var topics = ""
var numThreads = 1
var timeDuration = 3
var checkpointDir = "/Users/test/sparktemp"
println("Usage: configuration file")
val filePath = "config.properties"
LogUtil.info(filePath)
val props = new Properties()
props.load(new FileInputStream(filePath))
相关文章推荐
- spark加载properties配置文件方法
- java加载properties配置文件的几种方法
- Python实现加载及解析properties配置文件的方法
- Java配置properties文件的加载和读取方法
- spring加载一个或者多个properties配置文件方法
- SpringMvc 加载配置.Properties文件中的方法
- java读取.properties配置文件的几种方法
- 配置文件读取的一种方法config.properties
- All about JAVA 读取配置文件的方法 (这里是config.properties)
- java 读取properties配置文件内容乱码 --日文乱码对应方法
- 反射的应用,读取properties配置文件中的数据(普通InputStream方式,类加载方式),再调用运行
- Java中加载properties文件的6种方法
- Spring配置文件中使用ref local与ref bean的区别. 在ApplicationResources.properties文件中,使用<ref bean>与<ref local>方法如下
- JAVA Servlet 动态加载配置文件.properties
- Spring加载配置文件的几种方法
- Java读取properties配置文件时,中文乱码解决方法
- 读取***.properties配置文件里数据的方法
- 加载spring配置文件的常用三种方法
- 6种properties文件加载方法
- Spring加载配置文件的3种方法