0
votes

Datastax Cassandra Driver (Python): Is there a way to generate CQL files from the data models we create? As an example if I have a data model:

class ExampleModel(Model):
example_id      = columns.UUID(primary_key=True, default=uuid.uuid4)
example_type    = columns.Integer(index=True)
created_at      = columns.DateTime()
description     = columns.Text(required=False)

I know I can create table in Cassandra using the sync_table but my aim here is to derive the equivalent CQL statements for the create table statement generated from the model above. Is there a way?

1
You can create the table and then with cqlsh you can describe yourtable; to get the corresponding CREATE TABLE... statements.xmas79
@xmas79 Thanks for your help. I must admit this is what also crossed my mind before I posted the question. I am looking for a way to obtain this without leaving the programming environment in Python. One solution is: issue a desc table from python, is there a better way?Segmented

1 Answers

1
votes

If you are okay creating the table, the most robust way would be to sync the table, then use the driver's metadata API to reproduce the string.

Cluster.metadata.keyspaces[ks].tables[t] and TableMetadata.export_as_string

This is the exact code that generates the desc output for cqlsh.

Alternatively, you can avoid creating the table by using the cqlengine management function that generates CQL from these models:

cassandra.cqlengine.management._get_create_table

Note that this is not part of the API, and very likely to change as it is replaced with the core driver metadata equivalent.