5
votes

I have started working with Cassandra database. I am planning to use Datastax API to upsert/read into/from cassandra database. I am totally new to this Datastax API (which uses new Binary protocol) and I am not able to find lot of documentations as well which have some proper examples.

When I was working with Cassandra CLI using the Netflix client(Astyanax client), then I created the column family like this-

create column family profile
with key_validation_class = 'UTF8Type'
and comparator = 'UTF8Type'
and default_validation_class = 'UTF8Type'
and column_metadata = [
  {column_name : crd, validation_class : 'DateType'}
  {column_name : lmd, validation_class : 'DateType'}
  {column_name : account, validation_class : 'UTF8Type'}
  {column_name : advertising, validation_class : 'UTF8Type'}
  {column_name : behavior, validation_class : 'UTF8Type'}
  {column_name : info, validation_class : 'UTF8Type'}
  ];

Now I was trying to do the same thing using Datastax API. So to start working with Datastax API, do I need to create the column family in some different way as mentioned above? Or the above column familiy will work fine whenever I will try to insert data into Cassandra database using Datastax API.

If the above column family will not work then-

First of all I have created the KEYSPACE like below-

CREATE KEYSPACE USERS WITH strategy_class = 'SimpleStrategy' AND strategy_options:replication_factor = '1';

Now I am confuse how to create the table? I am not sure which is the right way to do that?

Should I create like this?

CREATE TABLE profile ( id varchar, account varchar, advertising varchar, behavior varchar, info varchar, PRIMARY KEY (id) );

or should I create like this?

CREATE COLUMN FAMILY profile ( id varchar, account varchar, advertising varchar, behavior varchar, info varchar, PRIMARY KEY (id) );

And also how to add-

crd as DateType
lmd as DateType

in above table or column family while working with Datastax API?

Any help will be appreciated.

5
what do u mean by datastax API??? OpsCenter or datastax java-driver?abhi
@abhi,Datastax java-driver.arsenal

5 Answers

14
votes

Whether you use the keyword TABLE or COLUMNFAMILY, both are the same (synonyms). I guess the keyword TABLE was introduced with CQL3. So you can use either one in your statements.

Second question, adding DateType, you should use timestamp.

CREATE COLUMNFAMILY sample (rowkey text, ts timestamp, PRIMARY KEY(rowkey));

INSERT INTO sample (rowkey, ts ) VALUES ( '1','1366354711797');
// ts value is basically the System.currentTimeMillis(), I mean a long value
1
votes

In cassandra keyspace or database are same,like wise columnfamily and table are just same.

Cassandra is more like Mysql In its syntax and supports hql(similar to sql)

A table in cassandra can be created like:

CREATE TABLE users (
user_name varchar,
password varchar,
gender varchar,
session_token varchar,
state varchar,
birth_year bigint,
PRIMARY KEY (user_name));

More information here : Cassandra Tutorials

1
votes

@neel4soft - With Cassandra things simply evolve. Therefore in order to be a kind of easier for people, steadily a renaming process is ongoing to make the transition from SQL to CQL easier for newbies. However CQL should not be thought of like being a relative to SQL, rather like a 3rd cousin from the side of it's mother, in other words not a close relative. Therefor comparing it to MySQL is an improper image of it's capabilities.

0
votes

There are few differences while creating table/ colomn family using cassandra-cli and cqlsh.

One of them is, using cassandra-cli if we create table, it will create with compact storage format which is unable to alter further.

In cqlsh, it will not be created in this format unless we mention specifically while created the table/colomn family.

0
votes

A Column Family is a collection of ordered columns and it is a container of the rows and it stores into Cassandra Keyspace and we can create multiple Column Families into a Keyspace.

A Column Family also called an RDBMS Table but the Column Families are not equal to tables.

Each Column Families are stored in separate files on disk. Each row has a unique key which is called Row Key. The Cassandra has also the concept of Super Column Family which is allowing nested access by holding a different set of columns.

In the Column Family, We can set default ordering of data, we can make the compressed table, we can use compact storage, we can set the expiry of data.