cassandra 没有可用的主机:

Sar*_*shi 3 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', {})
Run Code Online (Sandbox Code Playgroud)

我对 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)
)
Run Code Online (Sandbox Code Playgroud)

mbe*_*com 5

您似乎没有密钥空间: demo

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

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

质量标准:

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)
)
Run Code Online (Sandbox Code Playgroud)