是什么原因导致“在输入“无”时没有可行的选择”? Cassandra CQL错误 [英] What causes "no viable alternative at input 'None'" error with Cassandra CQL

查看:94
本文介绍了是什么原因导致“在输入“无”时没有可行的选择”? Cassandra CQL错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用新密钥将修改后的文档重新插入Cassandra DB。我很难弄清错误消息指出的是什么问题。当寻找其他遇到类似问题的人时,答案似乎与键有关,在我的情况下,无只是其中几个键的值。我该如何解决这个问题?

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?

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'


推荐答案

没有可行的选择的意思是某些键的数据类型与该列族列的架构不匹配,不幸的是,它在错误消息中并没有明确说明这一点。

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:

map<text,text> 

出于这个原因,在插入时没有一个被认为是错误的值。我通过在插入之前用空dict替换None来解决此问题:

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:

if current['meta'] is None:
    current['meta'] = dict()

CQL驱动程序接受空dict罚款作为地图类型的新值,但不允许None,即使查询map列为空也返回None。

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.

返回None不接受None感觉并不直观,因此后来我决定为cursor.fetchone()创建自定义包装,该包装返回列映射而不是列表列,并检查MapType,ListType或SetType是否返回None。如果没有值,它将用空的dict(),list()或set()替换它们,以避免像将修改后的数据重新插入Cassandra时那样的问题。看来效果不错。

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.

这篇关于是什么原因导致“在输入“无”时没有可行的选择”? Cassandra CQL错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