在 Cassandra 查询语言 (CQL) 中,有一个名为 source
的便捷命令,它允许用户执行存储在外部文件中的 cql 命令。
SOURCE
Executes a file containing CQL statements. Gives the output for each
statement in turn, if any, or any errors that occur along the way.
Errors do NOT abort execution of the CQL source file.
Usage:
SOURCE '<file>';
但我想知道是否可以允许这个外部文件接受额外的输入参数。 例如,假设我想开发以下带有两个输入参数的 cql 文件:
create keyspace $1 with
strategy_class='SimpleStrategy' and
strategy_options:replication_factor=$2;
并且想通过以下方式在 cqlsh 中执行此 cql:
source 'cql-filename' hello_world 3
我开发了上面的示例 cql,将其存储在名为 create-keyspace.cql
的文件中,并尝试了一些我能想到的可能命令,但它们都不起作用。
cqlsh> source 'create-keyspace.cql'
create-keyspace.cql:2:Invalid syntax at char 17
create-keyspace.cql:2: create keyspace $1 with strategy_class='SimpleStrategy' and strategy_options:replication_factor=$2;
create-keyspace.cql:2: ^
cqlsh> source 'create-keyspace.cql' hello_world 3
Improper source command.
cqlsh> source 'create-keyspace.cql hello_world 3'
Could not open 'create-keyspace.cql hello_world 3': [Errno 2] No such file or directory: 'create-keyspace.cql hello_world 3'
我可以知道CQl是否有这种类型的支撑吗?如果是,那么我应该如何正确执行?
最佳答案
cqlsh 并不是真正想要成为一个脚本环境。听起来使用 Python CQL 驱动程序会更好:https://github.com/datastax/python-driver
关于arguments - 如何在 CQL 命令中包含输入参数 - 源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18136807/