【问题标题】:HazelcastSerializationException with Kryo using ScalaHazelcastSerializationException 与 Kryo 使用 Scala
【发布时间】:2015-11-26 18:34:21
【问题描述】:

我正在使用 kryo 与 Scala 2.11.1 和 Hzaelcast 3.5 进行序列化 我正在尝试将我的数据放在 hazelcast 地图中,但我得到了 KryoException

这是我的模型类

  @SerialVersionUID(1)
        case class User( id : Int ,name : String, userType : UserType /*UserType is Enum (EMPLOYED , UNEMPLOYED)*/ , userhistory : UserHistory) extends  Serializable{

def this()= {
this(0,"",Active, null)
}
}

这里是UserHistory

    @SerialVersionUID(1)
case  class UserHistory( date : DateTime = DateTime.now(), artworkStatus : ArtworkStatus = ACTIVE) extends  Serializable{

def this()={
    this(DateTime.parse("0"),ACTIVE) 
  }
}

这是我的 User 类序列化程序

class UserSerializer extends StreamSerializer[User] {


   val log = LoggerFactory.getLogger(this.getClass)

   override def destroy() {

  }

  override def getTypeId() : Int ={
    val value : Int = 1;
    value

  }

  // takes the bytes and converts into User Object
   @throws(classOf[IOException])
  override def read(in : ObjectDataInput) : User =  {

     val kryo = new Kryo 
     val input = new Input(in.asInstanceOf[InputStream])
      log.info("********** Reading the bytes and converting into User object")
     kryo.readClassAndObject(input).asInstanceOf[User]



  }

   // takes User Object and converts into bytes
  @throws(classOf[IOException])
  override def write(out : ObjectDataOutput, obj : User) {

    val kryo= new Kryo
    val bops = new ByteArrayOutputStream
    val output = new Output(bops)
    kryo.writeClassAndObject(output , obj)
    output.flush
    output.close

    bops.writeTo(out.asInstanceOf[OutputStream])

    log.info("********** User object writen into bytes successfully")


  }


}

现在当我像这样将来自 Hcast 客户端的 User 类对象放入相应的地图中时

map.set(user.id , user)

它给了我这些例外:

 Cannot invoke the action, eventually got an error: com.hazelcast.nio.serialization.HazelcastSerializationException: com.esotericsoftware.kryo.KryoException: Class cannot be created (non-static member class): scala.Enumeration$Val

Hcast Client完成StackTraces

7:52:25.094 152121 [play-akka.actor.default-dispatcher-2] UserSerializer INFO - ********** 写入字节的用户对象 成功 17:52:29.667 156694 [play-akka.actor.default-dispatcher-2] 播放错误 - 无法调用 操作,最终报错: com.hazelcast.nio.serialization.HazelcastSerializationException: com.esotericsoftware.kryo.KryoException:无法创建类 (非静态成员类): scala.Enumeration$Val 序列化跟踪: 用户类型(用户历史) 用户历史(用户)17:52:29.832 156859 [play-akka.actor.default-dispatcher-2] 应用程序错误 -

! @6nb1hbglj - 内部服务器错误,用于(POST) [/用户/注册]->

