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

The following examples show how to use org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils#getDouble() . 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: FMPredictGenericUDAF.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 {
    if (parameters[0] == null) {
        return;
    }
    FMPredictAggregationBuffer buf = (FMPredictAggregationBuffer) agg;

    double w = PrimitiveObjectInspectorUtils.getDouble(parameters[0], wOI);
    if (parameters[1] == null || /* for TD */vOI.getListLength(parameters[1]) == 0) {// Vif was null
        buf.iterate(w);
    } else {
        if (parameters[2] == null) {
            throw new UDFArgumentException("The third argument Xj must not be null");
        }
        double x = PrimitiveObjectInspectorUtils.getDouble(parameters[2], xOI);
        buf.iterate(w, x, parameters[1], vOI, vElemOI);
    }
}
 
Example 2
Source File: ArrayAvgGenericUDAF.java    From incubator-hivemall with Apache License 2.0 6 votes vote down vote up
void doIterate(@Nonnull final Object tuple, @Nonnull ListObjectInspector listOI,
        @Nonnull PrimitiveObjectInspector elemOI) throws HiveException {
    final int size = listOI.getListLength(tuple);
    if (_size == -1) {
        init(size);
    }
    if (size != _size) {// a corner case
        throw new HiveException(
            "Mismatch in the number of elements at tuple: " + tuple.toString());
    }
    final double[] sum = _sum;
    final long[] count = _count;
    for (int i = 0, len = size; i < len; i++) {
        Object o = listOI.getListElement(tuple, i);
        if (o != null) {
            double v = PrimitiveObjectInspectorUtils.getDouble(o, elemOI);
            sum[i] += v;
            count[i] += 1L;
        }
    }
}
 
Example 3
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 4
Source File: ChangeFinder1D.java    From incubator-hivemall with Apache License 2.0 6 votes vote down vote up
@Override
public void update(@Nonnull final Object arg, @Nonnull final double[] outScores)
        throws HiveException {
    double x = PrimitiveObjectInspectorUtils.getDouble(arg, oi);

    // [Stage#1] Outlier Detection
    xRing.add(x).toArray(xSeries, false /* LIFO */);
    int k1 = xRing.size() - 1;
    double x_hat = sdar1.update(xSeries, k1);

    double scoreX = (k1 == 0.d) ? 0.d : loss(sdar1, x, x_hat, lossFunc1);
    // smoothing
    double y = ChangeFinderUDF.smoothing(outlierScores.add(scoreX));

    // [Stage#2] Change-point Detection
    yRing.add(y).toArray(ySeries, false /* LIFO */);
    int k2 = yRing.size() - 1;
    double y_hat = sdar2.update(ySeries, k2);

    // <LogLoss>
    double lossY = (k2 == 0.d) ? 0.d : loss(sdar2, y, y_hat, lossFunc2);
    double scoreY = ChangeFinderUDF.smoothing(changepointScores.add(lossY));

    outScores[0] = scoreX;
    outScores[1] = scoreY;
}
 
Example 5
Source File: HiveUtils.java    From incubator-hivemall with Apache License 2.0 6 votes vote down vote up
@Nonnull
public static void toDoubleArray(@Nullable final Object argObj,
        @Nonnull final ListObjectInspector listOI,
        @Nonnull final PrimitiveObjectInspector elemOI, @Nonnull final double[] out,
        final boolean avoidNull) throws UDFArgumentException {
    if (argObj == null) {
        return;
    }
    final int length = listOI.getListLength(argObj);
    if (out.length != length) {
        throw new UDFArgumentException(
            "Dimension mismatched. Expected: " + out.length + ", Actual: " + length);
    }
    for (int i = 0; i < length; i++) {
        Object o = listOI.getListElement(argObj, i);
        if (o == null) {
            if (avoidNull) {
                continue;
            }
            throw new UDFArgumentException("Found null at index " + i);
        }
        out[i] = PrimitiveObjectInspectorUtils.getDouble(o, elemOI);
    }
    return;
}
 
Example 6
Source File: VectorDotUDF.java    From incubator-hivemall with Apache License 2.0 6 votes vote down vote up
@Override
public List<Double> dot(@Nonnull Object x, @Nonnull Object y) throws HiveException {
    final double yd = PrimitiveObjectInspectorUtils.getDouble(y, yOI);

    final int xLen = xListOI.getListLength(x);
    final Double[] arr = new Double[xLen];
    for (int i = 0; i < xLen; i++) {
        Object xi = xListOI.getListElement(x, i);
        if (xi == null) {
            continue;
        }
        double xd = PrimitiveObjectInspectorUtils.getDouble(xi, xElemOI);
        double v = xd * yd;
        arr[i] = Double.valueOf(v);
    }

    return Arrays.asList(arr);
}
 
