Instantly share code, notes, and snippets.

Embed
What would you like to do?
Connection and Entity Manager Wrapper for Hector Client in Scala
package ag.bett.model
import org.joda.time._
import scala.collection.JavaConversions._
import scala.reflect.BeanProperty
import java.util.{UUID, ArrayList}
import javax.persistence.{Table, Entity}
import me.prettyprint.cassandra.serializers.LongSerializer
import me.prettyprint.cassandra.serializers.StringSerializer
import me.prettyprint.cassandra.service.template.ColumnFamilyResult
import me.prettyprint.cassandra.service.template.ColumnFamilyUpdater
import me.prettyprint.cassandra.service.template.ThriftColumnFamilyTemplate
import me.prettyprint.cassandra.service.ThriftKsDef
import me.prettyprint.hector.api.ddl.ColumnFamilyDefinition
import me.prettyprint.hector.api.ddl.ComparatorType
import me.prettyprint.hector.api.ddl.KeyspaceDefinition
import me.prettyprint.hector.api.factory.HFactory
import me.prettyprint.hector.api.Cluster
import me.prettyprint.hector.api.Keyspace
import me.prettyprint.hom._
import me.prettyprint.hom.annotations._
import me.prettyprint.hom.converters._
object Cassandra extends Logger {
lazy val keyspaceName = "myKeySpace"
lazy val clusterName = "myCluster"
lazy val cluster: Cluster = HFactory.getOrCreateCluster(clusterName, "localhost:9160")
def keyspace(keyspaceName: String): Keyspace = {
HFactory.createKeyspace(keyspaceName, cluster)
}
def virtualKeyspace(vkeyspaceName: String): Keyspace = {
HFactory.createVirtualKeyspace(keyspaceName, vkeyspaceName, new StringSerializer(), cluster)
}
def shutdown() {
cluster.getConnectionManager.shutdown
}
def createDefaultSchema() {
if (cluster.describeKeyspace(keyspaceName) != null) {
logger.info("Keyspace already created")
return
}
cluster.addKeyspace(Schema.create(
Schema.default("modelA", ComparatorType.BYTESTYPE) ::
Schema.default("modelB", ComparatorType.BYTESTYPE) ::
Nil
), true)
}
private object Schema {
def default(ds: String, comp: ComparatorType = ComparatorType.BYTESTYPE) =
HFactory.createColumnFamilyDefinition(keyspaceName, ds, comp)
def create(cfDef: List[ColumnFamilyDefinition], rf: Int = 1) =
HFactory.createKeyspaceDefinition(keyspaceName, ThriftKsDef.DEF_STRATEGY_CLASS, rf, cfDef)
}
def main(args: Array[String]) {
implicit val ks = keyspace(keyspaceName)
createDefaultSchema
val pfa = new ModelA
pfa.name = "my A record"
pfa.save
val pfb = new PlatformAuth
pfb.modelA = pfa.id
pfb.save
pfb.modelA match {
case Some(pfa) => println("found " + pfa.name)
case _ => println("nothing found")
}
println(ModelB.find(pfb.id))
shutdown
}
}
trait CassandraModel[K <: Object] {
def save()(implicit em: EntityManagerImpl) { em.persist(this) }
}
trait CassandraMeta[K <: Object, V <: CassandraModel[K]] {
def freshInstance: V
def find[V](k: K)(implicit em: EntityManagerImpl) = Option(em.find(freshInstance.getClass, k))
}
package ag.bett.model
import org.joda.time._
import scala.collection.JavaConversions._
import scala.reflect.BeanProperty
import java.util.{UUID, ArrayList}
import javax.persistence.{Table, Entity}
import me.prettyprint.hector.api.Keyspace
import me.prettyprint.hom._
import me.prettyprint.hom.annotations._
@Entity
@Table(name="modelA")
class ModelA extends CassandraModel[UUID] {
@Id
@BeanProperty
var id: UUID = UUID.randomUUID()
@Column(name="name")
@BeanProperty
var name: String = _
@Column(name="active")
@BeanProperty
var active: Boolean = true
}
object ModelA extends ModelA with CassandraMeta[UUID, ModelA] {
def freshInstance = new ModelA
}
@Entity
@Table(name="modelB")
class ModelB extends CassandraModel[UUID] {
@Id
@BeanProperty
var authKey: UUID = UUID.randomUUID()
@Column(name="modelA")
@BeanProperty
var modelA: UUID = UUID.randomUUID()
def getA(implicit ks: Keyspace) = ModelA.find(modelA)
}
object ModelB extends ModelB with CassandraMeta[UUID, ModelB] {
def freshInstance = new ModelB
}
package ag.bett
import me.prettyprint.hom.EntityManagerImpl
import me.prettyprint.hector.api.Keyspace
package object model {
implicit def entityManager(implicit ks: Keyspace) = new EntityManagerImpl(ks, "ag.bett.model") // Classloader Path
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment