I'm attempting to insert a modified document back to Cassandra DB with a new key. I'm having hard time figuring out what is the issue the error message is pointing at. When looking for others that have had similar problems the answers seem to be related to the keys, and in my case the None is just a value of few of the keys. How do I solve this issue?
我正在尝试用一个新键将修改后的文档插入到Cassandra DB中。我很难弄清楚错误信息指向的问题是什么。在寻找有类似问题的人时,答案似乎与键有关,在我的例子中,None只是几个键的值。我如何解决这个问题?
keys = ','.join(current.keys())
params = [':' + x for x in current.keys()]
values = ','.join(params)
query = "INSERT INTO wiki.pages (%s) Values (%s)" % (keys, values)
query = query.encode('utf-8')
cursor.execute(query, current)
Here's the data for query and current:
以下是查询和当前数据:
INSERT INTO wiki.pages (changed,content,meta,attachment,revision,page,editor)
VALUES (:changed,:content,:meta,:attachment,:revision,:page,:editor)
{
u'changed': '2013-02-15 16:31:49',
u'content': 'Testing',
u'meta': None,
u'attachment': None,
u'revision': 2,
u'page': u'FrontPage',
u'editor': 'Anonymous'
}
This fails with the following error:
这一错误失败的原因如下:
cql.apivalues.ProgrammingError:
Bad Request: line 1:123 no viable alternative at input 'None'
1 个解决方案
#1
13
The "no viable alternative" means that the data type for some key doesn't match the schema for that column family column, unfortunately it doesn't plainly say that in the error message.
“没有可行的替代方案”意味着某些键的数据类型与列族列的模式不匹配,不幸的是,它并没有在错误消息中明确地指出这一点。
In my case the data type for meta was:
在我的例子中,meta的数据类型是:
map<text,text>
for this reason None was considered a bad value at insertion time. I fixed the problem by replacing the None with an empty dict prior to insert:
由于这个原因,在插入时没有一个被认为是坏值。在插入之前,我用一个空的命令替换了None,从而解决了这个问题:
if current['meta'] is None:
current['meta'] = dict()
The CQL driver accepts empty dict fine as new value for a map type, while None is not allowed, even though querying the map column returns None if it is empty.
CQL驱动程序接受空的dict罚款作为一个映射类型的新值,而不允许任何值,即使查询映射列如果它是空的,则返回None。
Returning None and not accepting None did not feel intuitive, so later I decided to create custom wrapper for cursor.fetchone() that returns a map of columns instead of a list of columns, and also checks if MapType, ListType or SetType has returned None. If there are None values, it replaces them with empty dict(), list() or set() to avoid issues like the one I had when inserting modified data back to Cassandra. This seems to work nicely.
返回None和不接受None感觉不太直观,所以后来我决定为cursor.fetchone()创建自定义包装器,它返回列的映射而不是列的列表,并检查MapType、ListType或SetType是否返回None。如果没有值,它会用空的dict()、list()或set()替换它们,以避免出现类似我在向Cassandra插入修改后的数据时遇到的问题。这似乎运行得很好。
#1
13
The "no viable alternative" means that the data type for some key doesn't match the schema for that column family column, unfortunately it doesn't plainly say that in the error message.
“没有可行的替代方案”意味着某些键的数据类型与列族列的模式不匹配,不幸的是,它并没有在错误消息中明确地指出这一点。
In my case the data type for meta was:
在我的例子中,meta的数据类型是:
map<text,text>
for this reason None was considered a bad value at insertion time. I fixed the problem by replacing the None with an empty dict prior to insert:
由于这个原因,在插入时没有一个被认为是坏值。在插入之前,我用一个空的命令替换了None,从而解决了这个问题:
if current['meta'] is None:
current['meta'] = dict()
The CQL driver accepts empty dict fine as new value for a map type, while None is not allowed, even though querying the map column returns None if it is empty.
CQL驱动程序接受空的dict罚款作为一个映射类型的新值,而不允许任何值,即使查询映射列如果它是空的,则返回None。
Returning None and not accepting None did not feel intuitive, so later I decided to create custom wrapper for cursor.fetchone() that returns a map of columns instead of a list of columns, and also checks if MapType, ListType or SetType has returned None. If there are None values, it replaces them with empty dict(), list() or set() to avoid issues like the one I had when inserting modified data back to Cassandra. This seems to work nicely.
返回None和不接受None感觉不太直观,所以后来我决定为cursor.fetchone()创建自定义包装器,它返回列的映射而不是列的列表,并检查MapType、ListType或SetType是否返回None。如果没有值,它会用空的dict()、list()或set()替换它们,以避免出现类似我在向Cassandra插入修改后的数据时遇到的问题。这似乎运行得很好。