简体   繁体   中英

How to store array of objects in Cassandra

In cassandra DB I am planning to store an array-of-object. What is the best way to do that. object mapping to Data mapping with model class in java.

Class Test{
   @Column(name = "id")
   int id,
   @Column(name = "name")
   String name,
   Address[] address

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

Should I put all(id, name, add1, city, state) in one table by adding columns to same keyspace with add1, city, state also? or add new table for address Or any other options..

I have tried to add TYPE But throwing error as: "Error from server: code=2200 [Invalid query] message="A user type cannot contain non-frozen UDTs " From the error and type syntax I have used keyword ' frozen ', but not luck. Altering table also gives similar Error something like : " mismatched input 'frozen' expecting EOF "

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

For your case, first, you need to create a UDT(user defined type) in Cassandra.

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

Then create a table including this UDT.

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

If you want to know more about UTD and the usages, visit following links:

EDIT :

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

Answer : Alter table test add stringarr list<text> Check this links to get more idea about cassandra data types: CQL data types

you can create UDT type:

CREATE TYPE people (
    name text,
    address
);

and now declare your field like this

people set<frozen <people>>

I hope it's help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM