在scala中使用幻影库插入scylladb表不工作



我是ScyllaDB的新手,并试图在scala中使用幻影库执行CRUD操作。首先,我做了一个3节点集群,它运行得很好。

Datacenter: DC1
===============
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
--  Address     Load       Tokens       Owns    Host ID                               Rack
UN  172.19.0.3  1.1 MB     256          ?       1db1e9a6-8e99-4d7c-b086-f169b4619e16  Rack1
UN  172.19.0.2  1.14 MB    256          ?       f6e6ef6b-7ec5-4a59-aaa0-975eac039794  Rack1
UN  172.19.0.4  1.14 MB    256          ?       9aac568d-bafb-4d9b-b2aa-8a0bfd64ef4f  Rack1

然后我将keyspace命名为"user_keyspace",并创建表"users"在里面。现在我试图在scala中使用幻影库插入数据。用户模型:

case class User(userID: Int, firstName: String, lastName: String)

Users类,我用Table[]:

扩展它
import com.outworkers.phantom.dsl._
abstract class Users extends Table[Users, User] {
override def tableName: String = "users"
object UserID extends IntColumn with PartitionKey {
override def name: String = "user_id"
}
object FirstName extends IntColumn {
override def name: String = "first_name"
}
object LastName extends IntColumn {
override def name: String = "last_name"
}
}

UsersDatabase类:

import com.outworkers.phantom.dsl._
class UsersDatabase(override val connector: CassandraConnection) extends Database[UsersDatabase](connector) {
object users extends Users with Connector
}

UsersOperations类:

import com.outworkers.phantom.dsl._
import scala.concurrent.Future
class UserOperations(db: UsersDatabase) {
import db.{session, space}
def insertUser(user: User): Future[ResultSet] =
db.users.store(user).future()
}

主类:

import com.outworkers.phantom.dsl._
import scala.util._
object App extends App {
private val connection: CassandraConnection =
ContactPoints(List("172.19.0.3", "172.19.0.2", "172.19.0.4"))
.keySpace("user_keyspace")
private val db = new UsersDatabase(connection)
private val userOperations = new UserOperations(db)
userOperations.insertUser(User(2,"Ali","Hassan")).onComplete{
case Success(value) =>
println("Users: " + value)
db.shutdown()
case Failure(exception) =>
println("Exception: " + exception)
db.shutdown()
}
}

构建。sbt文件:

ThisBuild / version := "0.1.0-SNAPSHOT"
ThisBuild / scalaVersion := "2.13.1"
lazy val root = (project in file("."))
.settings(
name := "my_scylladb_app"
)
libraryDependencies += "com.outworkers" %% "phantom-dsl" % "2.59.0"
libraryDependencies += "org.scala-lang" % "scala-reflect" % "2.13.10"
libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.4.6"

当我运行这段代码时,它给出错误:

[error] /home/zaryab/IdeaProjects/my_scylladb_app/src/main/scala/UserOperations.scala:14:19: could not find implicit value for parameter sg: com.outworkers.phantom.macros.SingleGeneric.Aux[User,Repr,HL,Out] (The type you're trying to store User should match either Repr or HL)
[error]     db.users.store(user).future()
[error]                   ^
[error] one error found
[error] (Compile / compileIncremental) Compilation failed

请帮我删除这个错误。

如果您将类定义为case class User(userID: Int, firstName: String, lastName: String),则

object FirstName extends IntColumn {
override def name: String = "first_name"
}
object LastName extends IntColumn {
override def name: String = "last_name"
}

必须

object FirstName extends StringColumn {
override def name: String = "first_name"
}
object LastName extends StringColumn {
override def name: String = "last_name"
}

错误。


顺便说一下,最好使用scala-reflect,scala-compilerscalaVersion相同的版本。所以

ThisBuild / scalaVersion := "2.13.1"
libraryDependencies += "org.scala-lang" % "scala-reflect" % "2.13.10"

应该更好

ThisBuild / scalaVersion := "2.13.10"
libraryDependencies += "org.scala-lang" % "scala-reflect" % "2.13.10"

ThisBuild / scalaVersion := "2.13.1"
libraryDependencies += "org.scala-lang" % "scala-reflect" % "2.13.1"

最好写

libraryDependencies += "org.scala-lang" % "scala-reflect" % scalaVersion.value

或者

libraryDependencies += scalaOrganization.value % "scala-reflect" % scalaVersion.value

避免这样的混淆。实际上,您似乎根本没有使用scala-reflect

相关内容

  • 没有找到相关文章

最新更新