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

5

7

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.

Thermoluminescence answered 19/4, 2013 at 1:57 Comment(2)
what do u mean by datastax API??? OpsCenter or datastax java-driver?Brewmaster
@abhi,Datastax java-driver.Thermoluminescence
B
16

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
Brewmaster answered 19/4, 2013 at 7:3 Comment(3)
Thanks. What is the difference between varchar and text? I need to have String datatypes for most of my columns. So should I use varchar or text in that case?Thermoluminescence
Cool. Thanks. Any thoughts about my other questionThermoluminescence
may be some hours later. Little busy right now. Don't worry I will definitely answer if it is in my range. Any way is this answer not solving your problem as i am seeing status as unaccepted.Brewmaster
G
1

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

Goodden answered 11/8, 2015 at 16:11 Comment(1)
The link to Cassandra Tutorials does not work :(Quickstep
P
1

@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.

Pelf answered 12/8, 2015 at 7:46 Comment(0)
O
0

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.

Operator answered 26/4, 2019 at 9:50 Comment(0)
M
0

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.

Malicious answered 15/2, 2021 at 9:53 Comment(1)
how it's related to the question?Seoul

© 2022 - 2024 — McMap. All rights reserved.