Java Code Examples for org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils#getInt()

The following examples show how to use org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils#getInt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TileX2LonUDF.java    From incubator-hivemall with Apache License 2.0 6 votes vote down vote up
@Override
public DoubleWritable evaluate(DeferredObject[] arguments) throws HiveException {
    Object arg0 = arguments[0].get();
    Object arg1 = arguments[1].get();

    if (arg0 == null) {
        return null;
    }
    if (arg1 == null) {
        throw new UDFArgumentException("zoom level should not be null");
    }

    int x = PrimitiveObjectInspectorUtils.getInt(arg0, xOI);
    int zoom = PrimitiveObjectInspectorUtils.getInt(arg1, zoomOI);
    Preconditions.checkArgument(zoom >= 0, "Invalid zoom level", UDFArgumentException.class);

    final double lon;
    try {
        lon = GeoSpatialUtils.tilex2lon(x, zoom);
    } catch (IllegalArgumentException ex) {
        throw new UDFArgumentException(ex);
    }

    result.set(lon);
    return result;
}
 
Example 2
Source File: SignalNoiseRatioUDAF.java    From incubator-hivemall with Apache License 2.0 6 votes vote down vote up
private static int hotIndex(@Nonnull List<?> labels, PrimitiveObjectInspector labelOI)
        throws UDFArgumentException {
    final int nClasses = labels.size();

    int clazz = -1;
    for (int i = 0; i < nClasses; i++) {
        final int label = PrimitiveObjectInspectorUtils.getInt(labels.get(i), labelOI);
        if (label == 1) {// assumes one hot encoding 
            if (clazz != -1) {
                throw new UDFArgumentException(
                    "Specify one-hot vectorized array. Multiple hot elements found.");
            }
            clazz = i;
        } else {
            if (label != 0) {
                throw new UDFArgumentException(
                    "Assumed one-hot encoding (0/1) but found an invalid label: " + label);
            }
        }
    }
    if (clazz == -1) {
        throw new UDFArgumentException(
            "Specify one-hot vectorized array for label. Hot element not found.");
    }
    return clazz;
}
 
Example 3
Source File: DataToSketchUDAF.java    From incubator-datasketches-hive with Apache License 2.0 6 votes vote down vote up
private void initializeState(final UnionState state, final Object[] parameters) {
  int sketchSize = DEFAULT_NOMINAL_ENTRIES;
  if (nominalEntriesObjectInspector != null) {
    sketchSize = PrimitiveObjectInspectorUtils.getInt(parameters[1], nominalEntriesObjectInspector);
  }
  float samplingProbability = UnionState.DEFAULT_SAMPLING_PROBABILITY;
  if (samplingProbabilityObjectInspector != null) {
    samplingProbability = PrimitiveObjectInspectorUtils.getFloat(parameters[2],
        samplingProbabilityObjectInspector);
  }
  long seed = DEFAULT_UPDATE_SEED;
  if (seedObjectInspector != null) {
    seed = PrimitiveObjectInspectorUtils.getLong(parameters[3], seedObjectInspector);
  }
  state.init(sketchSize, samplingProbability, seed);
}
 
Example 4
Source File: SlimUDTF.java    From incubator-hivemall with Apache License 2.0 6 votes vote down vote up
@Nonnull
private static Int2FloatMap int2floatMap(final int item, @Nonnull final Map<?, ?> map,
        @Nonnull final PrimitiveObjectInspector keyOI,
        @Nonnull final PrimitiveObjectInspector valueOI, @Nullable final FloatMatrix dataMatrix,
        @Nullable Int2FloatMap dst) {
    if (dst == null) {
        dst = new Int2FloatOpenHashMap(map.size());
        dst.defaultReturnValue(0.f);
    } else {
        dst.clear();
    }

    for (Map.Entry<?, ?> entry : map.entrySet()) {
        float rating = PrimitiveObjectInspectorUtils.getFloat(entry.getValue(), valueOI);
        if (rating == 0.f) {
            continue;
        }
        int user = PrimitiveObjectInspectorUtils.getInt(entry.getKey(), keyOI);
        dst.put(user, rating);
        if (dataMatrix != null) {
            dataMatrix.set(item, user, rating);
        }
    }

    return dst;
}
 