Example 7
Source File: Lon2TileXUDF.java    From incubator-hivemall with Apache License 2.0 6 votes vote down vote up
@Override
public IntWritable 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");
    }

    double lon = PrimitiveObjectInspectorUtils.getDouble(arg0, lonOI);
    int zoom = PrimitiveObjectInspectorUtils.getInt(arg1, zoomOI);
    Preconditions.checkArgument(zoom >= 0, "Invalid zoom level", UDFArgumentException.class);

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

    result.set(x);
    return result;
}
 
Example 8
Source File: ST_Bin.java    From spatial-framework-for-hadoop with Apache License 2.0 6 votes vote down vote up
@Override
public Object evaluate(DeferredObject[] args) throws HiveException {
	double binSize = PrimitiveObjectInspectorUtils.getDouble(args[0].get(), oiBinSize);

	if (!binSizeIsConstant || bins == null) {
		bins = new BinUtils(binSize);
	} 

	OGCPoint point = geomHelper.getPoint(args);

	if (point == null) {
		return null;
	}

	return bins.getId(point.X(), point.Y());
}
 
Example 9
Source File: Lat2TileYUDF.java    From incubator-hivemall with Apache License 2.0 6 votes vote down vote up
@Override
public IntWritable 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");
    }

    double lat = PrimitiveObjectInspectorUtils.getDouble(arg0, latOI);
    int zoom = PrimitiveObjectInspectorUtils.getInt(arg1, zoomOI);
    Preconditions.checkArgument(zoom >= 0, "Invalid zoom level", UDFArgumentException.class);

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

    result.set(y);
    return result;
}
 
Example 10
Source File: XGBoostOnlinePredictUDTF.java    From incubator-hivemall with Apache License 2.0 6 votes vote down vote up
@Nonnull
private FVec parseDenseFeatures(@Nonnull Object argObj) throws UDFArgumentException {
    final int length = featureListOI.getListLength(argObj);
    final double[] values = new double[length];
    for (int i = 0; i < length; i++) {
        final Object o = featureListOI.getListElement(argObj, i);
        final double v;
        if (o == null) {
            v = Double.NaN;
        } else {
            v = PrimitiveObjectInspectorUtils.getDouble(o, featureElemOI);
        }
        values[i] = v;

    }
    return FVec.Transformer.fromArray(values, false);
}
 
Example 11
Source File: HaversineDistanceUDF.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Override
public DoubleWritable evaluate(DeferredObject[] arguments) throws HiveException {
    Object arg0 = arguments[0].get();
    Object arg1 = arguments[1].get();
    Object arg2 = arguments[2].get();
    Object arg3 = arguments[3].get();

    if (arg0 == null || arg1 == null || arg2 == null || arg3 == null) {
        return null;
    }
    double lat1 = PrimitiveObjectInspectorUtils.getDouble(arg0, lat1OI);
    double lon1 = PrimitiveObjectInspectorUtils.getDouble(arg1, lon1OI);
    double lat2 = PrimitiveObjectInspectorUtils.getDouble(arg2, lat2OI);
    double lon2 = PrimitiveObjectInspectorUtils.getDouble(arg3, lon2OI);

    final double distance;
    try {
        distance = GeoSpatialUtils.haversineDistance(lat1, lon1, lat2, lon2);
    } catch (IllegalArgumentException ex) {
        throw new UDFArgumentException(ex);
    }

    if (inMiles) {
        double miles = distance / 1.609344d;
        result.set(miles);
    } else {
        result.set(distance);
    }

    return result;
}
 
Example 12
Source File: ArrayOfDoublesSketchState.java    From incubator-datasketches-hive with Apache License 2.0 5 votes vote down vote up
void update(final Object[] data, final PrimitiveObjectInspector keyInspector,
    final PrimitiveObjectInspector[] valuesInspectors) {
  final double[] values = new double[valuesInspectors.length];
  for (int i = 0; i < values.length; i++) {
    values[i] = PrimitiveObjectInspectorUtils.getDouble(data[i + 1], valuesInspectors[i]);
  }
  switch (keyInspector.getPrimitiveCategory()) {
  case BINARY:
    sketch_.update(PrimitiveObjectInspectorUtils.getBinary(data[0], keyInspector).copyBytes(), values);
    return;
  case BYTE:
    sketch_.update(PrimitiveObjectInspectorUtils.getByte(data[0], keyInspector), values);
    return;
  case DOUBLE:
    sketch_.update(PrimitiveObjectInspectorUtils.getDouble(data[0], keyInspector), values);
    return;
  case FLOAT:
    sketch_.update(PrimitiveObjectInspectorUtils.getFloat(data[0], keyInspector), values);
    return;
  case INT:
    sketch_.update(PrimitiveObjectInspectorUtils.getInt(data[0], keyInspector), values);
    return;
  case LONG:
    sketch_.update(PrimitiveObjectInspectorUtils.getLong(data[0], keyInspector), values);
    return;
  case STRING:
    sketch_.update(PrimitiveObjectInspectorUtils.getString(data[0], keyInspector), values);
    return;
  default:
    throw new IllegalArgumentException(
        "Unrecongnized input data type, please use data of type: "
    + "byte, double, float, int, long, or string only.");
  }
}
 
