2

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 was something to this effect:

connect="cassandra-cli -host localhost -port 1960;"
create_keyspace="CREATE KEYSPACE someguy;"
exit="exit;"

final = Popen("{}; {}; {}".format(connect, create_keyspace, exit), shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
stdout, nothing = final.communicate()

Looking through various solutions, I'm not finding what I need. For example, the above code is throwing a "/bin/sh: 1: CREATE: not found", which I think means that it's not executing the CREATE statement on the CLI command line.

Any/all help would be GREATLY appreciated! Thank you!

4

1 に答える 1

1

これを試してください。私のマシンには cassandra-cli がインストールされていないため、自分でテストできませんでした。

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

以下の pycassaのコメントで述べたように、 Cassandra の Python クライアントは、create ステートメントをサポートしていないように見えるため、使用できません。

于 2012-11-12T16:52:26.793 に答える