Example 5
Source File: PLSAPredictUDAF.java    From incubator-hivemall with Apache License 2.0 6 votes vote down vote up
@Override
public void iterate(@SuppressWarnings("deprecation") AggregationBuffer agg,
        Object[] parameters) throws HiveException {
    PLSAPredictAggregationBuffer myAggr = (PLSAPredictAggregationBuffer) agg;

    if (parameters[0] == null || parameters[1] == null || parameters[2] == null
            || parameters[3] == null) {
        return;
    }

    String word = PrimitiveObjectInspectorUtils.getString(parameters[0], wordOI);
    float value = PrimitiveObjectInspectorUtils.getFloat(parameters[1], valueOI);
    int label = PrimitiveObjectInspectorUtils.getInt(parameters[2], labelOI);
    float prob = PrimitiveObjectInspectorUtils.getFloat(parameters[3], probOI);

    myAggr.iterate(word, value, label, prob);
}
 
Example 6
Source File: AUCUDAF.java    From incubator-hivemall with Apache License 2.0 6 votes vote down vote up
@Override
public void iterate(AggregationBuffer agg, Object[] parameters) throws HiveException {
    ClassificationAUCAggregationBuffer myAggr = (ClassificationAUCAggregationBuffer) agg;

    if (parameters[0] == null) {
        return;
    }
    if (parameters[1] == null) {
        return;
    }

    double score = HiveUtils.getDouble(parameters[0], scoreOI);
    if (score < 0.0d || score > 1.0d) {
        throw new UDFArgumentException("score value MUST be in range [0,1]: " + score);
    }

    int label = PrimitiveObjectInspectorUtils.getInt(parameters[1], labelOI);
    if (label == -1) {
        label = 0;
    } else if (label != 0 && label != 1) {
        throw new UDFArgumentException("label MUST be 0/1 or -1/1: " + label);
    }

    myAggr.iterate(score, label);
}
 
Example 7
Source File: TileY2LatUDF.java    From incubator-hivemall with Apache License 2.0 6 votes vote down vote up
@Override
public DoubleWritable evaluate(DeferredObject[] arguments) throws HiveException {
    Object arg0 = arguments[0].get();
    Object arg1 = arguments[1].get();

    if (arg0 == null) {
        return null;
    }
    if (arg1 == null) {
        throw new UDFArgumentException("zoom level should not be null");
    }

    int y = PrimitiveObjectInspectorUtils.getInt(arg0, yOI);
    int zoom = PrimitiveObjectInspectorUtils.getInt(arg1, zoomOI);
    Preconditions.checkArgument(zoom >= 0, "Invalid zoom level", UDFArgumentException.class);

    final double lat;
    try {
        lat = GeoSpatialUtils.tiley2lat(y, zoom);
    } catch (IllegalArgumentException ex) {
        throw new UDFArgumentException(ex);
    }

    result.set(lat);
    return result;
}
 
Example 8
Source File: RandomForestEnsembleUDAF.java    From incubator-hivemall with Apache License 2.0 6 votes vote down vote up
@Override
public void iterate(AggregationBuffer agg, Object[] parameters) throws HiveException {
    RfAggregationBufferV2 buf = (RfAggregationBufferV2) agg;

    Preconditions.checkNotNull(parameters[0]);
    int yhat = PrimitiveObjectInspectorUtils.getInt(parameters[0], yhatOI);
    Preconditions.checkNotNull(parameters[1]);
    double[] posteriori =
            HiveUtils.asDoubleArray(parameters[1], posterioriOI, posterioriElemOI);

    double weight = 1.0d;
    if (parameters.length == 3) {
        Preconditions.checkNotNull(parameters[2]);
        weight = PrimitiveObjectInspectorUtils.getDouble(parameters[2], weightOI);
    }

    buf.iterate(yhat, weight, posteriori);
}
 
Example 9
Source File: PrecisionUDAF.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Override
public void iterate(@SuppressWarnings("deprecation") AggregationBuffer agg,
        Object[] parameters) throws HiveException {
    PrecisionAggregationBuffer myAggr = (PrecisionAggregationBuffer) agg;

    List<?> recommendList = recommendListOI.getList(parameters[0]);
    if (recommendList == null) {
        recommendList = Collections.emptyList();
    }
    List<?> truthList = truthListOI.getList(parameters[1]);
    if (truthList == null) {
        return;
    }

    int recommendSize = recommendList.size();
    if (parameters.length == 3) {
        recommendSize =
                PrimitiveObjectInspectorUtils.getInt(parameters[2], recommendSizeOI);
        if (recommendSize < 0) {
            throw new UDFArgumentException(
                "The third argument `int recommendSize` must be in greater than or equals to 0: "
                        + recommendSize);
        }
    }

    myAggr.iterate(recommendList, truthList, recommendSize);
}
 
