I've just created two Cassandra clusters through Docker. The docker exec -it name nodetool status
command tells that clusters created everything is ok:
Then I'll try to INSERT data to table from the Python driver.
Here is my code:
from cassandra.cluster import Cluster
from faker import Faker
import time
fake = Faker()
num = 0
cluster = Cluster(['172.17.0.2', '172.17.0.3'], port=9042)
session = cluster.connect()
session.execute('USE keys')
print("Start create data")
start = time.time()
for i in range(10000):
time.sleep(0.001)
num+=1
session.execute(
"""
INSERT INTO tt (id, title) VALUES (%(id)s, %(title)s)
""",
{'id': num, 'title': fake.address()}
)
end = time.time()
print("Total time: ", end - start)
And finally when I try to execute the code I gave the following error in the terminal:
Are you connecting from your host machine?
In this case, check that you exposed the container port 9042 to your localhost (since the docker containers run on a different network to your host).
for example with a single node:
docker run -p 9042:9042 cassandra:latest
Since you have two nodes which both use container port 9042 by default, you want to map the port 9042 for the second node to something else, like
docker run -p 9043:9042 cassandra:latest
Both ports are mapped to localhost, and it is enough to specify one node when you set up the cluster in python, you only need one node for the connection, the rest of the cluster is discovered upon successful connection:
cluster = Cluster(['127.0.0.1'], port=9042)