如何在 cqlengine 的 python 模型中表示 cassandra 用户定义类型

How do I represent a cassandra user defined type in a python model in cqlengine

我的 cassandra 集群中定义了以下 table 模式

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()

显然,此功能在 cqlengine 中尚不支持,并且正在朝着在不久的将来提供此功能的方向发展。因此,目前,它回到利用 datastax 提供的 cassandra python 驱动程序来使它在代码中工作。我会留意 cqlengine 实现何时可用并在此处反馈。

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}