Illustration Image

Cassandra.Link

The best knowledge base on Apache Cassandra®

Helping platform leaders, architects, engineers, and operators build scalable real time data platforms.

8/7/2018

Reading time:1 min

tuplejump/play-cassandra

by John Doe

A Play Plugin for using CassandraThe Plugin initializes a Cassandra Session on startup and provides access to sessionand other properties through the Cassandra object.The Plugin also provides play-evolutions like functionality if it is not disabled.####UsageIn library dependencies, include"com.tuplejump" %% "play-cassandra" % "1.0.0-SNAPSHOT"Now, Cassandra host, port, cluster and session can be accessed through the API exposed by the Plugin.In addition to that, a method loadCQLFile is also available. The API is documented at TODOSome examples,executing a queryval query = QueryBuilder.select("title") .from("music", "playlists") .where(QueryBuilder.eq("id", playlistId)) val queryResult = Cassandra.session.execute(query).toIterableexecuting a batch statementval insertQuery: String = "INSERT INTO music.playlists (id, song_id, title, artist, album) VALUES (?, ?, ?, ?, ?)" val ps = Cassandra.session.prepare(insertQuery) var batch = new BatchStatement() songs.foreach { s => batch.add(ps.bind(playlistId, s.id, s.title, s.artist, s.album)) } Cassandra.session.execute(batch)loading a CQL statements from a fileCassandra.loadCQLFile("init.CQL")Note:The cluster and session exposed are closed by the Plugin when the application is stopped.#####EvolutionEvolution is enabled by default and the file names are expected to be integers in order,similar to play-evolutions for SQL or SQL-like databases.The configuration property cassandraPlugin.appName should be set when evolution is enabled.The plugin adds an entry for each appName and the default value is appWithCassandraPluginTo disable evolution, add the following to conf/application.conf,cassandraPlugin.evolution.enabled=falseNote: The plugin loads before GlobalSettings, so it is accessible in a custom Global object.####ConfigurationThe default configuration is,cassandraPlugin { //host and port of where Cassandra is running host = "127.0.0.1" port = 9042 evolution { enabled = true directory = "evolutions/cassandra/" //directory within conf to look for CQL files } appName = "appWithCassandraPlugin" // appName to be saved in DB when using evolutions}

Illustration Image

A Play Plugin for using Cassandra

The Plugin initializes a Cassandra Session on startup and provides access to session and other properties through the Cassandra object.

The Plugin also provides play-evolutions like functionality if it is not disabled.

####Usage In library dependencies, include

"com.tuplejump" %% "play-cassandra" % "1.0.0-SNAPSHOT"

Now, Cassandra host, port, cluster and session can be accessed through the API exposed by the Plugin. In addition to that, a method loadCQLFile is also available. The API is documented at TODO

Some examples,

  • executing a query
val query = QueryBuilder.select("title")
      .from("music", "playlists")
      .where(QueryBuilder.eq("id", playlistId))
    val queryResult = Cassandra.session.execute(query).toIterable
  • executing a batch statement
val insertQuery: String = "INSERT INTO music.playlists (id, song_id, title, artist, album) VALUES (?, ?, ?, ?, ?)"
    val ps = Cassandra.session.prepare(insertQuery)
    var batch = new BatchStatement()
    songs.foreach {
      s =>
        batch.add(ps.bind(playlistId, s.id, s.title, s.artist, s.album))
    }
    Cassandra.session.execute(batch)
  • loading a CQL statements from a file
Cassandra.loadCQLFile("init.CQL")

Note:The cluster and session exposed are closed by the Plugin when the application is stopped.

#####Evolution

Evolution is enabled by default and the file names are expected to be integers in order, similar to play-evolutions for SQL or SQL-like databases.

The configuration property cassandraPlugin.appName should be set when evolution is enabled. The plugin adds an entry for each appName and the default value is appWithCassandraPlugin

To disable evolution, add the following to conf/application.conf,

cassandraPlugin.evolution.enabled=false

Note: The plugin loads before GlobalSettings, so it is accessible in a custom Global object.

####Configuration The default configuration is,

cassandraPlugin {
  //host and port of where Cassandra is running
  host = "127.0.0.1"    
  port = 9042           
  
  evolution {
    enabled = true
    directory = "evolutions/cassandra/" //directory within conf to look for CQL files
  }
  
  appName = "appWithCassandraPlugin" // appName to be saved in DB when using evolutions
}

Related Articles

spring
angular
rest

GitHub - jhipster/jhipster-sample-app-cassandra: This is a sample application created with JHipster, with the Cassandra option

jhipster

3/7/2024

Checkout Planet Cassandra

Claim Your Free Planet Cassandra Contributor T-shirt!

Make your contribution and score a FREE Planet Cassandra Contributor T-Shirt! 
We value our incredible Cassandra community, and we want to express our gratitude by sending an exclusive Planet Cassandra Contributor T-Shirt you can wear with pride.

Join Our Newsletter!

Sign up below to receive email updates and see what's going on with our company

Explore Related Topics

AllKafkaSparkScyllaSStableKubernetesApiGithubGraphQl

Explore Further

play