Jack Crwaford Jack Crwaford - 1 year ago 120
Python Question

Unable to complete the operation against any hosts cassandra

I have tried the previous solutions stated here and here. I am using cassandra-driver by datastax. My version of cassandra is 2.2.8.I have created the keyspace

within it. I inserted values in CT_TABLE through
prompt. Following is the python code for retrieving the rows

from cassandra.cluster import Cluster
cluster = Cluster()
session = cluster.connect('CT_KEYSPACE')
result = session.execute("select * from CT_TABLE")
print result.attribute, result.value

Following is the output of DESCRIBE CT_KEYSPACE:

cqlsh> describe ct_keyspace

CREATE KEYSPACE ct_keyspace WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'} AND durable_writes = true;

CREATE TABLE ct_keyspace.ct_table (
attribute text PRIMARY KEY,
value int
) WITH bloom_filter_fp_chance = 0.01
AND caching = '{"keys":"ALL", "rows_per_partition":"NONE"}'
AND comment = ''
AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy'}
AND compression = {'sstable_compression': 'org.apache.cassandra.io.compress.LZ4Compressor'}
AND dclocal_read_repair_chance = 0.1
AND default_time_to_live = 0
AND gc_grace_seconds = 864000
AND max_index_interval = 2048
AND memtable_flush_period_in_ms = 0
AND min_index_interval = 128
AND read_repair_chance = 0.0
AND speculative_retry = '99.0PERCENTILE';

Following is the error I am getting:

Traceback (most recent call last):
File "connection.py", line 4, in <module>
result = session.execute("select * from CT_TABLE")
File "/usr/local/lib/python2.7/dist-packages/cassandra/cluster.py", line 1998, in execute
return self.execute_async(query, parameters, trace, custom_payload, timeout, execution_profile, paging_state).result()
File "/usr/local/lib/python2.7/dist-packages/cassandra/cluster.py", line 3781, in result
raise self._final_exception
cassandra.cluster.NoHostAvailable: ('Unable to complete the operation against any hosts', {})

Any help is greatly appreciated.

Answer Source

This is happening because you are providing the wrong keyspace name in connect. Use "ct_keyspace" instead of "CT_KEYSPACE". You may recall that unquoted identifiers are case-insensitive in CQL (such as your select statement). However, in the context of parameter passing (as in Cluster.connect(<keyspace>)) that literal is not case-insensitive.

If you enable logging, you would see the driver continually failing to set that keyspace on the connection. I created a ticket to improve error handling in this scenario: https://datastax-oss.atlassian.net/browse/PYTHON-665

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download