Example usage for org.apache.spark.api.java.function Function call

List of usage examples for org.apache.spark.api.java.function Function call

Introduction

In this page you can find the example usage for org.apache.spark.api.java.function Function call.

Prototype

R call(T1 v1) throws Exception;

Source Link

Usage

From source file:org.apache.beam.runners.spark.translation.TranslationUtils.java

License:Apache License

/**
 * A utility method that adapts {@link Function} to a {@link FlatMapFunction} with an {@link
 * Iterator} input. This is particularly useful because it allows to use functions written for map
 * functions in flatmap functions./*  w w w .j a va2 s  . co m*/
 *
 * @param func the {@link Function} to adapt.
 * @param <InputT> the input type.
 * @param <OutputT> the output type.
 * @return a {@link FlatMapFunction} that accepts an {@link Iterator} as an input and applies the
 *     {@link Function} on every element.
 */
public static <InputT, OutputT> FlatMapFunction<Iterator<InputT>, OutputT> functionToFlatMapFunction(
        final Function<InputT, OutputT> func) {
    return itr -> Iterators.transform(itr, t -> {
        try {
            return func.call(t);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    });
}

From source file:org.apache.crunch.fn.SFunctions.java

License:Apache License

public static <T, R> SFunction<T, R> wrap(final Function<T, R> f) {
    return new SFunction<T, R>() {
        @Override//ww  w . j  av a2 s . co  m
        public R call(T t) throws Exception {
            return f.call(t);
        }
    };
}

From source file:org.apache.kylin.engine.spark.util.IteratorUtils.java

License:Apache License

public static <K, V> Iterator<Tuple2<K, V>> merge(final Iterator<Tuple2<K, V>> input,
        final Comparator<K> comparator, final Function<Iterable<V>, V> converter) {
    return new Iterator<Tuple2<K, V>>() {

        Tuple2<K, V> current = input.hasNext() ? input.next() : null;

        @Override//from  www .  j a  va 2 s  .co  m
        public boolean hasNext() {
            return current != null;
        }

        @Override
        public Tuple2<K, V> next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            final LinkedList<V> values = new LinkedList();
            K currentKey = current._1();
            values.add(current._2());
            while (input.hasNext()) {
                Tuple2<K, V> next = input.next();
                if (comparator.compare(currentKey, next._1()) == 0) {
                    values.add(next._2());
                } else {
                    current = next;
                    try {
                        return new Tuple2<>(currentKey, converter.call(values));
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            }
            if (!input.hasNext()) {
                current = null;
            }
            try {
                return new Tuple2<>(currentKey, converter.call(values));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
}

From source file:org.icgc.dcc.release.job.join.utils.JsonNodes.java

License:Open Source License

/**
 * Populates array {@code array} with {@code values} and applies {@code function} to each of the {@code value}.
 *//*from  w w w  . j av a  2 s . c  om*/
@NonNull
@SneakyThrows
public static void populateArrayNode(ArrayNode array, Iterable<ObjectNode> values,
        Function<ObjectNode, ObjectNode> function) {
    for (val value : values) {
        array.add(function.call(value));
    }
}