Вопрос

val mutator=HFactory.createMutator(keyspace,StringSerializer.get())
mutator.addInsertion("rahul", "user", HFactory.createColumn("birth_year", 1990,   
StringSerializer.get(), LongSerializer.get()))//error in LongSerializer.get() as  
mutator.execute()

I am using LongSerializer like above and i am getting the following error. Description Resource Path Location Type type mismatch; found : me.prettyprint.cassandra.serializers.LongSerializer
required: me.prettyprint.hector.api.Serializer[Any] Note: Long <: Any (and me.prettyprint.cassandra.serializers.LongSerializer <: me.prettyprint.cassandra.serializers.AbstractSerializer[Long]), but Java-defined trait Serializer is invariant in type T. You may wish to investigate a wildcard type such as _ <: Any. (SLS 3.2.10) User.scala /winoria/app/models line 22 Scala Problem

Tell me the solution .

Это было полезно?

Решение

There are a couple of things happening here.

First, Java does not allow primitive types as generics, so Hector's LongSerializer is an AbstractSerializer[java.lang.Long]. However you are working in Scala, so you need an AbstractSerializer[scala.Long]. Depending on circumstances Scala's Long can either be a primitive long or java.lang.Long. The good news is Scala is smart enough to figure out what to use and when. What you need here is a little type coercion: LongSerializer.get().asInstanceOf[Serializer[Long]]

The other suspect is that you need me.prettyprint.hector.api.Serializer[Any]. It looks like whatever you are calling your method on is lacking proper type declarations. A sample of surrounding code would help diagnose this further.

Edit: Thanks for posting the surrounding code. It looks like you have a type disagreement. createColumn[N, V] is inferred as createColumn[String, Int], because the 1990 argument you have provided is an Int. This gets converted to java.lang.Int, which is a class and does not have type conversions like primitives do. This is why you get the error "int cannot be cast to long".

val mutator = HFactory.createMutator(keyspace, StringSerializer.get)
mutator.addInsertion(
  "rahul", "user",
  HFactory.createColumn( // Either do: HFactory.createColumn[String, Long]
    "birth_year", 1990L, // Or make sure the type is inferred as Long
    StringSerializer.get,
    // Coerce serializer to scala.Long
    LongSerializer.get.asInstanceOf[Serializer[Long]]))
mutator.execute()
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top