com.opower.hadoop.conf.ConfigurationUtils Maven / Gradle / Ivy
The newest version!
package com.opower.hadoop.conf;
import org.apache.hadoop.conf.Configuration;
import org.joda.time.DateTimeZone;
import org.joda.time.Interval;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Utility methods for the hadoop {@link Configuration}
*
* @author oren.benjamin
*/
public final class ConfigurationUtils {
/**
* Add an interval to the configuration
*
* Stored as three configuration values:
*
* - <key>
.start: interval start (millis since epoch set with {@link Configuration#setLong})
* - <key>
.end : interval end (millis since epoch set with {@link Configuration#setLong})
* - <key>
.zone : time zone ID
*
*
* @param key the configuration key
* @param interval the configuration value
*/
public static void setInterval(Configuration configuration, String key, Interval interval) {
checkNotNull(configuration, "configuration");
checkNotNull(key, "key");
checkNotNull(interval, "interval");
configuration.setLong(key + ".start", interval.getStartMillis());
configuration.setLong(key + ".end", interval.getEndMillis());
configuration.set(key + ".zone", interval.getChronology().getZone().getID());
}
/**
* Get an interval from the hadoop configuration
*
* @param configuration the hadoop configuration
* @param key the configuration key
* @return the interval value configured for {@code key}
* @see #setInterval
*/
public static Interval getInterval(Configuration configuration, String key) {
checkNotNull(configuration, "configuration");
checkNotNull(key, "key");
long start = getRequiredLong(configuration, key + ".start");
long end = getRequiredLong(configuration, key + ".end");
String zoneID = getRequiredValue(configuration, key + ".zone");
return new Interval(start, end, DateTimeZone.forID(zoneID));
}
/**
* Get a long value from a hadoop configuration
* @param configuration the hadoop configuration
* @param key the configuration key
* @return the long value configured for {@code key}
* @throws NumberFormatException if the configuration value is not a parseable long
* @throws NullPointerException if no configuration value is set
*/
public static long getRequiredLong(Configuration configuration, String key) {
checkNotNull(configuration, "configuration");
checkNotNull(key, "key");
return Long.parseLong(getRequiredValue(configuration, key));
}
/**
* Get a required value from a hadoop configuration
*
* @param configuration the hadoop configuration
* @param key the configuration key
* @return the configuration value
* @throws NullPointerException if no configuration value is set for {@code key}
*/
public static String getRequiredValue(Configuration configuration, String key) {
checkNotNull(configuration, "configuration");
checkNotNull(key, "key");
return checkNotNull(configuration.get(key), "No configuration value corresponding to key: %s", key);
}
private ConfigurationUtils() {}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy