diff --git hcatalog-pig-adapter/src/main/java/org/apache/hcatalog/pig/HCatLoader.java hcatalog-pig-adapter/src/main/java/org/apache/hcatalog/pig/HCatLoader.java index 4bdb7c3..5a4d400 100644 --- hcatalog-pig-adapter/src/main/java/org/apache/hcatalog/pig/HCatLoader.java +++ hcatalog-pig-adapter/src/main/java/org/apache/hcatalog/pig/HCatLoader.java @@ -82,7 +82,7 @@ public class HCatLoader extends HCatBaseLoader { @Override public void setLocation(String location, Job job) throws IOException { - HCatContext.setupHCatContext(job.getConfiguration()).getConf().get() + HCatContext.INSTANCE.setConf(job.getConfiguration()).getConf().get() .setBoolean(HCatConstants.HCAT_DATA_TINY_SMALL_INT_PROMOTION, true); UDFContext udfContext = UDFContext.getUDFContext(); @@ -187,7 +187,7 @@ public class HCatLoader extends HCatBaseLoader { @Override public ResourceSchema getSchema(String location, Job job) throws IOException { - HCatContext.setupHCatContext(job.getConfiguration()).getConf().get() + HCatContext.INSTANCE.setConf(job.getConfiguration()).getConf().get() .setBoolean(HCatConstants.HCAT_DATA_TINY_SMALL_INT_PROMOTION, true); Table table = phutil.getTable(location, diff --git hcatalog-pig-adapter/src/main/java/org/apache/hcatalog/pig/HCatStorer.java hcatalog-pig-adapter/src/main/java/org/apache/hcatalog/pig/HCatStorer.java index 54c174b..a457a07 100644 --- hcatalog-pig-adapter/src/main/java/org/apache/hcatalog/pig/HCatStorer.java +++ hcatalog-pig-adapter/src/main/java/org/apache/hcatalog/pig/HCatStorer.java @@ -77,7 +77,7 @@ public class HCatStorer extends HCatBaseStorer { @Override public void setStoreLocation(String location, Job job) throws IOException { - HCatContext.setupHCatContext(job.getConfiguration()).getConf().get() + HCatContext.INSTANCE.setConf(job.getConfiguration()).getConf().get() .setBoolean(HCatConstants.HCAT_DATA_TINY_SMALL_INT_PROMOTION, false); Configuration config = job.getConfiguration(); diff --git src/java/org/apache/hcatalog/common/HCatContext.java src/java/org/apache/hcatalog/common/HCatContext.java index 1c40b5e..34e1af9 100644 --- src/java/org/apache/hcatalog/common/HCatContext.java +++ src/java/org/apache/hcatalog/common/HCatContext.java @@ -21,50 +21,65 @@ package org.apache.hcatalog.common; import com.google.common.base.Optional; import com.google.common.base.Preconditions; import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hive.common.classification.InterfaceAudience; +import org.apache.hadoop.hive.common.classification.InterfaceStability; import java.util.Map; /** - * HCatContext provides global access to configuration data. It uses a reference to the - * job configuration so that settings are automatically passed to the backend by the - * MR framework. + * HCatContext is a singleton that provides global access to configuration data. + * + *

HCatalog provides a variety of functionality that users can configure at runtime through + * configuration properties. Available configuration properties are defined in + * {@link HCatConstants}. HCatContext allows users to enable optional functionality by + * setting properties in a provided configuration.

+ * + *

HCatalog users (MR apps, processing framework adapters) should set properties + * in a configuration that has been provided to + * {@link #setConf(org.apache.hadoop.conf.Configuration)} to enable optional functionality. + * The job configuration must be used to ensure properties are passed to the backend MR tasks.

+ * + *

HCatalog developers should enable optional functionality by checking properties + * from {@link #getConf()}. Since users are not obligated to set a configuration, optional + * functionality must provide a sensible default.

*/ -public class HCatContext { - - private static final HCatContext hCatContext = new HCatContext(); +@InterfaceAudience.Public +@InterfaceStability.Evolving +public enum HCatContext { + INSTANCE; private Configuration conf = null; - private HCatContext() { - } - /** - * Setup the HCatContext as a reference to the given configuration. Keys - * exclusive to an existing config are set in the new conf. + * Use the given configuration for optional behavior. Keys exclusive to an existing config + * are set in the new conf. The job conf must be used to ensure properties are passed to + * backend MR tasks. */ - public static synchronized HCatContext setupHCatContext(Configuration newConf) { - Preconditions.checkNotNull(newConf, "HCatContext must not have a null conf."); + public synchronized HCatContext setConf(Configuration newConf) { + Preconditions.checkNotNull(newConf, "Required parameter 'newConf' must not be null."); - if (hCatContext.conf == null) { - hCatContext.conf = newConf; - return hCatContext; + if (conf == null) { + conf = newConf; + return this; } - if (hCatContext.conf != newConf) { - for (Map.Entry entry : hCatContext.conf) { + if (conf != newConf) { + for (Map.Entry entry : conf) { if (newConf.get(entry.getKey()) == null) { newConf.set(entry.getKey(), entry.getValue()); } } - hCatContext.conf = newConf; + conf = newConf; } - return hCatContext; - } - - public static HCatContext getInstance() { - return hCatContext; + return this; } + /** + * Get the configuration, if there is one. Users are not required to setup HCatContext + * unless they wish to override default behavior, so the configuration may not be present. + * + * @return an Optional that might contain a Configuration + */ public Optional getConf() { return Optional.fromNullable(conf); } diff --git src/java/org/apache/hcatalog/data/HCatRecordSerDe.java src/java/org/apache/hcatalog/data/HCatRecordSerDe.java index 468a1f5..b59febe 100644 --- src/java/org/apache/hcatalog/data/HCatRecordSerDe.java +++ src/java/org/apache/hcatalog/data/HCatRecordSerDe.java @@ -265,8 +265,8 @@ public class HCatRecordSerDe implements SerDe { private static Object serializePrimitiveField(Object field, ObjectInspector fieldObjectInspector) { - if (field != null && HCatContext.getInstance().getConf().isPresent()) { - Configuration conf = HCatContext.getInstance().getConf().get(); + if (field != null && HCatContext.INSTANCE.getConf().isPresent()) { + Configuration conf = HCatContext.INSTANCE.getConf().get(); if (field instanceof Boolean && conf.getBoolean( diff --git src/java/org/apache/hcatalog/data/schema/HCatSchemaUtils.java src/java/org/apache/hcatalog/data/schema/HCatSchemaUtils.java index b4060e9..5e95439 100644 --- src/java/org/apache/hcatalog/data/schema/HCatSchemaUtils.java +++ src/java/org/apache/hcatalog/data/schema/HCatSchemaUtils.java @@ -139,14 +139,14 @@ public class HCatSchemaUtils { private static Type getPrimitiveHType(TypeInfo basePrimitiveTypeInfo) { switch (((PrimitiveTypeInfo) basePrimitiveTypeInfo).getPrimitiveCategory()) { case BOOLEAN: - return (HCatContext.getInstance().getConf().isPresent() && - HCatContext.getInstance().getConf().get().getBoolean( + return (HCatContext.INSTANCE.getConf().isPresent() && + HCatContext.INSTANCE.getConf().get().getBoolean( HCatConstants.HCAT_DATA_CONVERT_BOOLEAN_TO_INTEGER, HCatConstants.HCAT_DATA_CONVERT_BOOLEAN_TO_INTEGER_DEFAULT)) ? Type.INT : Type.BOOLEAN; case BYTE: - return (HCatContext.getInstance().getConf().isPresent() && - HCatContext.getInstance().getConf().get().getBoolean( + return (HCatContext.INSTANCE.getConf().isPresent() && + HCatContext.INSTANCE.getConf().get().getBoolean( HCatConstants.HCAT_DATA_TINY_SMALL_INT_PROMOTION, HCatConstants.HCAT_DATA_TINY_SMALL_INT_PROMOTION_DEFAULT)) ? Type.INT : Type.TINYINT; @@ -159,8 +159,8 @@ public class HCatSchemaUtils { case LONG: return Type.BIGINT; case SHORT: - return (HCatContext.getInstance().getConf().isPresent() && - HCatContext.getInstance().getConf().get().getBoolean( + return (HCatContext.INSTANCE.getConf().isPresent() && + HCatContext.INSTANCE.getConf().get().getBoolean( HCatConstants.HCAT_DATA_TINY_SMALL_INT_PROMOTION, HCatConstants.HCAT_DATA_TINY_SMALL_INT_PROMOTION_DEFAULT)) ? Type.INT : Type.SMALLINT;