How can I save array of objects in cassandra?
I'm using a nodeJS application and using cassandra-driver to connect to Cassandra DB. I wanted to save records like below in my db:
{
"id" : "5f1811029c82a61da4a44c05",
"logs" : [
{
"conversationId" : "e9b55229-f20c-4453-9c18-a1f4442eb667",
"source" : "source1",
"destination" : "destination1",
"url" : "https://asdasdas.com",
"data" : "data1"
},
{
"conversationId" : "e9b55229-f20c-4453-9c18-a1f4442eb667",
"source" : "source2",
"destination" : "destination2",
"url" : "https://afdvfbwadvsffd.com",
"data" : "data2"
}
],
"conversationId" : "e9b55229-f20c-4453-9c18-a1f4442eb667"
}
In the above record, I can use type "text" to save values of the columns "id" and "conversationId". But not sure how can I define the schema and save data for the field "logs".
With Cassandra, you'll want to store the data in the same way that you want to query it. As you mentioned querying by
conversatonid, that's going to influence how thePRIMARY KEYdefinition should look. Given this,conversationid, should make a good partition key. As for the clustering columns, I had to make some guesses as to cardinality. So,sourceidlooked like it could be used to uniquely identify a log entry within a conversation, so I went with that next.I thought about using
idas the final clustering column, but it looks like all entries with the sameconversationidwould also have the sameid. It might be a good idea to give each entry its own unique identifier, to help ensure uniqueness:This makes the final table definition look like this: