【问题标题】:Is it possible to use cassandra cqlsh copy command inside the python programme?是否可以在 python 程序中使用 cassandra cqlsh copy 命令?
【发布时间】:2017-02-12 08:13:13
【问题描述】:

是否可以通过 Python 程序执行 Cassandra CQLSH 复制命令。如果是,怎么做?

提前致谢。

【问题讨论】:

    标签: python cassandra


    【解决方案1】:

    是的,这是可能的。我正在为您提供有关我们如何在 python 中使用 cqlsh 的示例代码。我想,你可以从中得到一些帮助,但没有兴趣为你编写整个复制脚本。

     import subprocess
    
     host =  "xxx.xxx.xxx.xxx"
     user = "cassandra"
     password = "cassandra"
     process = subprocess.Popen("cqlsh " + host + " -u " + user + " -p " + password, shell=True)
     exitCode = process.wait()
     if exitCode == 0:
         print "Job Done!"
     else:
         print "Still working!"
    

    【讨论】:

    • 上述sn-p中的copy语句在哪里使用(如问题中所问)?这些更多的是配置语句,而不是展示如何使用复制语句。
    • 我在python程序里面提供了cqlsh的样例代码,对会写python程序的人来说,这个样例就足够了。同样,如果您想要更多提示来完成此操作,请将您的复制命令放在subprocess 中,它会起作用。
    【解决方案2】:

    您可以通过使用 cassandra 命令登录、使用键空间并在单个命令中同时执行查询来使用以下代码。

    上述的 Python 等价物将是:

    command = f"""cqlsh {host} -u {username} -p '{password}' -k '{db_name}' -e \" COPY played_logs FROM '{file_path}' WITH DELIMITER = ','; \" """
    

    然后使用os 模块只需将命令传递给shell:

    os.system(command)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-02
      • 2014-08-18
      • 2015-04-30
      • 1970-01-01
      • 1970-01-01
      • 2020-07-20
      • 2016-08-28
      • 1970-01-01
      相关资源
      最近更新 更多