Example 13
Source File: TileUDF.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Override
public LongWritable evaluate(DeferredObject[] arguments) throws HiveException {
    Object arg0 = arguments[0].get();
    Object arg1 = arguments[1].get();
    Object arg2 = arguments[2].get();

    if (arg0 == null || arg1 == null) {
        return null;
    }
    if (arg2 == null) {
        throw new UDFArgumentException("zoom level is null");
    }

    double lat = PrimitiveObjectInspectorUtils.getDouble(arg0, latOI);
    double lon = PrimitiveObjectInspectorUtils.getDouble(arg1, lonOI);
    int zoom = PrimitiveObjectInspectorUtils.getInt(arg2, zoomOI);
    Preconditions.checkArgument(zoom >= 0, "Invalid zoom level", UDFArgumentException.class);

    final long tile;
    try {
        tile = GeoSpatialUtils.tile(lat, lon, zoom);
    } catch (IllegalArgumentException ex) {
        throw new UDFArgumentException(ex);
    }

    result.set(tile);
    return result;
}
 
Example 14
Source File: ST_BinEnvelope.java    From spatial-framework-for-hadoop with Apache License 2.0 5 votes vote down vote up
@Override
public Object evaluate(DeferredObject[] args) throws HiveException {
	double binSize = PrimitiveObjectInspectorUtils.getDouble(args[0].get(), oiBinSize);
	
	
	if (!binSizeIsConstant || bins == null) {
		bins = new BinUtils(binSize);
	} 
			
	Envelope env = new Envelope();
	
	if (oiBinId != null) {
		// argument 1 is a number, attempt to get the envelope with bin ID
		if (args[1].get() == null) {
			// null bin ID argument usually means the source point was null or failed to parse
			return null; 
		}
		
		long binId = PrimitiveObjectInspectorUtils.getLong(args[1].get(), oiBinId);
		bins.queryEnvelope(binId, env);
	} else {
		// argument 1 is a geometry, attempt to get the envelope with a point
		OGCPoint point = binPoint.getPoint(args);
		
		if (point == null) {
			return null;
		}
		
		bins.queryEnvelope(point.X(), point.Y(), env);
	}

	return GeometryUtils.geometryToEsriShapeBytesWritable(env, 0, OGCType.ST_POLYGON);
}
 
Example 15
Source File: FMPredictGenericUDAF.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
void iterate(final double Wj, final double Xj, @Nonnull final Object Vif,
        @Nonnull final ListObjectInspector vOI,
        @Nonnull final PrimitiveObjectInspector vElemOI) throws HiveException {
    this.ret += (Wj * Xj);

    final int factors = vOI.getListLength(Vif);
    if (factors < 1) {
        throw new HiveException("# of Factor should be more than 0: " + factors);
    }

    if (sumVjXj == null) {
        this.sumVjXj = new double[factors];
        this.sumV2X2 = new double[factors];
    } else if (sumVjXj.length != factors) {
        throw new HiveException("Mismatch in the number of factors");
    }

    for (int f = 0; f < factors; f++) {
        Object o = vOI.getListElement(Vif, f);
        if (o == null) {
            throw new HiveException("Vj" + f + " should not be null");
        }
        double v = PrimitiveObjectInspectorUtils.getDouble(o, vElemOI);
        double vx = v * Xj;

        sumVjXj[f] += vx;
        sumV2X2[f] += (vx * vx);
    }
}
 
Example 16
Source File: RandomForestRegressionUDTF.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Override
public void process(Object[] args) throws HiveException {
    if (args[0] == null) {
        throw new HiveException("array<double> features was null");
    }
    parseFeatures(args[0], matrixBuilder);
    double target = PrimitiveObjectInspectorUtils.getDouble(args[1], targetOI);
    targets.add(target);
}
 
Example 17
Source File: HiveUtils.java    From incubator-hivemall with Apache License 2.0 4 votes vote down vote up
public static double getDouble(@Nullable Object o, @Nonnull PrimitiveObjectInspector oi) {
    if (o == null) {
        return 0.d;
    }
    return PrimitiveObjectInspectorUtils.getDouble(o, oi);
}
 
