在卡桑德拉数据库中插入dict

2024-10-02 14:22:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个cassandra数据库,其键空间如下:

CREATE TABLE custumer_events_service.events(
    acceptFirstContactDuration decimal,
    currentTime timestamp,
    visitorIdentifier text,
    PRIMARY KEY (visitorIdentifier, currentTime)
) WITH CLUSTERING ORDER BY (currentTime DESC);

还有一个python dict

^{pr2}$

有没有办法直接用cassandra-driver插入这个dict?我在找类似mongodb的东西

>>> import datetime
>>> post = {"author": "Mike",
...         "text": "My first blog post!",
...         "tags": ["mongodb", "python", "pymongo"],
...         "date": datetime.datetime.utcnow()}

>>> posts = db.posts
>>> post_id = posts.insert_one(post).inserted_id

Tags: textid数据库datetimemongodbcreatetable空间
1条回答
网友
1楼 · 发布于 2024-10-02 14:22:11

您可以手动构造一个迭代字典中的键和值的CQLinsert语句,但是^{} ^{}可能比dict更适合使用,而且很容易用dict中的值填充Model。在

您可以像这样定义Model(用所有列扩展它):

class CustomerEventsServiceEvent(Model):
    __table_name__ = "customer_events_service.events"

    acceptFirstContactDuration = columns.Decimal()
    allSeenPageCount = columns.Decimal()
    callAcceptedCount = columns.Decimal()

如果d是dictionary变量的名称,则

^{pr2}$

相当于您的MongoDB操作。在

相关问题 更多 >