首页 > 解决方案 > 使用 Algebird 库时,如何使用 Java lambda 返回字节数组?

问题描述

我正在使用库 Algebird 在 Java 中实现一个测试类来计算 HyperLogLog。这个库在 scala 中,但我想在 Java 中使用它。在某些时候,我必须将一个 int 列表转换为一个字节数组列表,然后我必须使用 Java lambda 方法。我收到错误消息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)
  }
}

标签: javascalalambdascala-java-interopalgebird

解决方案


尝试

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

推荐阅读