PrimitiveObjectInspectorFactory做了个工厂模式,避免hive过多的去new Object从而对GC造成负担。
public final class PrimitiveObjectInspectorFactory { public static final JavaBooleanObjectInspector javaBooleanObjectInspector = new JavaBooleanObjectInspector(); public static final JavaByteObjectInspector javaByteObjectInspector = new JavaByteObjectInspector(); public static final JavaShortObjectInspector javaShortObjectInspector = new JavaShortObjectInspector(); public static final JavaIntObjectInspector javaIntObjectInspector = new JavaIntObjectInspector(); public static final JavaLongObjectInspector javaLongObjectInspector = new JavaLongObjectInspector(); public static final JavaFloatObjectInspector javaFloatObjectInspector = new JavaFloatObjectInspector(); public static final JavaDoubleObjectInspector javaDoubleObjectInspector = new JavaDoubleObjectInspector(); public static final JavaStringObjectInspector javaStringObjectInspector = new JavaStringObjectInspector(); public static final JavaVoidObjectInspector javaVoidObjectInspector = new JavaVoidObjectInspector(); public static final WritableBooleanObjectInspector writableBooleanObjectInspector = new WritableBooleanObjectInspector(); public static final WritableByteObjectInspector writableByteObjectInspector = new WritableByteObjectInspector(); public static final WritableShortObjectInspector writableShortObjectInspector = new WritableShortObjectInspector(); public static final WritableIntObjectInspector writableIntObjectInspector = new WritableIntObjectInspector(); public static final WritableLongObjectInspector writableLongObjectInspector = new WritableLongObjectInspector(); public static final WritableFloatObjectInspector writableFloatObjectInspector = new WritableFloatObjectInspector(); public static final WritableDoubleObjectInspector writableDoubleObjectInspector = new WritableDoubleObjectInspector(); public static final WritableStringObjectInspector writableStringObjectInspector = new WritableStringObjectInspector(); public static final WritableVoidObjectInspector writableVoidObjectInspector = new WritableVoidObjectInspector(); private static HashMap<PrimitiveCategory, AbstractPrimitiveWritableObjectInspector> cachedPrimitiveWritableInspectorCache = new HashMap<PrimitiveCategory, AbstractPrimitiveWritableObjectInspector>(); static { cachedPrimitiveWritableInspectorCache.put(PrimitiveCategory.BOOLEAN, writableBooleanObjectInspector); cachedPrimitiveWritableInspectorCache.put(PrimitiveCategory.BYTE, writableByteObjectInspector); cachedPrimitiveWritableInspectorCache.put(PrimitiveCategory.SHORT, writableShortObjectInspector); cachedPrimitiveWritableInspectorCache.put(PrimitiveCategory.INT, writableIntObjectInspector); cachedPrimitiveWritableInspectorCache.put(PrimitiveCategory.LONG, writableLongObjectInspector); cachedPrimitiveWritableInspectorCache.put(PrimitiveCategory.FLOAT, writableFloatObjectInspector); cachedPrimitiveWritableInspectorCache.put(PrimitiveCategory.DOUBLE, writableDoubleObjectInspector); cachedPrimitiveWritableInspectorCache.put(PrimitiveCategory.STRING, writableStringObjectInspector); cachedPrimitiveWritableInspectorCache.put(PrimitiveCategory.VOID, writableVoidObjectInspector); } private static HashMap<PrimitiveCategory, AbstractPrimitiveJavaObjectInspector> cachedPrimitiveJavaInspectorCache = new HashMap<PrimitiveCategory, AbstractPrimitiveJavaObjectInspector>(); static { cachedPrimitiveJavaInspectorCache.put(PrimitiveCategory.BOOLEAN, javaBooleanObjectInspector); cachedPrimitiveJavaInspectorCache.put(PrimitiveCategory.BYTE, javaByteObjectInspector); cachedPrimitiveJavaInspectorCache.put(PrimitiveCategory.SHORT, javaShortObjectInspector); cachedPrimitiveJavaInspectorCache.put(PrimitiveCategory.INT, javaIntObjectInspector); cachedPrimitiveJavaInspectorCache.put(PrimitiveCategory.LONG, javaLongObjectInspector); cachedPrimitiveJavaInspectorCache.put(PrimitiveCategory.FLOAT, javaFloatObjectInspector); cachedPrimitiveJavaInspectorCache.put(PrimitiveCategory.DOUBLE, javaDoubleObjectInspector); cachedPrimitiveJavaInspectorCache.put(PrimitiveCategory.STRING, javaStringObjectInspector); cachedPrimitiveJavaInspectorCache.put(PrimitiveCategory.VOID, javaVoidObjectInspector); } /** * Returns the PrimitiveWritableObjectInspector for the PrimitiveCategory. * * @param primitiveCategory */ public static AbstractPrimitiveWritableObjectInspector getPrimitiveWritableObjectInspector( PrimitiveCategory primitiveCategory) { AbstractPrimitiveWritableObjectInspector result = cachedPrimitiveWritableInspectorCache.get(primitiveCategory); if (result == null) { throw new RuntimeException("Internal error: Cannot find ObjectInspector " + " for " + primitiveCategory); } return result; } /** * Returns the PrimitiveJavaObjectInspector for the PrimitiveCategory. * * @param primitiveCategory */ public static AbstractPrimitiveJavaObjectInspector getPrimitiveJavaObjectInspector( PrimitiveCategory primitiveCategory) { AbstractPrimitiveJavaObjectInspector result = cachedPrimitiveJavaInspectorCache.get(primitiveCategory); if (result == null) { throw new RuntimeException("Internal error: Cannot find ObjectInspector " + " for " + primitiveCategory); } return result; } /** * Returns an ObjectInspector for a primitive Class. The Class can be a Hive * Writable class, or a Java Primitive Class. * * A runtimeException will be thrown if the class is not recognized as a * primitive type by Hive. */ public static PrimitiveObjectInspector getPrimitiveObjectInspectorFromClass( Class<?> c) { if (Writable.class.isAssignableFrom(c)) { // It is a writable class PrimitiveTypeEntry te = PrimitiveObjectInspectorUtils .getTypeEntryFromPrimitiveWritableClass(c); if (te == null) { throw new RuntimeException("Internal error: Cannot recognize " + c); } return PrimitiveObjectInspectorFactory .getPrimitiveWritableObjectInspector(te.primitiveCategory); } else { // It is a Java class PrimitiveTypeEntry te = PrimitiveObjectInspectorUtils .getTypeEntryFromPrimitiveJavaClass(c); if (te == null) { throw new RuntimeException("Internal error: Cannot recognize " + c); } return PrimitiveObjectInspectorFactory .getPrimitiveJavaObjectInspector(te.primitiveCategory); } } private PrimitiveObjectInspectorFactory() { // prevent instantiation } }
public class WritableDoubleObjectInspector extends AbstractPrimitiveWritableObjectInspector implements SettableDoubleObjectInspector { WritableDoubleObjectInspector() { super(PrimitiveObjectInspectorUtils.doubleTypeEntry); } @Override public double get(Object o) { return ((DoubleWritable) o).get(); } @Override public Object copyObject(Object o) { return o == null ? null : new DoubleWritable(((DoubleWritable) o).get()); } @Override public Object getPrimitiveJavaObject(Object o) { return o == null ? null : Double.valueOf(((DoubleWritable) o).get()); } @Override public Object create(double value) { return new DoubleWritable(value); } @Override public Object set(Object o, double value) { ((DoubleWritable) o).set(value); return o; } }
public static StandardStructObjectInspector getStandardStructObjectInspector( List<String> structFieldNames, List<ObjectInspector> structFieldObjectInspectors) { ArrayList<List<?>> signature = new ArrayList<List<?>>(); signature.add(structFieldNames); signature.add(structFieldObjectInspectors); StandardStructObjectInspector result = cachedStandardStructObjectInspector .get(signature); if (result == null) { result = new StandardStructObjectInspector(structFieldNames, structFieldObjectInspectors); cachedStandardStructObjectInspector.put(signature, result); } return result; }
public static StandardListObjectInspector getStandardListObjectInspector( ObjectInspector listElementObjectInspector) { StandardListObjectInspector result = cachedStandardListObjectInspector .get(listElementObjectInspector); if (result == null) { result = new StandardListObjectInspector(listElementObjectInspector); cachedStandardListObjectInspector.put(listElementObjectInspector, result); } return result; }