0

I have the following object which I need to store in Cassandra. Do I need to use UDT or is there any other way to store the object. I need to finally store this from spring-boot application using Repository approach.

{
    "name": "Krishna",
    "age" : 24,
    "address" : [
        {
            "attributes" : [
                {
                    "name": "",
                    "value" : ""
                }
            ],
            "contactnumbers" : [ "123123234", "123456"]
        }
    ],
    "devices" : [
        "android_pixel",
        "ios_6s"
    ]
}
Krishna
  • 1,089
  • 5
  • 24
  • 38

1 Answers1

3

I think, you need to use UDT here. I could come up with this.

CREATE TYPE attribute_info (
    name text,
    value text
);

CREATE TYPE address_info (
    attributes list<frozen<attribute_info>>,
    contactnumbers list<text> 
);

CREATE TYPE user_info (
    name text,
    age int,
    address list<frozen<address_info>>,
    devices list<text>
);

Once you get user_info under your key space, use it as a type for the column while creating or altering the table.

Raj Parekh
  • 192
  • 1
  • 9
  • Yup. this is one way that I found and I'm implementing it. Is there any way to insert the documents(JSON) into Cassandra like the way we insert the documents in MongoDB since cassandra is also an NoSQL database? – Krishna May 22 '19 at 06:32
  • 1
    Storing JSON in Cassandra column as text is another way but I wouldn't recommend it. Even if we store it as text, we need to compress it. You can fine more [here](https://stackoverflow.com/a/36344778/4523512). Cassandra is more similar to key-value-based NoSQL databases. 3rd option is to use [JSON to write to Cassandra tables](https://www.bmc.com/blogs/using-json-with-cassandra/) but that depends on how your application wants to access this data. Reference [link](https://medium.com/code-zen/cassandra-schemas-for-beginners-like-me-9714cee9236a) to understand Cassandra schema. – Raj Parekh May 22 '19 at 09:42