cassandra没有可用的主机:

时间:2016-08-25 00:53:30

标签: python cassandra

我尝试使用以下代码但出错了:

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', {})

我对cassandra有点新鲜,如果有任何帮助,我会在我的大学代理人后面使用它。

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

1 个答案:

答案 0 :(得分:3)

您似乎没有密钥空间:demo

如果您引用了与DataStax Documentation页面上的示例类似的示例,您是否已经创建了demo键空间和用户表?

根据您的上述错误,我假设没有。

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