Repository navigation
Add support for JSON and YAML configurations #514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 17.0 |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -36,6 +36,7 @@ | |||||
| import org.apache.flume.conf.FlumeConfiguration; | ||||||
| import org.apache.flume.lifecycle.LifecycleAware; | ||||||
| import org.apache.flume.lifecycle.LifecycleState; | ||||||
| import org.apache.flume.node.internal.YamlConfigConverter; | ||||||
| import org.apache.logging.log4j.LogManager; | ||||||
| import org.apache.logging.log4j.Logger; | ||||||
|
|
||||||
|
|
@@ -186,6 +187,7 @@ public class UriConfigurationProvider extends AbstractConfigurationProvider impl | |||||
| private final EventBus eventBus; | ||||||
| private final int interval; | ||||||
| private final CounterGroup counterGroup; | ||||||
| private final boolean isYamlSupported; | ||||||
| private LifecycleState lifecycleState = LifecycleState.IDLE; | ||||||
| private ScheduledExecutorService executorService; | ||||||
|
|
||||||
|
|
@@ -201,6 +203,14 @@ public UriConfigurationProvider( | |||||
| this.eventBus = eventBus; | ||||||
| this.interval = pollInterval; | ||||||
| counterGroup = new CounterGroup(); | ||||||
| boolean isYaml; | ||||||
| try { | ||||||
| Class.forName("org.snakeyaml.engine.v2.api.Load"); | ||||||
| isYaml = true; | ||||||
| } catch (Exception exception) { | ||||||
| isYaml = false; | ||||||
| } | ||||||
| isYamlSupported = isYaml; | ||||||
|
Comment on lines
+206
to
+213
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. …which of course will remove the need for reflection. |
||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
|
|
@@ -256,6 +266,15 @@ public FlumeConfiguration getFlumeConfiguration() { | |||||
| switch (configurationSource.getExtension()) { | ||||||
| case ConfigurationSource.JSON: | ||||||
| case ConfigurationSource.YAML: | ||||||
| case ConfigurationSource.YML: { | ||||||
| if (isYamlSupported) { | ||||||
| properties = YamlConfigConverter.convertYamlToProperties(is); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should append the properties from multiple sources instead of replacing them.
Suggested change
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is going to be my next PR. |
||||||
| } else { | ||||||
| logger.warn( | ||||||
| "File extension type {} is unsupported", configurationSource.getExtension()); | ||||||
| } | ||||||
| break; | ||||||
| } | ||||||
| case ConfigurationSource.XML: { | ||||||
| logger.warn("File extension type {} is unsupported", configurationSource.getExtension()); | ||||||
| break; | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to you under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.flume.node.internal; | ||
|
|
||
| import java.io.InputStream; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Properties; | ||
| import org.snakeyaml.engine.v2.api.Load; | ||
| import org.snakeyaml.engine.v2.api.LoadSettings; | ||
|
|
||
| /** | ||
| * Converts YAML and/or JSON files into Java properties that can be consumed by the UriConfigurationProvider. | ||
| * This uses version 3.x of SnakeYaml that is hosted at https://codeberg.org/snakeyaml/snakeyaml-engine | ||
| * and supports YAML 1.2. | ||
| */ | ||
| public class YamlConfigConverter { | ||
|
|
||
| public static Properties convertYamlToProperties(InputStream yamlInputStream) { | ||
| Properties properties = new Properties(); | ||
|
|
||
| LoadSettings settings = LoadSettings.builder().build(); | ||
| Load load = new Load(settings); | ||
|
|
||
| // 2. Load the YAML content into basic Java structures | ||
| Object yamlData = load.loadFromInputStream(yamlInputStream); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can throw a If one source is defective, the entire configuration will fail.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will fix this in the next PR. |
||
|
|
||
| // 3. Recursively flatten the data structure | ||
| if (yamlData instanceof Map) { | ||
| flattenMap("", (Map<?, ?>) yamlData, properties); | ||
| } else if (yamlData != null) { | ||
| throw new IllegalArgumentException("Root YAML element must be a mapping object."); | ||
| } | ||
|
|
||
| return properties; | ||
| } | ||
|
|
||
| private static void flattenMap(String prefix, Map<?, ?> map, Properties properties) { | ||
| map.forEach((key, value) -> { | ||
| String currentKey = prefix.isEmpty() ? String.valueOf(key) : prefix + "." + key; | ||
|
|
||
| if (value instanceof Map) { | ||
| Map<?, ?> childMap = (Map<?, ?>) value; | ||
| if (!prefix.isEmpty() && isComponentMap(childMap)) { | ||
| properties.setProperty(currentKey, String.join(" ", keyNames(childMap))); | ||
| } | ||
| flattenMap(currentKey, childMap, properties); | ||
| } else if (value instanceof List) { | ||
| flattenList(currentKey, (List<?>) value, properties); | ||
| } else if (value != null) { | ||
| properties.setProperty(currentKey, String.valueOf(value)); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Determines whether a Map represents a set of named components, such as the sources, channels | ||
| * or sinks of an agent. This is the case when every key is a String and every value is a Map. | ||
| */ | ||
| private static boolean isComponentMap(Map<?, ?> map) { | ||
| if (map.isEmpty()) { | ||
| return false; | ||
| } | ||
| for (Map.Entry<?, ?> entry : map.entrySet()) { | ||
| if (!(entry.getKey() instanceof String) || !(entry.getValue() instanceof Map)) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| private static List<String> keyNames(Map<?, ?> map) { | ||
| List<String> names = new ArrayList<>(map.size()); | ||
| map.keySet().forEach(key -> names.add((String) key)); | ||
| return names; | ||
| } | ||
|
|
||
| private static void flattenList(String prefix, List<?> list, Properties properties) { | ||
| // Flume expects a list of simple values, such as channel names, as a space separated value. | ||
| if (list.stream().noneMatch(value -> value instanceof Map || value instanceof List)) { | ||
| List<String> values = new ArrayList<>(list.size()); | ||
| list.stream().filter(value -> value != null).forEach(value -> values.add(String.valueOf(value))); | ||
| if (!values.isEmpty()) { | ||
| properties.setProperty(prefix, String.join(" ", values)); | ||
| } | ||
| return; | ||
| } | ||
| for (int i = 0; i < list.size(); i++) { | ||
| Object value = list.get(i); | ||
| String currentKey = prefix + "[" + i + "]"; | ||
|
|
||
| if (value instanceof Map) { | ||
| flattenMap(currentKey, (Map<?, ?>) value, properties); | ||
| } else if (value instanceof List) { | ||
| flattenList(currentKey, (List<?>) value, properties); | ||
| } else if (value != null) { | ||
| properties.setProperty(currentKey, String.valueOf(value)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to you under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.flume.node.internal; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
| import static org.junit.jupiter.api.Assertions.fail; | ||
|
|
||
| import java.io.FileInputStream; | ||
| import java.util.Properties; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class YamlConfigConverterTest { | ||
|
|
||
| @Test | ||
| public void testYaml() { | ||
| try { | ||
| FileInputStream is = new FileInputStream("target/test-classes/flume-conf-init.yml"); | ||
| Properties props = YamlConfigConverter.convertYamlToProperties(is); | ||
| assertNotNull(props); | ||
| is = new FileInputStream("target/test-classes/flume-conf-init.properties"); | ||
| Properties properties = new Properties(); | ||
| properties.load(is); | ||
| System.out.println(props.size() + " properties"); | ||
| for (String name : props.stringPropertyNames()) { | ||
| System.out.println(name + ": " + props.get(name)); | ||
| } | ||
| assertEquals(properties, props); | ||
| assertNull(props.getProperty("host1")); | ||
| } catch (Exception ex) { | ||
| fail("Unable to convert YAML to properties: " + ex.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testJson() { | ||
| try { | ||
| FileInputStream is = new FileInputStream("target/test-classes/flume-conf-init.json"); | ||
| Properties props = YamlConfigConverter.convertYamlToProperties(is); | ||
| assertNotNull(props); | ||
| is = new FileInputStream("target/test-classes/flume-conf-init.properties"); | ||
| Properties properties = new Properties(); | ||
| properties.load(is); | ||
| assertEquals(properties, props); | ||
| } catch (Exception ex) { | ||
| fail("Unable to convert JSON to properties: " + ex.getMessage()); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| { | ||
| "host1": { | ||
| "sources": { | ||
| "source1": { | ||
| "type": "org.apache.flume.source.LocalSource", | ||
| "channels": [ | ||
| "channel1" | ||
| ], | ||
| "totalEvents": 5, | ||
| "backoffSleepIncrement": 50 | ||
| }, | ||
| "processedSource": { | ||
| "type": "org.apache.flume.source.LocalSource", | ||
| "channels": [ | ||
| "processedChannel" | ||
| ] | ||
| } | ||
| }, | ||
| "channels": { | ||
| "channel1": { | ||
| "type": "memory", | ||
| "capacity": 10000 | ||
| }, | ||
| "processedChannel": { | ||
| "type": "memory", | ||
| "capacity": 10000 | ||
| } | ||
| }, | ||
| "sinks": { | ||
| "sink1": { | ||
| "type": "org.apache.flume.sink.NullInitSink", | ||
| "batchSize": 1, | ||
| "targetSource": "processedSource", | ||
| "channel": "channel1" | ||
| } | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is fine to add it as required dependency.