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

wfypjpf4  于 2023-04-05  发布在  Docker
关注(0)|答案(1)|浏览(96)

我是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

然后我创建了名为“user_keyspace”的keyspace,并在其中创建了表“users”。现在我正在尝试使用scala中的phantom库插入数据。用户模型:

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"
  }
}

用户数据库类:

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

build.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

请帮我删除这个错误。

4si2a6ki

4si2a6ki1#

如果将类定义为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"
}

这就是错误。
顺便说一下,最好使用与scalaVersion相同的scala-reflectscala-compiler版本。

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

相关问题