Sarthak Joshi
Sarthak Joshi

Reputation: 41

cassandra no host available:

I tried using the following code but gave an error:

File "cassandra/cluster.py", line 1961, 
    in cassandra.cluster.Session.execute (cassandra/cluster.c:34076)
File "cassandra/cluster.py", line 3649, 
    in cassandra.cluster.ResponseFuture.result (cassandra/cluster.c:69755)
cassandra.cluster.NoHostAvailable: 
    ('Unable to complete the operation against any hosts', {})

I am a bit new to cassandra and I am using it behind my college proxy if that comes to any help.

from cassandra.cluster import Cluster
cluster=Cluster(['127.0.0.1'],port=9042)
session=cluster.connect('demo')
session.execute(
    """
    INSERT INTO users (name, credits)
    VALUES (%s, %s)
    """,
    ("John O'Reilly", 42)
)

Upvotes: 3

Views: 12221

Answers (1)

mbeacom
mbeacom

Reputation: 1520

It appears that you don't have a keyspace: demo

If you're referencing a similar example to the one on the DataStax Documentation page, did you already create the demo keyspace and the user table?

Based on your above error, I am assuming no.

CQL:

from cassandra.cluster import Cluster


cluster = Cluster(['127.0.0.1'], port=9042)
session = cluster.connect()  # Don't specify a keyspace here, since we haven't created it yet.

# Create the demo keyspace
session.execute(
    """
    CREATE KEYSPACE IF NOT EXISTS demo WITH REPLICATION = {
        'class' : 'SimpleStrategy',
        'replication_factor' : 1
    }
    """
)

# Set the active keyspace to demo
# Equivalent to:  session.execute("""USE demo""")
session.set_keyspace('demo')

# Create the users table
# This creates a users table with columns: name (text) and credits (int)
session.execute(
    """
    CREATE TABLE users (
        name text PRIMARY KEY,
        credits int
    );
    """
)

# Execute your original insert
session.execute(
    """
    INSERT INTO users (name, credits)
    VALUES (%s, %s)
    """,
    ("John O'Reilly", 42)
)

Upvotes: 5

Related Questions