Materialized Views with cqlengine

54 views
Skip to first unread message

Syed Hussain

unread,
Mar 26, 2020, 1:11:18 PM3/26/20
to DataStax Python Driver for Apache Cassandra User Mailing List
Hey guys,
 I've been playing around with cqlegine, and learning the ins and outs of it. I realize there are some limitations to what you can do with cqlengine, and I ran into one such limitation with Materialized Views. As far as I can tell, they are not supported in cqlengine, but python cassandra driver does offer some level of support. https://docs.datastax.com/en/drivers/python/3.4/api/cassandra/metadata.html#cassandra.metadata.MaterializedViewMetadata
I'm trying to use as little cql commands in my code as possible, so I'd ultimately like to use cqlengine to work with Materialized Views. There was a similar question posted several years ago on this topic, but the results were a little cryptic to me, so I thought I'd ask again for some clarification for those who, like me, need it.

Basically, how can we use cqlengine to create materialized views, and make queries on those views?
I'm not entirely sure what was meant by changing the `__table_name__` attribute, or how to make a model class based on a materialized view created using session.execute. Any help would be appreciated. Thanks!

Alan Boudreault

unread,
Mar 27, 2020, 10:17:40 AM3/27/20
to DataStax Python Driver for Apache Cassandra User Mailing List
Sorry for the double post. It looks like google groups UI doesn´t sync well with emails.

Hi Syed,

Unfortunately, there is no support to create a materialized view with cqlengine. You have to create it using CQL:


You can also try to define a Model class that reflect your materialized view. That should at least allow you to query it. Example:

# create the MV
session.execute( 'CREATE MATERIALIZED VIEW ...')

class UserActivitiesByUsername(Model):
    __keyspace__ = 'test'
     __table_name__ =  'user_activities_by_username'
    username = Text(primary_key=True)
   # all other columns of your MV

Alan

Syed Hussain

unread,
Mar 27, 2020, 12:48:57 PM3/27/20
to DataStax Python Driver for Apache Cassandra User Mailing List
But how is the materialized view going to be associated with the new model that I would create (i.e. UserActivitiesByUsername in your example)? Would __table_name__ have to be the same name as the materialized view name? If data is updated in the materialized view, would the table that I'm creating to represent the Materialized view reflect those changes?

Alan Boudreault

unread,
Mar 27, 2020, 12:57:36 PM3/27/20
to DataStax Python Driver for Apache Cassandra User Mailing List
1- cqlengine is just generating the CQL for you. So yes, the __table_name__ should be the materialized view name.
2- About the data... it´s not the role of cqlengine. cqlengine will return what is returned by the server. So yes, it will be reflected if you do a new query.

Syed Hussain

unread,
Mar 27, 2020, 1:36:17 PM3/27/20
to DataStax Python Driver for Apache Cassandra User Mailing List
Alright that makes sense. Thanks so much for your help, I appreciate it!
Reply all
Reply to author
Forward
0 new messages