在Cassandra中保存对象数组 [英] Save array of objects in cassandra

查看:88
本文介绍了在Cassandra中保存对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在cassandra中保存对象数组?
我正在使用nodeJS应用程序,并使用cassandra驱动程序连接到Cassandra DB.我想在我的数据库中保存如下记录:

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"
}  

在上面的记录中,我可以使用类型"text"保存列"id"的值.和"conversationId".但是不确定如何定义架构并保存字段"logs"的数据.

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

推荐答案

根据查询数据的方式,您有几种选择.

You have a few options depending on how you want to query this data.

首先是在日志字段中将json字符串化,并将其保存到数据库中,然后在查询数据后将其转换回JSON.

The first is to stringify the json in logs field and save that to the database and then convert it back to JSON after querying the data.

第二个选项与第一个选项相似,但是您无需将数组字符串化,而是将数据作为列表存储在数据库中.

The second option is similar to the first, but instead of stringifying the array, you store the data as a list in the database.

第三个选项是使用对话的主键和日志的每个元素的聚类键为日志定义一个新表.这将允许您通过全键查找或仅通过主键查询,并检索与那些条件匹配的所有行.

The third option is to define a new table for the logs with a primary key of the conversation and clustering keys for each element of the logs. This will allow you to lookup either by the full key or query by just the primary key and retrieve all the rows that match those criteria.

CREATE TABLE conversationlogs (
  conversationid uuid,
  logid timeuuid,
  ...
  PRIMARY KEY ((conversationid), logid));

这篇关于在Cassandra中保存对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