user3159821 user3159821 - 3 months ago 14
Python Question

Python loop inserting last row only in cassandra

I typed a small demo loop in order to insert random values in Cassandra but only the last record is persisted into the database. I am using cassandra-driver from datastax and its object modeling lib. Cassandra version is 3.7 and Python 3.4. Any idea what I am doing wrong?

#!/usr/bin/env python

import datetime
import uuid
from random import randint, uniform
from cassandra.cluster import Cluster
from cassandra.cqlengine import connection, columns
from cassandra.cqlengine.management import sync_table
from cassandra.cqlengine.models import Model
from cassandra.cqlengine.query import BatchQuery

class TestTable(Model):
_table_name = 'test_table'
key = columns.UUID(primary_key=True, default=uuid.uuid4())
type = columns.Integer(index=True)
value = columns.Float(required=False)
created_time = columns.DateTime(default=datetime.datetime.now())


def main():
connection.setup(['127.0.0.1'], 'test', protocol_version = 3)
sync_table(TestTable)

for _ in range(10):
type = randint(1, 3)
value = uniform(-10, 10)
row = TestTable.create(type=type, value=value)
print("Inserted row: ", row.type, row.value)

print("Done inserting")

q = TestTable.objects.count()
print("We have inserted " + str(q) + " rows.")


if __name__ == "__main__":
main()


Many thanks!

Answer

The problem is in the definition of the key column:

key = columns.UUID(primary_key=True, default=uuid.uuid4())

For the default value it's going to call the uuid.uuid4 function once and use that result as the default for all future inserts. Because that's your primary key, all 10 writes will happen to the same primary key.

Instead, drop the parentheses so you are just passing a reference to uuid.uuid4 rather than calling it:

key = columns.UUID(primary_key=True, default=uuid.uuid4)

Now each time you create a row you'll get a new unique UUID value, and therefore a new row in Cassandra.

Comments