简体   繁体   中英

How do I use Java lambda to return an array of bytes when I am using the Algebird library?

I am implementing a test class in Java with the library Algebird to compute HyperLogLog. This library is in scala but I want to use it in Java. In some point I have to translate a list of int into a list of array of bytes, then I have to use the Java lambda approach. There I am receiving the error Missing return statement . What am I doing wrong here? This is the java code:

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);
    }
}

this is the scala code

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)
  }
}

Try

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM