Can we use Cassandra as a Key-Value Store?
Asked Answered
A

1

6

I have a simple question about using Cassandra as a key-value data store. I am stil new to Casandra concepts and I have searched for an answer for this question in several places but, I could not find a solid answer for this.

Let's consider following scenario where I have set of sensor values to be stored in a database where the sensor names will be determined in runtime.

In scenario 1, I am getting following values

{"id": "001", "sensor1" : 25, "sensor2":15}

In scenario 2, I am getting following values,

{"id": "002", "sensor1" : 25, "sensor3":30}

So in these scenarios, If I use a key-value datastore such as AWS Dynamodb to store data then it would look like something as follows,

+-----+---------+---------+---------+
| ID  | sensor1 | sensor2 | sensor3 |
+-----+---------+---------+---------+
| 001 | 25      | 15      |         |
| 002 | 25      |         | 30      |
+-----+---------+---------+---------+

But, my problem is can we accomplish the same thing with Cassandra since it is not complete key-value store.

In cassandra, I need to define table before I start persisting data into it. So in that case I need to know all of my sensor names prior to that.

I am not sure whether we can dynamically add columns to Cassandra table when we add data through 'insert' cql.

I am aware that I can store data as a map inside one of defined column in cassandra but, that it not what we do with key-value stores.

Ancestress answered 14/3, 2018 at 14:23 Comment(0)
B
7

You need to put ID as partition key, and the sensor name as clustering column, something like:

create table sensors (
  id int,
  sensor text,
  value int,
  primary key(id, sensor));

then you could make queries like this (to select all data for given measurement):

select * from sensors where id = 001;

or his (to select data for given measurement and sensor):

select * from sensors where id = 001 and sensor = 'sensor1';
Bazil answered 14/3, 2018 at 15:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.