Create first entity using Cassandra

Create first entity using Cassandra

This project source code is available on github at https://github.com/deanhiller/playorm.

Assuming that you have created the entity User as given here for in-memory, let us see how it can be saved in Cassandra after writing below code.

import java.util.HashMap;
import java.util.Map;
import com.alvazan.orm.api.base.Bootstrap;
import com.alvazan.orm.api.base.DbTypeEnum;
import com.alvazan.orm.api.base.NoSqlEntityManager;
import com.alvazan.orm.api.base.NoSqlEntityManagerFactory;
import com.example.db.User;

public class Example {
private static NoSqlEntityManager mgr;

public static void main(String[] args) {
Map properties = new HashMap();
properties.put(Bootstrap.AUTO_CREATE_KEY, "create");

String clusterName = "PlayCluster";
String seeds = "localhost:9160";
String keyspace = "nosqlkeyspace";
Bootstrap.createAndAddBestCassandraConfiguration(properties, clusterName, keyspace, seeds);
NoSqlEntityManagerFactory factory = Bootstrap.create(DbTypeEnum.CASSANDRA, properties,null,null);
mgr = factory.createEntityManager();

User user1 = new User();

Command Line Tool:
You can use Playorm’s Command Line Tool for seeing the details of above entity. Start the tool as (make sure that Playorm.jar is in your classpath along with other dependencies of Playorm.)

java com.alvazan.ssql.cmdline.PlayOrm -k nosqlkeyspace -s localhost:9160 -t cassandra -c CL_ONE


you can use the scripts as well:

For Windows:

<PLAYORM_HOME\tools>playorm-cli.bat -k nosqlkeyspace -s localhost:9160 -t cassandra -c CL_ONE


For Unix:

 <PLAYORM_HOME/tools>playorm-cli -k nosqlkeyspace -s localhost:9160 -t cassandra -c CL_ONE

* Please note that -c is an optional parameter. The default value of this parameter is CL_QUORUM

This will display a message and a prompt like below:

Welcome to PlayOrm Command Line
Type 'help;' for help
Type 'exit;' to exit
playorm >>

Enter a query SQL query to query the database. e.g., select * from User

playorm >> select * from User;
----------- JoinedRow0
RowKey:13360274564:SF-PC (id)
=> age = 25
=> lastName = Todd
=> name = Jim
1 Rows returned
playorm >>

You may also view more details about Playorm’s Command Line Tool.


<< Back to Documentation Index