Как использовать Java-лямбду для возврата массива байтов при использовании библиотеки Algebird? - PullRequest
0 голосов
/ 04 июня 2019

Я реализую тестовый класс на Java с библиотекой Algebird для вычисления HyperLogLog. Эта библиотека в Scala, но я хочу использовать ее на Java. В какой-то момент я должен перевести список int в список массивов байтов, затем я должен использовать лямбда-подход Java. Там я получаю сообщение об ошибке Missing return statement. Что я здесь не так делаю? Это код Java:

import com.twitter.algebird.Approximate;
import com.twitter.algebird.HLL;
import com.twitter.algebird.HyperLogLogMonoid;
import scala.collection.TraversableOnce;

import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
// import com.twitter.algebird.HyperLogLog.int2Bytes;

public class AlgebirdHLLAppJ {
    public static void main(String[] args) {
        System.out.println("This is the Spark test of the Algebird HyperLogLog application");

        HyperLogLogMonoid hll = new HyperLogLogMonoid(4);
        List<Integer> data = new ArrayList<Integer>(Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4, 5, 5));

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        baos.toByteArray();
        hll.create(baos.toByteArray());
        TraversableOnce<HLL> seqHll = data.stream().map(d -> {
            ByteBuffer bb = ByteBuffer.allocate(4);
            bb.putInt(d);
            hll.create(bb.array());
        }); // ERROR: Missing return statement
        HLL sumHll = hll.sum(seqHll);
        Approximate<Object> approxSizeOf = hll.sizeOf(sumHll);
        Integer actualSize = data.size();
        Integer estimate = (Integer) approxSizeOf.estimate();
        System.out.println("Actual size: " + actualSize);
        System.out.println("Estimate size: " + estimate);
    }
}

это код скалы

import com.twitter.algebird.HyperLogLogMonoid
import com.twitter.algebird.HyperLogLog.int2Bytes

object AlgebirdHLLApp {
  def main(args: Array[String]): Unit = {
    println("This is the Spark test of the Algebird HyperLogLog application")

    val hll = new HyperLogLogMonoid(4)
    val data = List(1, 1, 2, 2, 3, 3, 4, 4, 5, 5)
    val seqHll = data.map { hll.create(_) }
    val sumHll = hll.sum(seqHll)
    val approxSizeOf = hll.sizeOf(sumHll)
    val actualSize = data.toSet.size
    val estimate = approxSizeOf.estimate

    println("Actual size: " + actualSize)
    println("Estimate size: " + estimate)
  }
}

1 Ответ

0 голосов
/ 07 июня 2019

Попробуйте

import com.twitter.algebird.Approximate;
import com.twitter.algebird.HLL;
import com.twitter.algebird.HyperLogLog;
import com.twitter.algebird.HyperLogLogMonoid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
import scala.collection.JavaConverters;

public class AlgebirdHLLAppJ {
    public static void main(String[] args) {
        System.out.println("This is the Spark test of the Algebird HyperLogLog application");

        HyperLogLogMonoid hll = new HyperLogLogMonoid(4);
        List<Integer> data = new ArrayList<>(Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4, 5, 5));
        List<HLL> seqHll = data.stream().map(i -> hll.create(HyperLogLog.int2Bytes(i))).collect(Collectors.toList());
        HLL sumHll = (HLL) hll.sum(JavaConverters.collectionAsScalaIterable(seqHll));

        Approximate<Object> approxSizeOf = hll.sizeOf(sumHll);
        int actualSize = new HashSet<>(data).size();
        long estimate = (long) approxSizeOf.estimate();

        System.out.println("Actual size: " + actualSize);
        System.out.println("Estimate size: " + estimate);
    }
}
...