【问题标题】:error ... ImportError: No module named cassandra.cluster错误... ImportError:没有名为 cassandra.cluster 的模块
【发布时间】:2020-03-20 07:00:26
【问题描述】:

我正在尝试遵循以下有关写入路径的视频,当我使用提供的示例学生文件时,我得到以下错误....我对 cassandra 真的很陌生,并试图弄清楚为什么练习文件不工作....我提供了我得到的错误(ImportError:没有名为 cassandra.cluster 的模块)以及 .sh 和 .py 文件...任何帮助都可以得到...

https://academy.datastax.com/courses/understanding-cassandra-write-path/understanding-data-files

    cass@cass:~/student-files/write-path/exercise-1$ ccm list
 *demo_1node
cass@cass:~/student-files/write-path/exercise-1$
cass@cass:~/student-files/write-path/exercise-1$ ccm status
Cluster: 'demo_1node'
---------------------
node1: UP
cass@cass:~/student-files/write-path/exercise-1$
cass@cass:~/student-files/write-path/exercise-1$ ./write_data.sh 300000
Traceback (most recent call last):
  File "./write_data.py", line 5, in <module>
    from cassandra.cluster import Cluster
ImportError: No module named cassandra.cluster
cass@cass:~/student-files/write-path/exercise-1$

cass@cass:~/student-files/write-path/exercise-1$ cat write_data.sh
#!/bin/bash

if [ $# -ne 1 ]; then
  echo "Usage: write_data.sh <number of keys>"
  exit 0
fi

if [ `ccm status | grep "node1: UP" | wc -l` -ne 1 ]; then
  echo "Cassandra cluster not up"
  exit 0
fi

./write_data.py $1
cass@cass:~/student-files/write-path/exercise-1$
cass@cass:~/student-files/write-path/exercise-1$ cat write_data.py
#!/usr/bin/python

# This Python script will insert a number of keys into
# musicdb.user
from cassandra.cluster import Cluster
from random import randint
from sets import Set
from uuid import uuid4
import os,sys,time,binascii

if len(sys.argv) < 2:
    print "Usage: python.py <number of keys>"
    sys.exit()

cluster = Cluster(['127.0.0.1'])
session = cluster.connect()

insert_row_prepare = session.prepare("INSERT INTO musicdb.user (id,preferences) VALUES(?,?)")

quarter=int(int(sys.argv[1])/4)
half=int(int(sys.argv[1])/2)
three_quarter=int(int(sys.argv[1])/4)+int(int(sys.argv[1])/2)

for x in range(0,int(sys.argv[1])):
  id = uuid4()
  set = Set([binascii.b2a_hex(os.urandom(100)),binascii.b2a_hex(os.urandom(100)),binascii.b2a_hex(os.urandom(100)),
             binascii.b2a_hex(os.urandom(100)),binascii.b2a_hex(os.urandom(100)),binascii.b2a_hex(os.urandom(100)),
             binascii.b2a_hex(os.urandom(100)),binascii.b2a_hex(os.urandom(100)),binascii.b2a_hex(os.urandom(100)),
             binascii.b2a_hex(os.urandom(100)),binascii.b2a_hex(os.urandom(100)),binascii.b2a_hex(os.urandom(100))])
  insert_row_bind = insert_row_prepare.bind([id,set])
  session.execute(insert_row_bind)

  if (x+1) == int(sys.argv[1]):
    print "100% completed - " + str(x+1) + " rows inserted."
  elif (x+1) == quarter:
    print "25% completed - " + str(x+1) + " rows inserted."
  elif (x+1) == half:
    print "50% completed - " + str(x+1) + " rows inserted."
  elif (x+1) == three_quarter:
    print "75% completed - " + str(x+1) + " rows inserted."

time.sleep(1)
cluster.shutdown()
cass@cass:~/student-files/write-path/exercise-1$

【问题讨论】:

  • 你安装datastax python驱动了吗?
  • 谢谢你,理查德,正如我所提到的,我对此真的很陌生......但是在安装了 python 驱动器后,它似乎可以使用一个警告......
  • cass@cass:~/student-files/write-path/exercise-1$ ./write_data.sh 3000 /usr/local/lib/python2.7/dist-packages/cassandra/cqltypes .py:64:UserWarning:blist 库不可用,因此将使用普通集合代替 blist.sortedset 来获取集合值。您可以在此处找到 blist 库:pypi.python.org/pypi/blist“blist 库不可用,因此正常集合将”完成 25% - 插入了 750 行。 50% 完成 - 插入 1500 行。 75% 完成 - 插入了 2250 行。 100% 完成 - 插入了 3000 行。 cass@cass:~/student-files/write-path/exercise-1$
  • 这是一个警告,你可以忽略它。看起来它现在正在工作。
  • 谢谢理查德。对不起,我也是stackoverflow的新手。我如何将您的答案标记为正确?

标签: cassandra


【解决方案1】:

安装cassandra驱动:

pip install cassandra-driver

【讨论】:

    【解决方案2】:

    我带着同样的问题来到这里。当我看到答案时,我意识到我可能安装了 Python 2 的驱动程序。我尝试了以下方法,现在它可以工作了:

    pip3 install cassandra-driver
    

    Python 开发人员真的很难对付 Python 2 和 Python 3 如此不兼容的菜鸟。

    【讨论】:

      【解决方案3】:

      你需要安装python驱动。

      【讨论】:

      • 此页面在 Google 中的排名很高,但缺少特定命令的答案,甚至没有链接。 (pip、apt、tarball?)
      • sudo pip install cassandra-driver && sudo pip install cassandra-driver --upgrade && sudo apt-get install python2.7-dev && sudo pip install blist # 假设你在 Ubuntu 上。另请注意,Cassandra 驱动程序格局发生了变化,这意味着今天安装的最佳驱动程序可能在 2 年后不再是最佳驱动程序。
      • 我试图在上面运行脚本,但终端说找不到 pip 命令..!我是 python 新手。
      猜你喜欢
      • 2017-09-23
      • 1970-01-01
      • 2017-10-23
      • 2019-07-31
      • 2019-07-30
      • 2018-12-08
      • 2014-02-08
      • 2012-05-11
      • 2013-04-06
      相关资源
      最近更新 更多