Rah*_*rma 9 python cassandra cassandra-2.0
我试图使用多线程Python程序将记录插入Cassandra.我在3台机器上同时运行这个程序.有一段时间记录被插入,但后来我得到了以下异常.我正在使用datastax提供的驱动程序.
cassandra.cluster.NoHostAvailable
我做了一些搜索并找到了(来源:https://datastax.github.io/python-driver/api/cassandra/cluster.html)
exception cassandra.cluster.NoHostAvailable
在尝试操作但所有连接繁忙,失效,关闭或在使用时导致错误时触发.
我的问题是:
1.这是一个正常的例外,可能面临与Cassandra太多连接.
2.如果我想创建与cassandra的许多连接/会话,我将如何解决这个问题.(我知道创建太多会话是不可取的,它会影响服务器性能,因为每个会话消耗少量内存)
下面是代码片段.
cluster = Cluster(['192.168.1.21'])
session = cluster.connect('myNameSpace')
def insertInToCassandra(catRange):
for x in catRange:
//function to insert records into Cassandra table
ProductRange = [
range(900,920),
range(921,940),
range(941,960),
range(961,980),
range(981,1000)
]
# Make the Pool of workers
pool = ThreadPool(20)
# Open the urls in their own threads
# and return the results
results = pool.map(insertInToCassandra, ProductRange)
#close the pool and wait for the work to finish
pool.close()
pool.join()
Run Code Online (Sandbox Code Playgroud)
如果一个或多个cassandra节点不可用,这可能会发生正常异常,特别是如果它进入GC旋转或以其他方式崩溃.
根据您的复制因子(RF)和一致性级别(CL),单个节点脱机可能会或可能不会中断应用程序(RF为3和CL的仲裁,任何单个节点都应该没有问题).
您应该检查cassandra集群的运行状况nodetool status,并检查/var/log/cassandra/system.log节点是否向上/向下拍打的迹象.