What causes “no viable alternative at input 'None'” error with Cassandra CQL

戏子无情 提交于 2019-12-04 01:56:17

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> 

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

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!