PlayFramework からHectorライブラリ経由でCassandraへアクセスする。
設定自体は簡単なものだった。Build.scala に下記を記載する。
object ApplicationBuild extends Build {
val appName = "cassandra-sample"
val appVersion = "1.0-SNAPSHOT"
val appDependencies = Seq(
// Add your project dependencies here,
"me.prettyprint" % "hector-core" % "0.8.0-2"
)
val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA).settings(
// Add your own project settings here
)
}これで後は使うだけだ。
package sample
import org.specs2.mutable.Specification
import me.prettyprint.hector.api.factory.HFactory
import me.prettyprint.cassandra.service.template.ThriftColumnFamilyTemplate
import me.prettyprint.cassandra.serializers.StringSerializer
class SampleSpec extends Specification {
"read" should {
"return cassandra setted value" in {
val cluster = HFactory.getOrCreateCluster("Test Cluster", "localhost:9160")
val keySpace = HFactory.createKeyspace("sample", cluster)
val template = new ThriftColumnFamilyTemplate[String, String](keySpace, "user", StringSerializer.get(), StringSerializer.get())
val res = template.queryColumns("azalea")
res.getString("mail") mustEqual("xxxxxx@xxxxx-azalea.net")
}
}
}