play.api.Application$$anon$1:执行 异常[[HazelcastSerializationException: com.esotericsoftware.kryo.KryoException:无法创建类 (非静态成员类): scala.Enumeration$Val 序列化跟踪: 用户类型(用户历史) UserHistory(用户)在 play.api.Application$class.handleError(Application.scala:296) ~[play_2.11-2.3.8.jar:2.3.8] 在 play.api.DefaultApplication.handleError(Application.scala:402) [play_2.11-2.3.8.jar:2.3.8] 在 play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$4.apply(PlayDefaultUpstreamHandler.scala:320) [play_2.11-2.3.8.jar:2.3.8] 在 play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$4.apply(PlayDefaultUpstreamHandler.scala:320) [play_2.11-2.3.8.jar:2.3.8]

以下是来自Hcast ServerStackTraces

17:52:28.010 [hz._hzInstance_1_dev.partition-operation.thread-0] INFO models.serializers.UserSerializer - ********** 读取字节 并转换为用户对象 17:52:28.990 [hz._hzInstance_1_dev.partition-operation.thread-0] 错误 c.h.map.impl.operation.SetOperation - [192.168.15.20]:5701 [dev] [3.5] com.esotericsoftware.kryo.KryoException:无法创建类 (非静态成员类): scala.Enumeration$Val 序列化跟踪: 用户类型(用户历史)用户历史(用户) com.hazelcast.nio.serialization.HazelcastSerializationException: com.esotericsoftware.kryo.KryoException:无法创建类 (非静态成员类): scala.Enumeration$Val 序列化跟踪: 用户类型(用户历史)用户历史(用户)在 com.hazelcast.nio.serialization.SerializationServiceImpl.handleException(SerializationServiceImpl.java:380) ~[hazelcast-3.5.jar:3.5] 在 com.hazelcast.nio.serialization.SerializationServiceImpl.toObject(SerializationServiceImpl.java:282) ~[hazelcast-3.5.jar:3.5] 在 com.hazelcast.map.impl.mapstore.AbstractMapDataStore.toObject(AbstractMapDataStore.java:78) ~[hazelcast-3.5.jar:3.5] 在 com.hazelcast.map.impl.mapstore.writethrough.WriteThroughStore.add(WriteThroughStore.java:39) ~[hazelcast-3.5.jar:3.5] 在 com.hazelcast.map.impl.mapstore.writethrough.WriteThroughStore.add(WriteThroughStore.java:31) ~[hazelcast-3.5.jar:3.5] 在 com.hazelcast.map.impl.DefaultRecordStore.set(DefaultRecordStore.java:803) ~[hazelcast-3.5.jar:3.5] 在 com.hazelcast.map.impl.operation.SetOperation.run(SetOperation.java:41) ~[hazelcast-3.5.jar:3.5] 在 com.hazelcast.spi.impl.operationservice.impl.OperationRunnerImpl.run(OperationRunnerImpl.java:137) ~[hazelcast-3.5.jar:3.5] 在 com.hazelcast.spi.impl.operationexecutor.classic.OperationThread.processOperation(OperationThread.java:154) [hazelcast-3.5.jar:3.5] 在 com.hazelcast.spi.impl.operationexecutor.classic.OperationThread.process(OperationThread.java:110) [hazelcast-3.5.jar:3.5] 在 com.hazelcast.spi.impl.operationexecutor.classic.OperationThread.doRun(OperationThread.java:101) [hazelcast-3.5.jar:3.5] 在 com.hazelcast.spi.impl.operationexecutor.classic.OperationThread.run(OperationThread.java:76) [hazelcast-3.5.jar:3.5] 原因: com.esotericsoftware.kryo.KryoException:无法创建类 (非静态成员类):scala.Enumeration$Val

请帮帮我!!

【问题讨论】:

    标签: scala serialization hazelcast kryo


    【解决方案1】:

    通常使用 Scala,您应该使用 StdInstantiatorStrategy,例如

    val kryo = new Kryo
    val is = new org.objenesis.strategy.StdInstantiatorStrategy
    kryo.setInstantiatorStrategy(instantiatorStrategy)
    

    不确定这是否能解决您的问题,但我没有看到任何此类问题。

    【讨论】:

    • 也适用于 Java
    【解决方案2】:

    您是否按照github 中的说明注册了枚举序列化程序?

            // Serialization of Scala enumerations
            kryo.addDefaultSerializer(classOf[scala.Enumeration#Value], classOf[EnumerationSerializer])
            kryo.register(Class.forName("scala.Enumeration$Val"))
            kryo.register(classOf[scala.Enumeration#Value])
    

    异常很清楚,它不能创建枚举的userType

    com.esotericsoftware.kryo.KryoException: Class cannot be created (non-static member class): scala.Enumeration$Val Serialization trace: userType (UserHistory) UserHistory (User)
    

    【讨论】:

    • 我应该为我的枚举单独编写序列化程序(您的解决方案中的 EnumerationSerializer)吗?
    猜你喜欢
    • 2019-02-19
    • 1970-01-01
    • 1970-01-01
    • 2020-09-30
    • 2014-10-12
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 1970-01-01
    相关资源
    最近更新 更多