Example 10
Source File: UnionSketchUDAF.java    From incubator-datasketches-hive with Apache License 2.0 5 votes vote down vote up
private void initializeState(final UnionState state, final Object[] parameters) {
  int lgK = DEFAULT_LG_K;
  if (lgKInspector_ != null) {
    lgK = PrimitiveObjectInspectorUtils.getInt(parameters[1], lgKInspector_);
  }
  TgtHllType type = DEFAULT_HLL_TYPE;
  if (hllTypeInspector_ != null) {
    type = TgtHllType.valueOf(PrimitiveObjectInspectorUtils.getString(parameters[2], hllTypeInspector_));
  }
  state.init(lgK, type);
}
 
Example 11
Source File: DataToSketchUDAF.java    From incubator-datasketches-hive with Apache License 2.0 5 votes vote down vote up
private void initializeState(final State state, final Object[] parameters) {
  int lgK = DEFAULT_LG_K;
  if (lgKInspector_ != null) {
    lgK = PrimitiveObjectInspectorUtils.getInt(parameters[1], lgKInspector_);
  }
  long seed = DEFAULT_UPDATE_SEED;
  if (seedInspector_ != null) {
    seed = PrimitiveObjectInspectorUtils.getLong(parameters[2], seedInspector_);
  }
  state.init(lgK, seed);
}
 
Example 12
Source File: SlimUDTF.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Override
public void process(@Nonnull Object[] args) throws HiveException {
    if (_weightMatrix == null) {// initialize variables
        this._weightMatrix = new DoKFloatMatrix();
        if (numIterations >= 2) {
            this._dataMatrix = new DoKFloatMatrix();
        }
        this._nnzKNNi = new MutableInt();
    }

    final int itemI = PrimitiveObjectInspectorUtils.getInt(args[0], itemIOI);

    if (itemI != _previousItemId || _ri == null) {
        // cache Ri and kNNi
        this._ri =
                int2floatMap(itemI, riOI.getMap(args[1]), riKeyOI, riValueOI, _dataMatrix, _ri);
        this._kNNi = kNNentries(args[2], knnItemsOI, knnItemsKeyOI, knnItemsValueOI,
            knnItemsValueKeyOI, knnItemsValueValueOI, _kNNi, _nnzKNNi);

        final int numKNNItems = _nnzKNNi.getValue();
        if (numIterations >= 2 && numKNNItems >= 1) {
            recordTrainingInput(itemI, _kNNi, numKNNItems);
        }
        this._previousItemId = itemI;
    }

    int itemJ = PrimitiveObjectInspectorUtils.getInt(args[3], itemJOI);
    Int2FloatMap rj =
            int2floatMap(itemJ, rjOI.getMap(args[4]), rjKeyOI, rjValueOI, _dataMatrix);

    train(itemI, _ri, _kNNi, itemJ, rj);
    _observedTrainingExamples++;
}
 
Example 13
Source File: FMeasureUDAF.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
private static int asIntLabel(@Nonnull final Object o,
        @Nonnull final PrimitiveObjectInspector intOI) throws UDFArgumentException {
    final int value = PrimitiveObjectInspectorUtils.getInt(o, intOI);
    switch (value) {
        case 1:
            return 1;
        case 0:
        case -1:
            return 0;
        default:
            throw new UDFArgumentException("Int label must be 1, 0 or -1: " + value);
    }
}
 
Example 14
Source File: RandomForestEnsembleUDAF.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Override
public void iterate(AggregationBuffer agg, Object[] parameters) throws HiveException {
    RfAggregationBufferV1 buf = (RfAggregationBufferV1) agg;

    Preconditions.checkNotNull(parameters[0]);
    int yhat = PrimitiveObjectInspectorUtils.getInt(parameters[0], yhatOI);

    buf.iterate(yhat);
}
 
Example 15
Source File: DataToSketchUDAF.java    From incubator-datasketches-hive with Apache License 2.0 5 votes vote down vote up
private void initializeState(final SketchState<U, S> state, final Object[] data) {
  int nominalNumEntries = DEFAULT_NOMINAL_ENTRIES;
  if (nominalNumEntriesInspector_ != null) {
    nominalNumEntries = PrimitiveObjectInspectorUtils.getInt(data[2], nominalNumEntriesInspector_);
  }
  float samplingProbability = DEFAULT_SAMPLING_PROBABILITY;
  if (samplingProbabilityInspector_ != null) {
    samplingProbability = PrimitiveObjectInspectorUtils.getFloat(data[3],
        samplingProbabilityInspector_);
  }
  state.init(nominalNumEntries, samplingProbability, getSummaryFactory(data));
}
 
