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

时间:2015-02-24 12:17:03

标签: python django cassandra-2.0 cqlengine

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

2 个答案:

答案 0 :(得分: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}

答案 1 :(得分:0)

显然,cqlengine尚不支持此功能,并且在不久的将来有提供此功能的开发。所以暂时,它回到使用datastax提供的cassandra python驱动程序,以便在代码中使用它。我会关注cqlengine实现何时可用并反馈到这里。