All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.deeplearning4j.spark.impl.common.CountPartitionsFunction Maven / Gradle / Ivy

There is a newer version: 1.0.0-beta_spark_2
Show newest version
package org.deeplearning4j.spark.impl.common;

import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.function.Function2;
import org.deeplearning4j.spark.api.Repartition;
import scala.Tuple2;

import java.util.Collections;
import java.util.Iterator;

/**
 * This is a function use to count the number of elements in each partition.
 * It is used as part of {@link org.deeplearning4j.spark.util.SparkUtils#repartitionBalanceIfRequired(JavaRDD, Repartition, int, int)}
 *
 * @author Alex Black
 */
public class CountPartitionsFunction implements Function2, Iterator>> {
    @Override
    public Iterator> call(Integer v1, Iterator v2) throws Exception {

        int count = 0;
        while(v2.hasNext()){
            v2.next();
            count++;
        }

        return Collections.singletonList(new Tuple2<>(v1, count)).iterator();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy