Skip to main content
 首页 » 编程设计

python之带有 Python 的 Cassandra 3.7, 'NoneType' 对象没有属性 'encode_message'

2024年10月17日7oomusou

我有一个简单的 python 客户端来从 cassandra 获取数据。查询字符串本身使用 DBeaver EE 客户端运行良好。

def getDataSet(self, station_id, tablename): 
    prepared_stmt = self.session.prepare ( "SELECT event_time, reading FROM " + 
                                           tablename + " WHERE station_id = ?;") 
    bound_stmt = prepared_stmt.bind([station_id]) 
    rslt = self.session.execute(bound_stmt) 
    df = pd.DataFrame() 
    for r in rslt: 
        df = df.append(r) 
    return df 
 
def __init__(self): 
    cluster = Cluster( 
        contact_points=['127.0.0.1'], 
        ) 
    self.session = cluster.connect('data') 
    self.session.row_factory = tuple_factory 
    self.session.client_protocol_handler = NumpyProtocolHandler 

我得到以下异常

File "/home/david/git/python-example/src/start/fetchcassandra.py", line 13, in getDataSet rslt = self.session.execute(bound_stmt) File "/usr/local/lib/python3.4/dist-packages/cassandra/cluster.py", line 1961, in execute return self.execute_async(query, parameters, trace, custom_payload, timeout, execution_profile).result() File "/usr/local/lib/python3.4/dist-packages/cassandra/cluster.py", line 3649, in result raise self._final_exception cassandra.cluster.NoHostAvailable: ('Unable to complete the operation against any hosts', {<Host: 127.0.0.1 datacenter1>: AttributeError("'NoneType' object has no attribute 'encode_message'",)})

请您参考如下方法:

很可能未构建 NumpyProtocolHander,因此您将指定 None 作为协议(protocol)处理程序。

参见 here有关安装说明,并查阅您的 pip 日志以获取有关未构建它的原因的线索。