質問

I want to insert data in a composite column family through CQL 3.

create column family marks with 
comparator = 'CompositeType(DateType,UTF8Type,UTF8Type)' AND 
key_validation_class=UTF8Type AND
default_validation_class=UTF8Type;

The structure will be made one time with the cassandra-cli shell and result will be saved in cassandra as

**63**  (2013-06-04 00:00:00 UTC, Science, 89.00): ''
        (2013-06-04 00:00:00 UTC, Mathematics, 80.00): ''
        (2013-06-04 00:00:00 UTC, English, 90.00):''

Here the row key is 63 which is Primary Key and Unique. And data will be saved in cassandra like above only in the column name. What will be the insert query for this and what will be the most suitable driver to achieve this CQL3 or thrift.

役に立ちましたか?

解決

In CQL3, you do this by using a compound primary key:

CREATE TABLE marks (
  id text,
  date timestamp,
  subject text,
  mark text,
  PRIMARY KEY (id, date, subject, mark)
)

With this schema, id is the row key since it is listed first. The column names are a composite of date:subject:mark.

You can then insert:

insert into marks (id, date, subject, mark) values ('63', '2013-06-04 00:00:00 UTC', 'Science', '89.00');
insert into marks (id, date, subject, mark) values ('63', '2013-06-04 00:00:00 UTC', 'Mathematics', '80.00');
insert into marks (id, date, subject, mark) values ('63', '2013-06-04 00:00:00 UTC', 'English', '90.00');

And list:

> select * from marks;

 id | date                     | subject     | mark
----+--------------------------+-------------+-------
 63 | 2013-06-04 01:00:00+0100 |     English | 90.00
 63 | 2013-06-04 01:00:00+0100 | Mathematics | 80.00
 63 | 2013-06-04 01:00:00+0100 |     Science | 89.00

You might want to store the mark as int (or potentially float) so you can do numeric comparisons in your queries.

You could also store your marks in the column value rather than column name. To do this remove mark from the primary key. Then you could e.g. build a secondary index on marks.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top