MST

星途 面试题库

面试题:Cassandra用户自定义类型在复杂数据建模中的应用

假设你要设计一个社交网络应用的数据库,其中用户的兴趣爱好(每种爱好有名称、描述和相关标签)需要存储在Cassandra中。请详细说明如何使用用户自定义类型来构建这个数据模型,包括创建UDT、创建表以及插入数据的操作。
27.0万 热度难度
数据库Cassandra

知识考点

AI 面试

面试题答案

一键面试
  1. 创建UDT(用户自定义类型)
    CREATE TYPE hobby_type (
        hobby_name text,
        hobby_description text,
        hobby_tags list<text>
    );
    
  2. 创建表
    CREATE TABLE users (
        user_id uuid PRIMARY KEY,
        user_name text,
        hobbies set<hobby_type>
    );
    
  3. 插入数据
    INSERT INTO users (user_id, user_name, hobbies)
    VALUES (
        uuid(),
        'John Doe',
        {
            {
                'hobby_name': 'Reading',
                'hobby_description': 'Love to read books of various genres',
                'hobby_tags': ['fiction', 'non - fiction', 'classics']
            },
            {
                'hobby_name': 'Traveling',
                'hobby_description': 'Explore new places around the world',
                'hobby_tags': ['adventure', 'culture', 'nature']
            }
        }
    );