Example 18
Source File: SignalNoiseRatioUDAF.java    From incubator-hivemall with Apache License 2.0 4 votes vote down vote up
@Override
public void iterate(@SuppressWarnings("deprecation") AggregationBuffer agg,
        Object[] parameters) throws HiveException {
    final Object featuresObj = parameters[0];
    final Object labelsObj = parameters[1];

    Preconditions.checkNotNull(featuresObj);
    Preconditions.checkNotNull(labelsObj);

    final SignalNoiseRatioAggregationBuffer myAgg = (SignalNoiseRatioAggregationBuffer) agg;

    final List<?> labels = labelsOI.getList(labelsObj);
    final int nClasses = labels.size();
    Preconditions.checkArgument(nClasses >= 2, UDFArgumentException.class);

    final List<?> features = featuresOI.getList(featuresObj);
    final int nFeatures = features.size();
    Preconditions.checkArgument(nFeatures >= 1, UDFArgumentException.class);

    if (myAgg.counts == null) {
        myAgg.init(nClasses, nFeatures);
    } else {
        Preconditions.checkArgument(nClasses == myAgg.counts.length,
            UDFArgumentException.class);
        Preconditions.checkArgument(nFeatures == myAgg.means[0].length,
            UDFArgumentException.class);
    }

    // incrementally calculates means and variance
    // http://i.stanford.edu/pub/cstr/reports/cs/tr/79/773/CS-TR-79-773.pdf
    final int clazz = hotIndex(labels, labelOI);
    final long n = myAgg.counts[clazz];
    myAgg.counts[clazz]++;
    for (int i = 0; i < nFeatures; i++) {
        final double x =
                PrimitiveObjectInspectorUtils.getDouble(features.get(i), featureOI);
        final double meanN = myAgg.means[clazz][i];
        final double varianceN = myAgg.variances[clazz][i];
        myAgg.means[clazz][i] = (n * meanN + x) / (n + 1.d);
        myAgg.variances[clazz][i] =
                (n * varianceN + (x - meanN) * (x - myAgg.means[clazz][i])) / (n + 1.d);
    }
}
 
Example 19
Source File: SignalNoiseRatioUDAF.java    From incubator-hivemall with Apache License 2.0 4 votes vote down vote up
@Override
public void merge(@SuppressWarnings("deprecation") AggregationBuffer agg, Object other)
        throws HiveException {
    if (other == null) {
        return;
    }

    final SignalNoiseRatioAggregationBuffer myAgg = (SignalNoiseRatioAggregationBuffer) agg;

    final List<?> counts =
            countsOI.getList(structOI.getStructFieldData(other, countsField));
    final List<?> means = meansOI.getList(structOI.getStructFieldData(other, meansField));
    final List<?> variances =
            variancesOI.getList(structOI.getStructFieldData(other, variancesField));

    final int nClasses = counts.size();
    final int nFeatures = meanListOI.getListLength(means.get(0));
    if (myAgg.counts == null) {
        myAgg.init(nClasses, nFeatures);
    }

    for (int i = 0; i < nClasses; i++) {
        final long n = myAgg.counts[i];
        final long cnt = PrimitiveObjectInspectorUtils.getLong(counts.get(i), countOI);

        // no need to merge class `i`
        if (cnt == 0) {
            continue;
        }

        final List<?> mean = meanListOI.getList(means.get(i));
        final List<?> variance = varianceListOI.getList(variances.get(i));

        myAgg.counts[i] += cnt;
        for (int j = 0; j < nFeatures; j++) {
            final double meanN = myAgg.means[i][j];
            final double meanM =
                    PrimitiveObjectInspectorUtils.getDouble(mean.get(j), meanElemOI);
            final double varianceN = myAgg.variances[i][j];
            final double varianceM = PrimitiveObjectInspectorUtils.getDouble(
                variance.get(j), varianceElemOI);

            if (n == 0) {// only assign `other` into `myAgg`
                myAgg.means[i][j] = meanM;
                myAgg.variances[i][j] = varianceM;
            } else {
                // merge by Chan's method
                // http://i.stanford.edu/pub/cstr/reports/cs/tr/79/773/CS-TR-79-773.pdf
                myAgg.means[i][j] = (n * meanN + cnt * meanM) / (double) (n + cnt);
                myAgg.variances[i][j] = (varianceN * (n - 1) + varianceM * (cnt - 1)
                        + Math.pow(meanN - meanM, 2) * n * cnt / (n + cnt)) / (n + cnt - 1);
            }
        }
    }
}
 
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;
    }
}