【问题标题】:How do I represent a cassandra user defined type in a python model in cqlengine如何在 cqlengine 的 python 模型中表示 cassandra 用户定义的类型
【发布时间】:2015-02-24 12:17:02
【问题描述】:

我在我的 cassandra 集群中定义了以下表架构

CREATE TABLE users (
username text PRIMARY KEY,
creationdate bigint,
email text,
firstlogin boolean,
firstname text,
lastloggedin bigint,
lastname text,
lastprofileupdate bigint,
name text,
olduserid int,
profile frozen<profile_type>,
user_id uuid

和用户定义的类型,profile_type 如下...

CREATE TYPE profile_type (
birthdate timestamp,
gender text,
title text,
relationshipstatus text,
homecountry text,
currentcountry text,
timezone text,
profilepicture blob,
alternate_email text,
religion text,
interests list<text>,
cellphone text,
biography text
);

如何将此结构表示为 cqlengine 模型?我对用户定义的类型表示特别感兴趣,因为我没有看到任何列定义来表示这样的?然后我需要手动映射吗?到目前为止,我在 python 中有这个....

class User(Model):
    username = columns.Text(primary_key=True)
    firstname = columns.Text(required=True)
    lastname = columns.Text(required=True)
    email = columns.Text(required=True)
    name = columns.Text(required=False)
    olduserid = columns.Integer()
    user_id = columns.UUID(default=uuid.uuid4)
    creationdate = columns.BigInt()

【问题讨论】:

    标签: python django cassandra-2.0 cqlengine


    【解决方案1】:

    cqlengine支持UDT,请参考this

    from cassandra.cqlengine.columns import *
    from cassandra.cqlengine.models import Model
    from cassandra.cqlengine.usertype import UserType
    
    class address(UserType):
        street = Text()
        zipcode = Integer()
    
    class users(Model):
        __keyspace__ = 'account'
        name = Text(primary_key=True)
        addr = UserDefinedType(address)
    
    sync_table(users)
    
    users.create(name="Joe", addr=address(street="Easy St.", zip=99999))
    user = users.objects(name="Joe")[0]
    print user.name, user.addr
    # Joe {'street': Easy St., 'zipcode': None}
    

    【讨论】:

      【解决方案2】:

      显然,cqlengine 尚不支持此功能,并且在不久的将来会提供此功能。所以目前,它回到了利用 datastax 提供的 cassandra python 驱动程序来让它在代码中工作。我会留意 cqlengine 实现何时可用并在这里反馈。

      【讨论】:

        猜你喜欢
        • 2019-03-18
        • 2021-02-06
        • 2018-01-04
        • 2022-08-18
        • 1970-01-01
        • 2019-06-01
        • 2014-12-31
        • 2016-03-21
        • 2016-09-01
        相关资源
        最近更新 更多