Question

I have following table in CQl3

create table userInfo
(
 userid text,
 email text,
 firstName text,
 lastName text,
 city text,
 Primary key (userid,email)
);

Now i want to add two column COL1 and COL2 dynamically to this table as per user selection.How can i do that?

I did following thing

ALTER TABLE ADD COL1 text;
ALTER TABLE ADD COL2 text;

It added the COL1 and COL2 to the table. Then when i do insert into userInfo(..,..,COL1,..) values(..,..,'some value',..) It just put NULL in COL1.

Suggest some good way to add column dynamically. I don't think ALTER the table is good way if i have large number of column to add as I think.I am using Hector library from java program to add column dynamically.

Was it helpful?

Solution

Perhaps CQL is not your best choice. CQL requires you to define your schema up front to allow for SQL-like syntax capability, but it takes away some of the useful dynamic features of Cassandra. Using the Thrift-based mutation capability of nearly any Cassandra client, you can simply name the column you want to write, and if it doesn't exist it will be created for you.

For example:

ks.insert(key, createColumnPath(colName), bytes(value));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top