I'm using phantom to connect cassandra in play framework. Created the first class following the tutorial. Everything works fine.
case class User(id: String, page: Map[String,String])
sealed class Users extends CassandraTable[Users, User] {
object id extends StringColumn(this) with PartitionKey[String]
object page extends MapColumn[String,String](this)
def fromRow(row: Row): User = {
User(
id(row),
page(row)
)
}
}
abstract class ConcreteUsers extends Users with RootConnector {
def getById(page: String): Future[Option[User]] = {
select.where(_.id eqs id).one()
}
def create(id:String, kv:(String,String)): Future[ResultSet] = {
insert.value(_.id, id).value(_.page, Map(kv)).consistencyLevel_=(ConsistencyLevel.QUORUM).future()
}
}
class UserDB(val keyspace: KeySpaceDef) extends Database(keyspace) {
object users extends ConcreteUsers with keyspace.Connector
}
object UserDB extends ResourceAuthDB(conn) {
def createTable() {
Await.ready(users.create.ifNotExists().future(), 3.seconds)
}
}
However, when I try to create another table following the exact same way, play throws the exception when compile:
overriding method session in trait RootConnector of type => com.datastax.driver.core.Session;
How could I build create another table? Also can someone explain what causes the exception? Thanks.
EDIT
I moved the connection part together in one class:
class UserDB(val keyspace: KeySpaceDef) extends Database(keyspace) {
object users extends ConcreteUsers with keyspace.Connector
object auth extends ConcreteAuthInfo with keyspace.Connector
}
This time the error message is:
overriding object session in class AuthInfo; lazy value session in trait Connector of
type com.datastax.driver.core.Session cannot override final member
Hope the message helps identify the problem.
The only problem I see here is not to do with connectors, it's here:
def getById(page: String): Future[Option[User]] = {
select.where(_.id eqs id).one()
}
This should be:
def getById(page: String): Future[Option[User]] = {
select.where(_.id eqs page).one()
}
Try this, I was able to compile. Is RootConnector the default one or do you define another yourself?
It took me 6 hours to figure out the problem. It is because there is a column named "session" in the other table. It turns out that you need to be careful when selecting column names. "session" obviously gives the above exception. Cassandra also has a long list of reserved keywords. If you accidentally use one of them as your column name, phantom will not throw any exceptions (maybe it should?). I don't know if any other keywords are reserved in phantom. A list of them will be really helpful.
Related
I'm using this library for the first time, and I ran into a problem. I did everything according to the documentation, but nothing is working, and i don't know why. Here is my table model:
trait CassandraModel
object CassandraModel {
case class TaskData(notifyid: String,
notifyType: String)
extends CassandraModel
abstract class TaskDataCassandra extends Table[TaskDataCassandra, TaskData] {
object notifyid extends StringColumn with PartitionKey
object notifyType extends StringColumn
def store(record: TaskData): InsertQuery.Default[TaskDataCassandra, TaskData] =
insert
.value(_.notifyId, record.notifyId)
.value(_.notifyType, record.notifyType)
}
}
And DataBase with DatabaseProvider:
class AppDatabase(override val connector: CassandraConnection) extends Database[AppDatabase](connector) {
object taskDataCassandra extends TaskDataCassandra with Connector
}
trait AppDatabaseProvider extends DatabaseProvider[AppDatabase]
So, when i starting my app, i'm trying to create a keyspace, but nothing is happens
object Boot extends App with AmqpConnector with ServiceRestRoute with JsonSerializer with AppDatabaseProvider {
override def database: AppDatabase = new AppDatabase(CassandraConnector.createCassandraConnection)
database.taskDataCassandra.create.ifNotExists()
}
store method also doesn't works
Read through the documentation properly and the differences will be obvious. The thing to read is the Database Docs.
You have 2 options. You can call database.create(), which is a blocking creation operation that will create all the tables inside the database.
Option 2 is to call database.taskDataCassandra.create.ifNotExists().future().
If you do not use future(), all you have is a query generated, you are not actually executing anything. If you check the return type of database.taskDataCassandra.create.ifNotExists() it will be a CreateQuery, wheres if you add future() you get a Future[Result].
Hope this makes sense.
I have the following class :
case class AucLog(timestamp: UUID, modelname: String, good: Int,
list: List[Double])
class AucDatabase(override val connector : CassandraConnection)
extends Database[AucDatabase](connector) {
object users extends CMetrics with Connector
}
object AucDatabase extends AucDatabase(AucConnector.connector)
abstract class AucMetrics extends Table[AucMetrics, AucLog] {
object id extends UUIDColumn with PartitionKey
object name extends StringColumn
object ud extends IntColumn
object zob extends ListColumn[Double]
}
abstract class CMetrics extends AucMetrics with RootConnector {
def store(metric : AucLog): Future[ResultSet] = {
insert.value(_.id, metric.timestamp)
.value(_.name, metric.modelname)
.value(_.ud, metric.good)
.value(_.zob, metric.list)
.consistencyLevel_=(ConsistencyLevel.ONE)
.future()
}
DmpDatabase.create()
AucDatabase.create()
val pd = DmpDatabase.users.myselect()
val timeout = new Timeout(500000)
val result = Await.result(pd, timeout.duration)
"<--- this attempt to read from my database is working - no problemo ---> "
val todf = result.records.map { elem => elem.idcat }
val rdd = spark.sparkContext.parallelize(todf)
import spark.implicits._
rdd.toDF().show(100)
---> I'm storing one line in my database to be sure that it is not empty when
i'm reading it.
AucDatabase.users.store(new AucLog(UUIDs.timeBased(), "tyron", 0, List(0.1)))
val second = AucDatabase.users.myselect()
val resultmetric = Await.result(second, timeout.duration)
-----> this line cause the Execption
val r = spark.sparkContext.parallelize(resultmetric.records).toDF().show(
What I do not understand is that i'm doing basically the same thing with both databases. Yet, one is throwing the following error : UnsupportedOperationException : No encoder found for com.outworkers.phantom.dsl.UUID.
Thank you.
First of all the store method is macro generated so you don't need to create one. The problem you are having is likely not related to phantom at all, but to some kind of Spark construct.
The phantom UUID is nothing more than a type alias for java.util.UUID, so I'm quite surprised there is no straight up encoder for a default type. If you help me out with the full name of the Encoder class, including the package, I can figure out explicitly what is broken.
I'm trying to follow the example from this blog. I understand the example but having trouble implementing it.
trait Database {
// ...
}
trait UserDb {
this: Database =>
// ...
}
trait EmailService {
this: UserDb =>
// Can only access UserDb methods, cannot touch Database methods
}
The example mentions that the full Database functionality will be hidden from the EmailService - this is what i'm after but don't know how to implement these traits correctly.This is what i tried to implement:
trait Database {
def find(query: String): String
}
trait UserDb {
this: Database =>
}
trait EmailService {
this: UserDb =>
}
trait MongoDatabase extends Database {
}
trait MongoUserDb extends UserDb with MongoDatabase{
}
class EmailServiceImpl extends EmailService with MongoUserDb {
override def find(query: String): String = {
"result"
}
}
It looks weird to me becasue MongoDatabase trait didn't asked for find implementation and when i implemented EmailService i was then prompted for find implementation,although the example mentioned this will be hidden from the EmailService. What am i missing here?
After reading your comments, I'm trying to implement what i'm being trying to understand on an example that is closer to what i'm actually trying to do.
The first snippet won't compile, but the second one will...
At the end of the day i want to have different Repository implementations where i can switch between the Databases they rely on, am i close with one of the snippets below?
trait Database {
def find(s: String): String
}
trait Repository {
this: Database =>
}
class UserRepository extends Repository {
def database = new MongoDB
class MongoDB extends Database {
def find(s: String): String = {
"res"
}
}
}
trait Repository {
def database: Database
trait Database {
def find(s: String): String
}
}
trait UserRepository extends Repository {
def database = new MongoDB
class MongoDB extends Database {
def find(s: String): String = {
"res"
}
}
}
As mentioned MongoUserDB will not ask for an implementation as its a trait. However since EmailServiceImpl extends the trait it needs to provide an implementation.
What you are looking for could be done by adding another abstraction. I do it using the service and DAO architecture.
Below is a working example that you may use to see if it suits you.
//All future versions of DAO will extend this
trait AbstractDAO{
def getRecords:String
def updateRecords(records:String):Unit
}
//One concrete version
trait concreteDAO extends AbstractDAO{
override def getRecords={"Here are DB records"}
override def updateRecords(records:String){
//Actual DB calls and operations
println("Updated "+records)
}
}
//Second concrete version
trait concreteDAO1 extends AbstractDAO{
override def getRecords={"DB Records returned from DAO2"}
override def updateRecords(records:String){
//Actual DB calls and operations
println("Updated via DAO2"+records)
}
}
//This trait just defines dependencies (in this case an instance of AbstractDAO) and defines operations based over that
trait service{
this:AbstractDAO =>
def updateRecordsViaDAO(record:String)={
updateRecords(record)
}
def getRecordsViaDAO={
getRecords
}
}
//Test Stub
object DI extends App{
val wiredObject = new service with concreteDAO //injecting concrete DAO to the service and calling methods
wiredObject.updateRecords("RECORD1")
println(wiredObject.getRecords)
val wiredObject1 = new service with concreteDAO1
wiredObject1.updateRecords("RECORD2")
println(wiredObject1.getRecords)
}
EDIT ---
Here is the code you might want to implement,
trait Database {
def find(s: String): String
}
trait MongoDB extends Database{
def find(s:String):String = { "Test String" }
}
trait SQLServerDB extends Database{
def find(s:String):String = { "Test String2" }
}
trait Repository {
this: Database =>
}
class UserRepository extends Repository with MongoDB{ // UserRepository is injected with MongoDB here
find("call MongoDB") //This call will go to the find method in MongoDB trait
}
class UserRepository1 extends Repository with SQLServerDB{ // UserRepository is injected with SQLServerDB here
find("call SQLServerDB") //This call will go to the find method in SQLServerDB trait
}
Database is hidden from EnailService, but not from EmailServiceImpl. The latter is a subclass of MongoUserDB, obviously, it has access to it.
MongoUserDB does not "ask" for find implementation, because it is a trait, and traits can have abstract methods. You should still implement it there, even without being asked ;)
I have been toying around with the smiple code provided in the phantom wiki, the follow I have tried;
import com.websudos.phantom.dsl._
case class Student(id: UUID, name: String)
class Students extends CassandraTable[Students, Student] {
object id extends UUIDColumn(this) with PartitionKey[UUID]
object name extends StringColumn(this)
def fromRow(row: Row): Student = {
Student(id(row), name(row))
}
}
object Students extends Students with Connector {
def getByName(name: String): Future[Option[Student]] = {
select.where(_.name eqs name).one()
}
}
But my IDE keeps saying Cannot resolve symbol where and the compiler says value where is not a member of com.websudos.phantom.builder.query.RootSelectBlock[Students,Student]
I'm using Scala 2.11.6 and Phantom 1.10.1, all help is greatly appreciated!
I ran into this issue and resolved this using #flavian's suggestion above.
Ensure that your Connector has an implicit keyspace defined.
This is directly lifted from the example project.
trait KeyspaceDefinition {
implicit val keySpace = KeySpace("sample_keyspace")
}
trait Connector extends SimpleConnector with KeyspaceDefinition
You are missing out on a fundamental Cassandra issue, you cannot query by name as it's not an indexed column. Based on the table you've just defined, the query you are trying to perform is invalid and Cassandra will tell you that at runtime.
Phantom will prevent most bad things at compile time. It's worth reading through this blog series to understand how things work in Cassandra.
To put things in perspective, the only where query that's valid for your Students table is:
def getById(id: UUID): Future[Option[Student]] = {
select.where(_.id eqs id).one()
}
Does anyone know of a convenient way of inserting values to Cassandra via phatom-dsl? Currently I'm doing this:
case class Entry(id: UUID, firstName: String, lastName: String)
sealed class EntryTable extends CassandraTable[EntryTable, Entry] {
override val tableName = "entries"
object id extends UUIDColumn(this) with Index[UUID]
object firstName extends StringColumn(this)
object lastName extends StringColumn(this)
override def fromRow(r: dsl.Row): Entry = {
Entry(id(r), firstName(r), firstName(r))
}
}
object EntryTable extends EntryTable {
private val connector = CassandraConnector.apply(Set(InetAddress.getByName("localhost")))
implicit val keySpace = KeySpace("keyspace")
def insert(e: Entry) = {
connector.withSessionDo(implicit session => {
insert().value(_.id, e.id)).value(_.firstName, e.firstName).value(_.lastName, e.lastName).future()
}
}
}
But I would like to do:
def insert(e: Entry) = {
connector.withSessionDo(implicit session => {
insert().value(e).future()
}
}
Which would be way more convenient when the case class has many fields. Any ideas?
You are using the API slightly wrong and we are in the process of publishing multiple tutorials to make the "new" way public. In the mean time, a basic version of it is available here and this branch in the activator template is also describing everything you need to know.
Specifically, the way to insert records is described here.