Creating column family or table in Cassandra while working Datastax API(which uses new Binary protocol)

arsenal picture arsenal · Apr 19, 2013 · Viewed 21.2k times · Source

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.

Answer

abhi picture abhi · Apr 19, 2013

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