"错误的顶级声明声明"何时导入scala.reflect.ClassTag

时间:2017-10-23 09:54:27

标签: scala apache-spark serialization closures

我正在学习Spark的闭包及其序列化,我写了一个演示,如下所示。但得到了一个提示" Wrong Top声明声明"何时导入scala.reflect.ClassTag。

package lulijun.scala.study.serialize

import java.io._
import java.nio.ByteBuffer

import org.apache.spark.serializer.{DeserializationStream, SerializationStream}
import org.apache.spark.util.{ByteBufferInputStream, ByteBufferOutputStream}

import scala.reflect.ClassTag


/**
  * Created by lulijun on 2017/10/23.
  */
public class JavaSerializerTest {


  def serialize[T: ClassTag](t: T): ByteBuffer = {
    val bos = new ByteBufferOutputStream()
    val out = serializeStream(bos)
    out.writeObject(t)
    out.close()
    bos.toByteBuffer
  }

  def deserialize[T: ClassTag](bytes: ByteBuffer, loader: ClassLoader): T = {
    val bis = new ByteBufferInputStream(bytes)
    val in = deserializeStream(bis, loader)
    in.readObject()
  }

  def serializeStream(s: OutputStream): SerializationStream = {
    new JavaSerializationStream(s, 1, true)
  }


  def deserializeStream(s: InputStream, loader: ClassLoader): DeserializationStream = {
    new JavaDeserializationStream(s, loader)
  }
}

1 个答案:

答案 0 :(得分:2)

这与ClassTags无关。 Scala没有public修饰符。如果要公开课程,则根本不要添加修饰符:

class JavaSerializerTest {
  ...
}

您还有许多其他问题,例如尝试访问私有Spark成员(ByteBufferOutputStream)。