在cassandra DB中插入dict [英] Insert a dict in cassandra DB
问题描述
我有一个带有keyspace的cassandra数据库,如下所示:
I have a cassandra database with a keyspace that looks like:
CREATE TABLE custumer_events_service.events(
acceptFirstContactDuration decimal,
currentTime timestamp,
visitorIdentifier text,
PRIMARY KEY (visitorIdentifier, currentTime)
) WITH CLUSTERING ORDER BY (currentTime DESC);
和一个python dict
And a python dict
{u'acceptFirstContactDuration': 0,
u'currentTime': u'2016-02-18T11:51:55.468Z',
u'12eca72928845aae26268f431b8c75b2564d7919c916e',}
有没有办法直接用 cassandra-driver
?我正在寻找像mongodb中的东西
Is there a way to insert this dict directly with cassandra-driver
? I am looking for something like in 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
推荐答案
可以手动构造一个 CQL
插入语句,遍历字典中的键和值,但 cqlengine
模型
可能比 dict
更适合使用,并且很容易使用中的值填充
。模型
c $ c> dict
You could manually construct a CQL
insert statement iterating over the keys and values in the dictionary, but a cqlengine
Model
is probably more appropriate to use than a dict
, and it is easy to populate a Model
with the values from the dict
.
您可以定义 Model
它与所有列):
You can define the Model
like so (extending it with all of your columns):
class CustomerEventsServiceEvent(Model):
__table_name__ = "customer_events_service.events"
acceptFirstContactDuration = columns.Decimal()
allSeenPageCount = columns.Decimal()
callAcceptedCount = columns.Decimal()
如果 d
是字典变量的名称,则
If d
is the name of your dictionary variable, then
event = CustomerEventsServiceEvent(**d)
event.save()
等同于您的MongoDB操作。
is the equivalent of your MongoDB operation.
这篇关于在cassandra DB中插入dict的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!