@Override public int compare(Object o1, MapObjectInspector moi1, Object o2, MapObjectInspector moi2) { int mapsize1 = moi1.getMapSize(o1); int mapsize2 = moi2.getMapSize(o2); if (mapsize1 != mapsize2) { return mapsize1 - mapsize2; } ObjectInspector mvoi1 = moi1.getMapValueObjectInspector(); ObjectInspector mvoi2 = moi2.getMapValueObjectInspector(); Map<?, ?> map1 = moi1.getMap(o1); for (Object mk1: map1.keySet()) { int rc = ObjectInspectorUtils.compare(moi1.getMapValueElement(o1, mk1), mvoi1, moi2.getMapValueElement(o2, mk1), mvoi2, this); if (rc != 0) { return rc; } } return 0; } }
@Override public Object evaluate(DeferredObject[] arguments) throws HiveException { assert (arguments.length == 2); Object index = arguments[1].get(); Object indexObject = converter.convert(index); if (indexObject == null) { return null; } if (mapOI != null) { return mapOI.getMapValueElement(arguments[0].get(), indexObject); } return listOI.getListElement(arguments[0].get(), ((IntWritable)indexObject).get()); }
@Override public Object evaluate(DeferredObject[] arguments) throws HiveException { assert (arguments.length == 2); Object index = arguments[1].get(); Object indexObject = converter.convert(index); if (indexObject == null) { return null; } if (mapOI != null) { return mapOI.getMapValueElement(arguments[0].get(), indexObject); } return listOI.getListElement(arguments[0].get(), ((IntWritable)indexObject).get()); }
inspector.getAllStructFieldRefs().get(10).getFieldObjectInspector(); assertEquals(3, mapOI.getMapSize(map)); assertEquals(4, mapOI.getMapValueElement(map, 2));
@Test public void testDeSerializeMap() throws Exception { // Test that timestamp object can be deserialized Writable w = new Text("{\"country\":\"Switzerland\",\"languages\":\"Italian\",\"religions\": { \"f\": \"v\", \"n\":null} }"); JSONObject result = (JSONObject) instance.deserialize(w); StructObjectInspector soi = (StructObjectInspector) instance.getObjectInspector(); StructField sfr = soi.getStructFieldRef("religions"); assertEquals(sfr.getFieldObjectInspector().getCategory(),ObjectInspector.Category.MAP); MapObjectInspector moi = (MapObjectInspector) sfr.getFieldObjectInspector(); Object val = soi.getStructFieldData(result, sfr) ; assertEquals(2, moi.getMapSize(val)); assertEquals("v", moi.getMapValueElement(val, "f")); ObjectInspector voi = moi.getMapValueObjectInspector(); assertTrue(voi instanceof StringObjectInspector); StringObjectInspector svoi = (StringObjectInspector) voi; assertNull( svoi.getPrimitiveJavaObject(moi.getMapValueElement(val,"n"))); }
@Override public Object evaluate(DeferredObject[] arguments) throws HiveException { assert (arguments.length == 2); Object index = arguments[1].get(); Object indexObject = converter.convert(index); if (indexObject == null) { return null; } Object arg0 = arguments[0].get(); if (arg0 == null) { return null; } return mapOI.getMapValueElement(arg0, indexObject); }
@Override public int compare(Object o1, MapObjectInspector moi1, Object o2, MapObjectInspector moi2) { int mapsize1 = moi1.getMapSize(o1); int mapsize2 = moi2.getMapSize(o2); if (mapsize1 != mapsize2) { return mapsize1 - mapsize2; } ObjectInspector mvoi1 = moi1.getMapValueObjectInspector(); ObjectInspector mvoi2 = moi2.getMapValueObjectInspector(); Map<?, ?> map1 = moi1.getMap(o1); for (Object mk1: map1.keySet()) { int rc = ObjectInspectorUtils.compare(moi1.getMapValueElement(o1, mk1), mvoi1, moi2.getMapValueElement(o2, mk1), mvoi2, this); if (rc != 0) { return rc; } } return 0; } }
@Override public int compare(Object o1, MapObjectInspector moi1, Object o2, MapObjectInspector moi2) { int mapsize1 = moi1.getMapSize(o1); int mapsize2 = moi2.getMapSize(o2); if (mapsize1 != mapsize2) { return mapsize1 - mapsize2; } ObjectInspector mvoi1 = moi1.getMapValueObjectInspector(); ObjectInspector mvoi2 = moi2.getMapValueObjectInspector(); Map<?, ?> map1 = moi1.getMap(o1); for (Object mk1: map1.keySet()) { int rc = ObjectInspectorUtils.compare(moi1.getMapValueElement(o1, mk1), mvoi1, moi2.getMapValueElement(o2, mk1), mvoi2, this); if (rc != 0) { return rc; } } return 0; } }
@Override public int compare(Object o1, MapObjectInspector moi1, Object o2, MapObjectInspector moi2) { int mapsize1 = moi1.getMapSize(o1); int mapsize2 = moi2.getMapSize(o2); if (mapsize1 != mapsize2) { return mapsize1 - mapsize2; } ObjectInspector mvoi1 = moi1.getMapValueObjectInspector(); ObjectInspector mvoi2 = moi2.getMapValueObjectInspector(); Map<?, ?> map1 = moi1.getMap(o1); for (Object mk1: map1.keySet()) { int rc = ObjectInspectorUtils.compare(moi1.getMapValueElement(o1, mk1), mvoi1, moi2.getMapValueElement(o2, mk1), mvoi2, this); if (rc != 0) { return rc; } } return 0; } }
@Override public int compare(Object o1, MapObjectInspector moi1, Object o2, MapObjectInspector moi2) { int mapsize1 = moi1.getMapSize(o1); int mapsize2 = moi2.getMapSize(o2); if (mapsize1 != mapsize2) { return mapsize1 - mapsize2; } ObjectInspector mvoi1 = moi1.getMapValueObjectInspector(); ObjectInspector mvoi2 = moi2.getMapValueObjectInspector(); Map<?, ?> map1 = moi1.getMap(o1); for (Object mk1: map1.keySet()) { int rc = ObjectInspectorUtils.compare(moi1.getMapValueElement(o1, mk1), mvoi1, moi2.getMapValueElement(o2, mk1), mvoi2, this); if (rc != 0) { return rc; } } return 0; } }
@Override public Object evaluate(DeferredObject[] arguments) throws HiveException { assert (arguments.length == 2); Object index = arguments[1].get(); Object indexObject = converter.convert(index); if (indexObject == null) { return null; } if (mapOI != null) { return mapOI.getMapValueElement(arguments[0].get(), indexObject); } return listOI.getListElement(arguments[0].get(), ((IntWritable)indexObject).get()); }
/** {@inheritDoc} */ @Override public Map<FijiColumnName, NavigableMap<Long, FijiCellWritable>> convertToTimeSeries( ObjectInspector objectInspector, Object hiveObject) { Map<FijiColumnName, NavigableMap<Long, FijiCellWritable>> expressionData = Maps.newHashMap(); MapObjectInspector mapObjectInspector = (MapObjectInspector) objectInspector; Map mapData = mapObjectInspector.getMap(hiveObject); for (Object key : mapData.keySet()) { NavigableMap<Long, FijiCellWritable> timeseries = Maps.newTreeMap(); // Assumes that this key is a string. Preconditions.checkState(key instanceof String, "Hive Map key must be a string"); String qualifier = (String) key; StructObjectInspector structObjectInspector = (StructObjectInspector) mapObjectInspector.getMapValueObjectInspector(); Object mapValueObject = mapObjectInspector.getMapValueElement(hiveObject, key); FijiCellWritable fijiCellWritable = new FijiCellWritable(structObjectInspector, mapValueObject); if (fijiCellWritable.hasData()) { timeseries.put(fijiCellWritable.getTimestamp(), fijiCellWritable); } FijiColumnName fijiColumnName = new FijiColumnName(getFamily(), qualifier); expressionData.put(fijiColumnName, timeseries); } return expressionData; } }
@Override public Map<FijiColumnName, NavigableMap<Long, FijiCellWritable>> convertToTimeSeries( ObjectInspector objectInspector, Object hiveObject) { Map<FijiColumnName, NavigableMap<Long, FijiCellWritable>> expressionData = Maps.newHashMap(); MapObjectInspector familyAllValuesOI = (MapObjectInspector) objectInspector; Map familyAllValuesMap = familyAllValuesOI.getMap(hiveObject); for (Object qualifierObject : familyAllValuesMap.keySet()) { String qualifier = (String) qualifierObject; ListObjectInspector allValuesOI = (ListObjectInspector) familyAllValuesOI.getMapValueObjectInspector(); List<Object> allValuesObjects = (List<Object>) familyAllValuesOI.getMapValueElement(hiveObject, qualifierObject); StructObjectInspector timestampedCellOI = (StructObjectInspector) allValuesOI.getListElementObjectInspector(); NavigableMap<Long, FijiCellWritable> timeseries = Maps.newTreeMap(); for (Object obj : allValuesObjects) { FijiCellWritable fijiCellWritable = new FijiCellWritable(timestampedCellOI, obj); if (fijiCellWritable.hasData()) { timeseries.put(fijiCellWritable.getTimestamp(), fijiCellWritable); } } FijiColumnName fijiColumnName = new FijiColumnName(getFamily(), qualifier); expressionData.put(fijiColumnName, timeseries); } return expressionData; } }
@Override public StdData get(StdData key) { MapObjectInspector mapOI = _mapObjectInspector; Object mapObj = _object; Object keyObj; try { keyObj = ((HiveData) key).getUnderlyingDataForObjectInspector(_keyObjectInspector); } catch (RuntimeException e) { // Cannot convert key argument to Map's KeyOI. So convert both the map and the key arg to // objects having standard OIs mapOI = (MapObjectInspector) getStandardObjectInspector(); mapObj = getStandardObject(); keyObj = ((HiveData) key).getStandardObject(); } return HiveWrapper.createStdData( mapOI.getMapValueElement(mapObj, keyObj), mapOI.getMapValueObjectInspector(), _stdFactory); }
@Override public StdData get(StdData key) { MapObjectInspector mapOI = _mapObjectInspector; Object mapObj = _object; Object keyObj; try { keyObj = ((HiveData) key).getUnderlyingDataForObjectInspector(_keyObjectInspector); } catch (RuntimeException e) { // Cannot convert key argument to Map's KeyOI. So convert both the map and the key arg to // objects having standard OIs mapOI = (MapObjectInspector) getStandardObjectInspector(); mapObj = getStandardObject(); keyObj = ((HiveData) key).getStandardObject(); } return HiveWrapper.createStdData( mapOI.getMapValueElement(mapObj, keyObj), mapOI.getMapValueObjectInspector(), _stdFactory); }
@Override public Object evaluate(DeferredObject[] arguments) throws HiveException { assert (arguments.length == 2); Object main = arguments[0].get(); Object index = arguments[1].get(); if (mapOI != null) { Object indexObject; if (mapKeyPreferWritable) { indexObject = indexOI.getPrimitiveWritableObject(index); } else { indexObject = indexOI.getPrimitiveJavaObject(index); } return mapOI.getMapValueElement(main, indexObject); } else { assert (listOI != null); int intIndex = 0; try { intIndex = PrimitiveObjectInspectorUtils.getInt(index, indexOI); } catch (NullPointerException e) { // If index is null, we should return null. return null; } catch (NumberFormatException e) { // If index is not a number, we should return null. return null; } return listOI.getListElement(main, intIndex); } }