6

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

Nomad
  • 751
  • 4
  • 13
  • 34

2 Answers2

10

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

MD Ruhul Amin
  • 4,386
  • 1
  • 22
  • 37
  • is there a possibility to add for existing table instead creating table? Because When I try to create typeit throws exception: "Error from server: code=2200 [Invalid query] message="A user type cannot contain non-frozen UDTs"" – Nomad Mar 19 '18 at 14:29
  • Which query get the exception? yeah, you can add a column to an existing table by doing alter command. `Alter table test add address1 list> `. – MD Ruhul Amin Mar 19 '18 at 14:47
  • I updated the question, with the error for both adding type and altering table – Nomad Mar 19 '18 at 14:52
  • 1
    I was doing a mistake over there in query. Now I am able to alter the table with Custom type. Thanks. – Nomad Mar 19 '18 at 15:11
  • 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. – Nomad Mar 21 '18 at 17:48
  • `Alter table test add stringarr list>` or `Alter table test add stringarr list
    ` would do.
    – MD Ruhul Amin Mar 21 '18 at 17:52
2

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

Manish Bohra
  • 340
  • 2
  • 11
  • [https://stackoverflow.com/questions/45222734/cql-data-type-for-storing-an-array-of-object]. instead set can I use array or List? – Nomad Mar 16 '18 at 18:59