CompanyJuly 24, 2020

Developer Newsletter: Flexible Cassandra Data Models with User-Defined Types |  Cassandra 4.0 Beta

Developer Newsletter: Flexible Cassandra Data Models with User-Defined Types |  Cassandra 4.0 Beta

This issue edited by Sushant Kumar (sushantkr16), Technical Architect @ JCPenney

Cassandra being a NoSQL database, denormalization and duplication of data is allowed and considered normal. Cassandra supports many data types and one of them is user-defined types (UDTs). 

UDTs are a kind of custom data type which can be completely user-requirement based and can contain one or more data fields. These data fields have their own individual data types, including collections (Sets, Lists, and Maps). The benefit of a UDT is that it allows you to group multiple fields and store nested objects in the same table rather than in another separate table. 

For example, let’s say we’re planning a marathon. We want to make sure we have all the information related to a marathon such as name, venue, distance, and date stored, and also the basic information of the person who has won the particular marathon. To store this data in Cassandra, either we need to have two different tables, which might not be feasible, or all the columns individually stored, which makes columns cluttered in the whole row.

A better design is to create a table with all the marathon details and use a UDT to store the winner details. Now this UDT can be stored as a single column in the table. This also makes the data very easy to read, as in this JSON response. 

{
    "marathonDate" : "2020-07-26",
    "marathonName" : "LA marathon",
    "distance" : "50m",
    "venue" : "LA",
    "winner" : {
        "firstName" : "Jhon",
        "lastName" : "Doe",
        "marathonParticipantNumber" : "2345"
    }
}

UDTs offer flexibility in storing required details in easily readable formats without greatly impacting performance. DataStax drivers for various languages can serialize and deserialize the data to POJO's/Domain objects. To see UDTs in action in a Java Spring Boot application, check out this example on GitHub.

Notable Software Releases

Upcoming Events 

  • Cassandra Workshop series - We have just reached the halfway mark of this 8 week series! So far we have covered the basics of Cassandra, how to data model with Cassandra and how to build apps with Cassandra. It is not too late to get involved! Catch up and join us for the second half where we delve into monitoring and maintaining your deployments.

Example of the Week

What topics would you like to hear more about? Working on something interesting? We’d love your feedback: developer@datastax.com | @DataStaxDevs

Share

One-stop Data API for Production GenAI

Astra DB gives JavaScript developers a complete data API and out-of-the-box integrations that make it easier to build production RAG apps with high relevancy and low latency.