use of org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.getStandardMapObjectInspector in project hive by apache.
the class TestGenericUDFSortArray method testSortMap.
@Test
public void testSortMap() throws HiveException {
ObjectInspector[] inputOIs = { ObjectInspectorFactory.getStandardListObjectInspector(ObjectInspectorFactory.getStandardMapObjectInspector(PrimitiveObjectInspectorFactory.writableStringObjectInspector, PrimitiveObjectInspectorFactory.writableIntObjectInspector)) };
udf.initialize(inputOIs);
Map<Text, IntWritable> m1 = new HashMap<Text, IntWritable>();
m1.put(new Text("a"), new IntWritable(4));
m1.put(new Text("b"), new IntWritable(3));
m1.put(new Text("c"), new IntWritable(1));
m1.put(new Text("d"), new IntWritable(2));
Map<Text, IntWritable> m2 = new HashMap<Text, IntWritable>();
m2.put(new Text("d"), new IntWritable(4));
m2.put(new Text("b"), new IntWritable(3));
m2.put(new Text("a"), new IntWritable(1));
m2.put(new Text("c"), new IntWritable(2));
Map<Text, IntWritable> m3 = new HashMap<Text, IntWritable>();
m3.put(new Text("d"), new IntWritable(4));
m3.put(new Text("b"), new IntWritable(3));
m3.put(new Text("a"), new IntWritable(1));
runAndVerify(asList((Object) m1, m2, m3), asList((Object) m3, m2, m1));
}
use of org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.getStandardMapObjectInspector in project hive by apache.
the class TestObjectInspectorUtils method testObjectInspectorUtils.
public void testObjectInspectorUtils() throws Throwable {
try {
ObjectInspector oi1 = ObjectInspectorFactory.getReflectionObjectInspector(Complex.class, ObjectInspectorFactory.ObjectInspectorOptions.THRIFT);
// metadata
assertEquals(Category.STRUCT, oi1.getCategory());
// standard ObjectInspector
StructObjectInspector soi = (StructObjectInspector) ObjectInspectorUtils.getStandardObjectInspector(oi1);
List<? extends StructField> fields = soi.getAllStructFieldRefs();
assertEquals(10, fields.size());
assertEquals(fields.get(0), soi.getStructFieldRef("aint"));
// null
for (int i = 0; i < fields.size(); i++) {
assertNull(soi.getStructFieldData(null, fields.get(i)));
}
// real object
Complex cc = new Complex();
cc.setAint(1);
cc.setAString("test");
List<Integer> c2 = Arrays.asList(new Integer[] { 1, 2, 3 });
cc.setLint(c2);
List<String> c3 = Arrays.asList(new String[] { "one", "two" });
cc.setLString(c3);
List<IntString> c4 = new ArrayList<IntString>();
cc.setLintString(c4);
cc.setMStringString(null);
// standard object
Object c = ObjectInspectorUtils.copyToStandardObject(cc, oi1);
assertEquals(1, soi.getStructFieldData(c, fields.get(0)));
assertEquals("test", soi.getStructFieldData(c, fields.get(1)));
assertEquals(c2, soi.getStructFieldData(c, fields.get(2)));
assertEquals(c3, soi.getStructFieldData(c, fields.get(3)));
assertEquals(c4, soi.getStructFieldData(c, fields.get(4)));
assertNull(soi.getStructFieldData(c, fields.get(5)));
ArrayList<Object> cfields = new ArrayList<Object>();
for (int i = 0; i < 10; i++) {
cfields.add(soi.getStructFieldData(c, fields.get(i)));
}
assertEquals(cfields, soi.getStructFieldsDataAsList(c));
// sub fields
assertEquals(PrimitiveObjectInspectorFactory.javaIntObjectInspector, fields.get(0).getFieldObjectInspector());
assertEquals(PrimitiveObjectInspectorFactory.javaStringObjectInspector, fields.get(1).getFieldObjectInspector());
assertEquals(ObjectInspectorFactory.getStandardListObjectInspector(PrimitiveObjectInspectorFactory.javaIntObjectInspector), fields.get(2).getFieldObjectInspector());
assertEquals(ObjectInspectorFactory.getStandardListObjectInspector(PrimitiveObjectInspectorFactory.javaStringObjectInspector), fields.get(3).getFieldObjectInspector());
assertEquals(ObjectInspectorUtils.getStandardObjectInspector(ObjectInspectorFactory.getStandardListObjectInspector(ObjectInspectorFactory.getReflectionObjectInspector(IntString.class, ObjectInspectorFactory.ObjectInspectorOptions.THRIFT))), fields.get(4).getFieldObjectInspector());
assertEquals(ObjectInspectorFactory.getStandardMapObjectInspector(PrimitiveObjectInspectorFactory.javaStringObjectInspector, PrimitiveObjectInspectorFactory.javaStringObjectInspector), fields.get(5).getFieldObjectInspector());
} catch (Throwable e) {
e.printStackTrace();
throw e;
}
}
use of org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.getStandardMapObjectInspector in project hive by apache.
the class TestThriftObjectInspectors method testThriftObjectInspectors.
public void testThriftObjectInspectors() throws Throwable {
try {
ObjectInspector oi1 = ObjectInspectorFactory.getReflectionObjectInspector(Complex.class, ObjectInspectorFactory.ObjectInspectorOptions.THRIFT);
ObjectInspector oi2 = ObjectInspectorFactory.getReflectionObjectInspector(Complex.class, ObjectInspectorFactory.ObjectInspectorOptions.THRIFT);
assertEquals(oi1, oi2);
// metadata
assertEquals(Category.STRUCT, oi1.getCategory());
StructObjectInspector soi = (StructObjectInspector) oi1;
List<? extends StructField> fields = soi.getAllStructFieldRefs();
assertEquals(10, fields.size());
assertEquals(fields.get(0), soi.getStructFieldRef("aint"));
// null
for (int i = 0; i < fields.size(); i++) {
assertNull(soi.getStructFieldData(null, fields.get(i)));
}
ObjectInspector oi = ObjectInspectorFactory.getReflectionObjectInspector(PropValueUnion.class, ObjectInspectorFactory.ObjectInspectorOptions.THRIFT);
assertNotNull(oi.toString());
// real object
Complex c = new Complex();
c.setAint(1);
c.setAString("test");
List<Integer> c2 = Arrays.asList(new Integer[] { 1, 2, 3 });
c.setLint(c2);
List<String> c3 = Arrays.asList(new String[] { "one", "two" });
c.setLString(c3);
List<IntString> c4 = new ArrayList<IntString>();
c.setLintString(c4);
c.setMStringString(null);
c.setAttributes(null);
c.setUnionField1(null);
c.setUnionField2(null);
c.setUnionField3(null);
assertEquals(1, soi.getStructFieldData(c, fields.get(0)));
assertEquals("test", soi.getStructFieldData(c, fields.get(1)));
assertEquals(c2, soi.getStructFieldData(c, fields.get(2)));
assertEquals(c3, soi.getStructFieldData(c, fields.get(3)));
assertEquals(c4, soi.getStructFieldData(c, fields.get(4)));
assertNull(soi.getStructFieldData(c, fields.get(5)));
assertNull(soi.getStructFieldData(c, fields.get(6)));
assertNull(soi.getStructFieldData(c, fields.get(7)));
assertNull(soi.getStructFieldData(c, fields.get(8)));
assertNull(soi.getStructFieldData(c, fields.get(9)));
ArrayList<Object> cfields = new ArrayList<Object>();
for (int i = 0; i < 10; i++) {
cfields.add(soi.getStructFieldData(c, fields.get(i)));
}
assertEquals(cfields, soi.getStructFieldsDataAsList(c));
// sub fields
assertEquals(PrimitiveObjectInspectorFactory.javaIntObjectInspector, fields.get(0).getFieldObjectInspector());
assertEquals(PrimitiveObjectInspectorFactory.javaStringObjectInspector, fields.get(1).getFieldObjectInspector());
assertEquals(ObjectInspectorFactory.getStandardListObjectInspector(PrimitiveObjectInspectorFactory.javaIntObjectInspector), fields.get(2).getFieldObjectInspector());
assertEquals(ObjectInspectorFactory.getStandardListObjectInspector(PrimitiveObjectInspectorFactory.javaStringObjectInspector), fields.get(3).getFieldObjectInspector());
assertEquals(ObjectInspectorFactory.getStandardListObjectInspector(ObjectInspectorFactory.getReflectionObjectInspector(IntString.class, ObjectInspectorFactory.ObjectInspectorOptions.THRIFT)), fields.get(4).getFieldObjectInspector());
assertEquals(ObjectInspectorFactory.getStandardMapObjectInspector(PrimitiveObjectInspectorFactory.javaStringObjectInspector, PrimitiveObjectInspectorFactory.javaStringObjectInspector), fields.get(5).getFieldObjectInspector());
} catch (Throwable e) {
e.printStackTrace();
throw e;
}
}
use of org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.getStandardMapObjectInspector in project haivvreo by jghoman.
the class AvroObjectInspectorGenerator method createObjectInspectorWorker.
private ObjectInspector createObjectInspectorWorker(TypeInfo ti) throws SerDeException {
// at deserialization and the object inspector will never see the actual union.
if (!supportedCategories(ti))
throw new HaivvreoException("Don't yet support this type: " + ti);
ObjectInspector result;
switch(ti.getCategory()) {
case PRIMITIVE:
PrimitiveTypeInfo pti = (PrimitiveTypeInfo) ti;
result = PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(pti.getPrimitiveCategory());
break;
case STRUCT:
StructTypeInfo sti = (StructTypeInfo) ti;
ArrayList<ObjectInspector> ois = new ArrayList<ObjectInspector>(sti.getAllStructFieldTypeInfos().size());
for (TypeInfo typeInfo : sti.getAllStructFieldTypeInfos()) {
ois.add(createObjectInspectorWorker(typeInfo));
}
result = ObjectInspectorFactory.getStandardStructObjectInspector(sti.getAllStructFieldNames(), ois);
break;
case MAP:
MapTypeInfo mti = (MapTypeInfo) ti;
result = ObjectInspectorFactory.getStandardMapObjectInspector(PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveObjectInspector.PrimitiveCategory.STRING), createObjectInspectorWorker(mti.getMapValueTypeInfo()));
break;
case LIST:
ListTypeInfo ati = (ListTypeInfo) ti;
result = ObjectInspectorFactory.getStandardListObjectInspector(createObjectInspectorWorker(ati.getListElementTypeInfo()));
break;
case UNION:
UnionTypeInfo uti = (UnionTypeInfo) ti;
List<TypeInfo> allUnionObjectTypeInfos = uti.getAllUnionObjectTypeInfos();
List<ObjectInspector> unionObjectInspectors = new ArrayList<ObjectInspector>(allUnionObjectTypeInfos.size());
for (TypeInfo typeInfo : allUnionObjectTypeInfos) {
unionObjectInspectors.add(createObjectInspectorWorker(typeInfo));
}
result = ObjectInspectorFactory.getStandardUnionObjectInspector(unionObjectInspectors);
break;
default:
throw new HaivvreoException("No Hive categories matched: " + ti);
}
return result;
}
use of org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.getStandardMapObjectInspector in project presto by prestodb.
the class HiveWriteUtils method getJavaObjectInspector.
public static ObjectInspector getJavaObjectInspector(Type type) {
if (type.equals(BooleanType.BOOLEAN)) {
return javaBooleanObjectInspector;
} else if (type.equals(BigintType.BIGINT)) {
return javaLongObjectInspector;
} else if (type.equals(IntegerType.INTEGER)) {
return javaIntObjectInspector;
} else if (type.equals(SmallintType.SMALLINT)) {
return javaShortObjectInspector;
} else if (type.equals(TinyintType.TINYINT)) {
return javaByteObjectInspector;
} else if (type.equals(RealType.REAL)) {
return javaFloatObjectInspector;
} else if (type.equals(DoubleType.DOUBLE)) {
return javaDoubleObjectInspector;
} else if (type instanceof VarcharType) {
return writableStringObjectInspector;
} else if (type instanceof CharType) {
return writableHiveCharObjectInspector;
} else if (type.equals(VarbinaryType.VARBINARY)) {
return javaByteArrayObjectInspector;
} else if (type.equals(DateType.DATE)) {
return javaDateObjectInspector;
} else if (type.equals(TimestampType.TIMESTAMP)) {
return javaTimestampObjectInspector;
} else if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
return getPrimitiveJavaObjectInspector(new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale()));
} else if (isArrayType(type)) {
return ObjectInspectorFactory.getStandardListObjectInspector(getJavaObjectInspector(type.getTypeParameters().get(0)));
} else if (isMapType(type)) {
ObjectInspector keyObjectInspector = getJavaObjectInspector(type.getTypeParameters().get(0));
ObjectInspector valueObjectInspector = getJavaObjectInspector(type.getTypeParameters().get(1));
return ObjectInspectorFactory.getStandardMapObjectInspector(keyObjectInspector, valueObjectInspector);
} else if (isRowType(type)) {
return ObjectInspectorFactory.getStandardStructObjectInspector(type.getTypeSignature().getParameters().stream().map(parameter -> parameter.getNamedTypeSignature().getName()).collect(toList()), type.getTypeParameters().stream().map(HiveWriteUtils::getJavaObjectInspector).collect(toList()));
}
throw new IllegalArgumentException("unsupported type: " + type);
}
Aggregations