Example 16
Source File: UnionSketchUDAF.java    From incubator-datasketches-hive with Apache License 2.0 5 votes vote down vote up
private void initializeState(final UnionState state, final Object[] parameters) {
  int lgK = DEFAULT_LG_K;
  if (lgKInspector_ != null) {
    lgK = PrimitiveObjectInspectorUtils.getInt(parameters[1], lgKInspector_);
  }
  long seed = DEFAULT_UPDATE_SEED;
  if (seedInspector_ != null) {
    seed = PrimitiveObjectInspectorUtils.getLong(parameters[2], seedInspector_);
  }
  state.init(lgK, seed);
}
 
Example 17
Source File: DataToItemsSketchUDAF.java    From incubator-datasketches-hive with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public void iterate(final AggregationBuffer buf, final Object[] data) throws HiveException {
  if (data[0] == null) { return; }
  @SuppressWarnings("unchecked")
  final ItemsUnionState<T> state = (ItemsUnionState<T>) buf;
  if (!state.isInitialized() && (kObjectInspector != null)) {
    final int k = PrimitiveObjectInspectorUtils.getInt(data[1], kObjectInspector);
    state.init(k);
  }
  state.update(extractValue(data[0], inputObjectInspector));
}
 
Example 18
Source File: BitsCollectUDAF.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Override
public void iterate(@SuppressWarnings("deprecation") AggregationBuffer aggr,
        Object[] parameters) throws HiveException {
    assert (parameters.length == 1);
    Object arg = parameters[0];
    if (arg != null) {
        int index = PrimitiveObjectInspectorUtils.getInt(arg, inputOI);
        if (index < 0) {
            throw new UDFArgumentException(
                "Specified index SHOULD NOT be negative: " + index);
        }
        ArrayAggregationBuffer agg = (ArrayAggregationBuffer) aggr;
        agg.bitset.set(index);
    }
}
 
Example 19
Source File: SubarrayUDF.java    From incubator-hivemall with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
public List<Object> evaluate(@Nonnull DeferredObject[] args) throws HiveException {
    Object arg0 = args[0].get();
    if (arg0 == null) {
        return null;
    }
    result.clear();

    final int size = valuesOI.getListLength(arg0);

    Object arg1 = args[1].get();
    if (arg1 == null) {
        throw new UDFArgumentException("2nd argument MUST NOT be null");
    }
    int fromIndex = PrimitiveObjectInspectorUtils.getInt(arg1, fromIndexOI);
    if (fromIndex < 0) {
        fromIndex = 0;
    }

    int toIndex;
    if (args.length == 3) {
        Object arg2 = args[2].get();
        if (arg2 == null) {
            throw new UDFArgumentException("3rd argument MUST NOT be null");
        }
        toIndex = PrimitiveObjectInspectorUtils.getInt(arg2, toIndexOI);
        if (toIndex > size) {
            toIndex = size;
        }
    } else {
        toIndex = size;
    }

    for (int i = fromIndex; i < toIndex; i++) {
        Object e = valuesOI.getListElement(arg0, i);
        result.add(e);
    }

    return result;
}
 
Example 20
Source File: EachTopKUDTF.java    From incubator-hivemall with Apache License 2.0 4 votes vote down vote up
@Override
public void process(Object[] args) throws HiveException {
    final Object arg1 = args[1];
    if (isSameGroup(arg1) == false) {
        Object group = ObjectInspectorUtils.copyToStandardObject(arg1, argOIs[1],
            ObjectInspectorCopyOption.DEFAULT); // arg1 and group may be null
        this._previousGroup = group;
        if (_queue != null) {
            drainQueue();
        }

        if (_constantK == false) {
            final int k = PrimitiveObjectInspectorUtils.getInt(args[0], kOI);
            if (k == 0) {
                return;
            }
            if (k != _prevK) {
                this._queue = getQueue(k);
                this._prevK = k;
            }
        }
    }

    final double key = PrimitiveObjectInspectorUtils.getDouble(args[2], cmpKeyOI);
    final Object[] row;
    TupleWithKey tuple = this._tuple;
    if (_tuple == null) {
        row = new Object[args.length - 1];
        tuple = new TupleWithKey(key, row);
        this._tuple = tuple;
    } else {
        row = tuple.getRow();
        tuple.setKey(key);
    }
    for (int i = 3; i < args.length; i++) {
        Object arg = args[i];
        ObjectInspector argOI = argOIs[i];
        row[i - 1] = ObjectInspectorUtils.copyToStandardObject(arg, argOI,
            ObjectInspectorCopyOption.DEFAULT);
    }

    if (_queue.offer(tuple)) {
        this._tuple = null;
    }
}