How do I execute Cassandra CLI commands from a Python script?

前端 未结 1 994
小鲜肉
小鲜肉 2021-01-22 16:36

I have a python script that I want to use to make remote calls on a server, connect to Cassandra CLI, and execute commands to create keyspaces. One of the attempts that I made

1条回答
  •  有刺的猬
    2021-01-22 17:11

    try this out. I don't have cassandra-cli installed on my machine, so I couldn't test it myself.

    from subprocess import check_output
    from tempfile import NamedTemporaryFile
    CASSANDRA_CMD = 'cassandra-cli -host localhost -port 1960 -f '
    
    def cassandra(commands):
        with NamedTemporaryFile() as f:
            f.write(';\n'.join(commands))
            f.flush()
            return check_output(CASSANDRA_CMD + f.name, shell=True)
    
    cassandra(['CREATE KEYSPACE someguy', 'exit'])
    

    As you mentioned in the comment below pycassa a Python client for Cassandra cannot be used since it doesn't seem to support create statements.

    0 讨论(0)
提交回复
热议问题