Lets assume we want to read all properties with prefix spark.
private static final String SPARK_PREFIX = "spark.";
@Bean
Properties sparkProperties(final Environment environment) {
if (environment instanceof ConfigurableEnvironment) {
List<PropertySource<?>> propertySources =
((ConfigurableEnvironment) environment)
.getPropertySources().stream().collect(Collectors.toList());
List<String> sparkPropertyNames =
propertySources.stream()
.filter(propertySource -> propertySource instanceof EnumerablePropertySource)
.map(propertySource -> (EnumerablePropertySource) propertySource)
.map(EnumerablePropertySource::getPropertyNames)
.flatMap(Arrays::stream)
.distinct()
.filter(key -> key.startsWith(SPARK_PREFIX))
.collect(Collectors.toList());
return sparkPropertyNames.stream()
.collect(
Properties::new,
(props, key) -> props.put(key, environment.getProperty(key)),
Properties::putAll);
} else {
return new Properties();
}
}
Then we can use this anywhere as follows. @Qualifies is important as there is another bean defined by Spring boot of type Properties
for systemProperties
@Bean
SparkConf sparkConf(@Qualifier("sparkProperties") final Properties sparkProperties) {
final SparkConf sparkConf = new SparkConf();
sparkProperties.forEach((key, value) -> sparkConf.set(key.toString(), value.toString()));
return sparkConf;
}