org.apache.hudi.common.config.HoodieConfig Maven / Gradle / Ivy
/*
* 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.hudi.common.config;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hudi.common.util.Option;
import org.apache.hudi.common.util.ReflectionUtils;
import org.apache.hudi.common.util.StringUtils;
import org.apache.hudi.exception.HoodieException;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
/**
* This class deals with {@link ConfigProperty} and provides get/set functionalities.
*/
public class HoodieConfig implements Serializable {
private static final Logger LOG = LogManager.getLogger(HoodieConfig.class);
protected static final String CONFIG_VALUES_DELIMITER = ",";
public static HoodieConfig create(FSDataInputStream inputStream) throws IOException {
HoodieConfig config = new HoodieConfig();
config.props.load(inputStream);
return config;
}
protected TypedProperties props;
public HoodieConfig() {
this.props = new TypedProperties();
}
public HoodieConfig(Properties props) {
this.props = new TypedProperties(props);
}
public void setValue(ConfigProperty cfg, String val) {
cfg.checkValues(val);
props.setProperty(cfg.key(), val);
}
public void setValue(String key, String val) {
props.setProperty(key, val);
}
public void setAll(Properties properties) {
props.putAll(properties);
}
public void setDefaultValue(ConfigProperty configProperty) {
if (!contains(configProperty)) {
Option inferValue = Option.empty();
if (configProperty.getInferFunc().isPresent()) {
inferValue = configProperty.getInferFunc().get().apply(this);
}
props.setProperty(configProperty.key(), inferValue.isPresent() ? inferValue.get().toString() : configProperty.defaultValue().toString());
}
}
public void setDefaultValue(ConfigProperty configProperty, T defaultVal) {
if (!contains(configProperty)) {
props.setProperty(configProperty.key(), defaultVal.toString());
}
}
public Boolean contains(String key) {
return props.containsKey(key);
}
public boolean contains(ConfigProperty configProperty) {
if (props.containsKey(configProperty.key())) {
return true;
}
return configProperty.getAlternatives().stream().anyMatch(props::containsKey);
}
private Option