如何在 Cassandra 中存储对象数组

How to store array of objects in Cassandra

在 cassandra DB 中,我打算存储一个对象数组。最好的方法是什么。 java.

中模型 class 的对象映射到数据映射
Class Test{
   @Column(name = "id")
   int id,
   @Column(name = "name")
   String name,
   Address[] address

 class Address{
   String add1,
   String city,
   String state 
  }
}

我是否应该通过将列添加到与 add1、city、state 相同的键空间来将所有(id、name、add1、city、state)放在一个 table 中?或为地址添加新的 table 要么 任何其他选项..

我尝试添加 TYPE 但抛出错误为:"Error from server: code=2200 [Invalid query] message="A user type cannot contain non-frozen UDTs" 从错误和类型语法来看,我使用了关键字“frozen”,但不是运气。改变 table 也会产生类似的错误,例如:“mismatched input 'frozen' expecting EOF

此外, 如果我必须保存 'String[ ]' 类型的列怎么办,因为它不是像 Address[] 这样的自定义类型。它是字符串还是文本。?我们只需要添加 alter 语句吗?如果是的话它看起来如何

您可以创建 UDT 类型:

CREATE TYPE people (
    name text,
    address
);

然后像这样声明你的字段

people set<frozen <people>>

希望对你有所帮助

对于您的情况,首先,您需要在 Cassandra 中创建一个 UDT(用户定义类型)。

Create TYPE address(
   add1 text,
   city text,
   state text
);

然后创建一个包含此 UDT 的 table。

Create table Test(
   id int,
   name text,
   address list<frozen<address>>,
   primary key(id)
);

如果您想了解更多有关 UTD 及其用法的信息,请访问以下链接:

编辑:

Also, What if I have to save column of type 'String[ ]' As it is not custom type like Address[]. it is of String or text.? Do we need to just add alter statement? if so how it looks like

答案Alter table test add stringarr list<text>查看此链接以了解有关 cassandra 数据类型的更多信息:CQL data types