org.trustedanalytics.atk.giraph.aggregators.VectorSumAggregator.java Source code

Java tutorial

Introduction

Here is the source code for org.trustedanalytics.atk.giraph.aggregators.VectorSumAggregator.java

Source

/**
 *  Copyright(c)2015 IntelCorporation
 *
 *  LicensedundertheApacheLicense,Version2.0(the"License");
 *  youmaynotusethisfileexceptincompliancewiththeLicense.
 *  YoumayobtainacopyoftheLicenseat
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unlessrequiredbyapplicablelaworagreedtoinwriting,software
 *  distributedundertheLicenseisdistributedonan"ASIS"BASIS,
 *  WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.
 *  SeetheLicenseforthespecificlanguagegoverningpermissionsand
 *  limitationsundertheLicense.
 */

package org.trustedanalytics.atk.giraph.aggregators;

import org.apache.giraph.aggregators.BasicAggregator;
import org.apache.mahout.math.DenseVector;
import org.apache.mahout.math.Vector;
import org.apache.mahout.math.VectorWritable;

/**
 * Aggregator for summing up vector values.
 */
public class VectorSumAggregator extends BasicAggregator<VectorWritable> {
    @Override
    public void aggregate(VectorWritable value) {
        Vector currentValue = getAggregatedValue().get();
        if (currentValue.size() == 0) {
            getAggregatedValue().set(value.get());
        } else if (value.get().size() > 0) {
            getAggregatedValue().set(currentValue.plus(value.get()));
        }
    }

    @Override
    public VectorWritable createInitialValue() {
        return new VectorWritable(new DenseVector());
    }
}