我需要帮助使用幻像和cassandra实现通知模型。我到目前为止做了什么:
import java.util.UUID
import com.websudos.phantom.dsl._
import com.websudos.phantom.connectors.Connector
import org.joda.time.DateTime
import scala.concurrent.Future
case class Notification(
id: UUID,
userId: UUID,
timestamp: DateTime,
read: Boolean,
actionUser: List[String],
verb: String,
itemId: UUID,
collectionId: String
)
sealed class NotificationTable extends CassandraTable[NotificationTable, Notification] {
object id extends UUIDColumn(this) with ClusteringOrder[UUID] with Ascending
object userId extends StringColumn(this) with PartitionKey[String]
object timestamp extends DateTimeColumn(this) with ClusteringOrder[DateTime] with Descending
object read extends BooleanColumn(this)
object actionUser extends ListColumn[NotificationTable, Notification, String](this)
object verb extends StringColumn(this)
object itemId extends UUIDColumn(this)
object collectionId extends StringColumn(this)
def fromRow(row: Row): Notification =
Notification(
id(row),
userId(row),
timestamp(row),
read(row),
actionUser(row),
verb(row),
itemId(row),
collectionId(row)
)
}
object NotificationTable extends NotificationTable with Connector {
override def keySpace: String = "test"
implicit val keyspace: com.websudos.phantom.connectors.KeySpace = com.websudos.phantom.connectors.KeySpace("test")
def insertItem(item: Notification): Future[ResultSet] =
insert
.value(_.id, item.id)
.value(_.userId, item.userId)
.value(_.timestamp, item.timestamp)
.value(_.read, item.read)
.value(_.actionUser, item.actionUser)
.value(_.verb, item.verb)
.value(_.itemId, item.itemId)
.value(_.collectionId, item.collectionId)
.future()
}
不知何故,我必须定义两个keyspaces
,一个用于RootConnector
,一个用于insert
语句。这足够接近:this example ,.但是,我的代码没有编译。我知道他们在那里使用抽象类,因此它编译。</ p>
我的问题是如何使用该抽象类?我想从另一个scala源调用insert语句。
答案 0 :(得分:2)
尝试:
import java.util.UUID
import com.websudos.phantom.dsl._
import com.websudos.phantom.connectors.Connector
import org.joda.time.DateTime
import scala.concurrent.Future
case class Notification(
id: UUID,
userId: UUID,
timestamp: DateTime,
read: Boolean,
actionUser: List[String],
verb: String,
itemId: UUID,
collectionId: String
)
//use normal class
class NotificationTable extends CassandraTable[NotificationTable, Notification] {
object id extends UUIDColumn(this) with ClusteringOrder[UUID] with Ascending
object userId extends StringColumn(this) with PartitionKey[String]
object timestamp extends DateTimeColumn(this) with ClusteringOrder[DateTime] with Descending
object read extends BooleanColumn(this)
object actionUser extends ListColumn[NotificationTable, Notification, String](this)
object verb extends StringColumn(this)
object itemId extends UUIDColumn(this)
object collectionId extends StringColumn(this)
def fromRow(row: Row): Notification =
Notification(
id(row),
userId(row),
timestamp(row),
read(row),
actionUser(row),
verb(row),
itemId(row),
collectionId(row)
)
}
//use abstract
abstract class NotificationTable extends NotificationTable with Connector {
def insertItem(item: Notification): Future[ResultSet] =
insert
.value(_.id, item.id)
.value(_.userId, item.userId)
.value(_.timestamp, item.timestamp)
.value(_.read, item.read)
.value(_.actionUser, item.actionUser)
.value(_.verb, item.verb)
.value(_.itemId, item.itemId)
.value(_.collectionId, item.collectionId)
.future()
}
答案 1 :(得分:2)
您错过了在那里使用RootConnector
而不是随机Connector
特征的事实。该类是抽象的原因是因为它只应在Database
对象内实例化。
有关详情,请查看this tutorial,但简而言之,请注意RootConnector
mixin:
abstract class ConcreteNotificationTable extends
NotificationTable with RootConnector
然后:
class MyDatabase(val connector: KeySpaceDef) extends Database(connector) {
// And here you inject the real session and keyspace in the table
object notifications extends ConcreteNotificationsTable with connector.Connector
}
然后你做这样的事情:
object MyDatabase extends MyDatabase(ContactPoint.local.keySpace("my_app"))
来自其他所有源文件:
val notification = Notification(id, //etc...)
MyDatabase.notifications.insertItem(someNotification)
甚至可以更好地分离关注点,如教程中所示:
trait DbProvider extends DatabaseProvider {
def database: MyDatabase
}
trait ProductionDbProvider extends DbProvider {
// this would now point to your object
override val database = MyDatabase
}
然后,每个需要数据库的地方都需要混合DbProvider
或直接ProductionDbProvider
。阅读教程以获取更多详细信息,这不是一个非常简单的主题,所有细节都已存在。