How to query for a set type cassandra column using python?

505 views
Skip to first unread message

凸鎚大法師

unread,
Mar 26, 2018, 6:16:16 AM3/26/18
to DataStax Python Driver for Apache Cassandra User Mailing List
How to query for a set type cassandra column using python?
My cassandra table has a column of type legs set<int>.

ex:
CREATE TABLE ptt_spider.useridindex (
    userid text PRIMARY KEY,
    cd_idx set<int>,
    pd_idx set<int>,
    udindex set<int>

I am using following python code for query data from my cassandra database:

from cassandra.cluster import Cluster
from cassandra.cqlengine import columns
from cassandra.cqlengine.models import Model
from cassandra.cqlengine import connection


class UserIdIndex(Model):
    __table_name__  = 'useridindex'
    __keyspace__ = 'TestDB'
    __connection__ = 'cluster2'
    userid = columns.Text(primary_key=True)
    UDIndex = columns.Set(columns.UUID())
    pd_Idx = columns.Set(columns.Integer())
    cd_Idx = columns.Set(columns.Integer())
    
def cassandraTest():
    cluster = Cluster(contact_points=['localhost'],port=9042)
    session = cluster.connect('TestDB')
 
    connection.register_connection('cluster2', session=session)
    
    querySet = UserIdIndex.objects.filter(userid='a5981826')
    user = querySet.get()
    for item in user.UDIndex:
        print(item)

        
if __name__ == '__main__':
    cassandraTest()



When I pass a query for selecting the columns of type text, it works fine but if I try to select a set type column, it throws an error:

# ERROR :
# Traceback (most recent call last):
#   File "/home/jack/PythonProject/main/KeywordSearch/test.py", line 114, in <module>
#     cassandraTest()
#   File "/home/jack/PythonProject/main/KeywordSearch/test.py", line 106, in cassandraTest
#     for item in user.UDIndex:
# TypeError: 'type' object is not iterable

please help me , thank you!

jaume.m...@datastax.com

unread,
Mar 26, 2018, 7:56:20 AM3/26/18
to DataStax Python Driver for Apache Cassandra User Mailing List
Hello,

The CREATE query you have posted doesn't correspond to the table in the script, that may be the reason, but this script works for me:

from cassandra.cluster import Cluster
from cassandra.cqlengine import columns
from cassandra.cqlengine.models import Model
from cassandra.cqlengine import connection
from cassandra.cqlengine.management import sync_table, create_keyspace_simple

from uuid import uuid4
class UserIdIndex(Model):
    __table_name__
= 'useridindex'
    __keyspace__ = 'testdb'
    __connection__ = 'cluster2'
    userid = columns.Text(primary_key=True)

    udindex
= columns.Set(columns.UUID())
    pd_idx
= columns.Set(columns.Integer())
    cd_idx
= columns.Set(columns.Integer())



def cassandraTest():
    cluster
= Cluster(contact_points=['localhost'], port=9042)

    session
= cluster.connect()


    connection
.register_connection('cluster2', session=session)
    create_keyspace_simple
('testdb', 1, connections=['cluster2'])
    sync_table
(UserIdIndex)

   
UserIdIndex.create(userid='a5981826', pd_idx={1, 2, 3}, cd_idx={6, 7, 8}, udindex={uuid4(), uuid4(), uuid4()})


    querySet
= UserIdIndex.objects.filter(userid='a5981826')
    user
= querySet.get()

   
for item in user.udindex:

       
print(item)


if __name__ == '__main__':
    cassandraTest
()

Enter code here...

Jaume

凸鎚大法師

unread,
Mar 27, 2018, 2:28:54 AM3/27/18
to DataStax Python Driver for Apache Cassandra User Mailing List

you are right , thank your help

Sovanna

unread,
Mar 27, 2018, 5:22:25 PM3/27/18
to python-dr...@lists.datastax.com
I've updated your code on http://collabedit.com/xkt2h

--
Sovanna HING

"Love people and use things" - Minimalism

On Mon, Mar 26, 2018 at 11:52 AM, 凸鎚大法師 <arlia...@gmail.com> wrote:
How to query for a set type cassandra column using python?
My cassandra table has a column of type legs set<int>.

ex:
CREATE TABLE ptt_spider.useridindex (
    userid text PRIMARY KEY,
    cd_idx set<int>,
    pd_idx set<int>,
    udindex set<int>
)

I am using following python code for query data from my cassandra database:



When I pass a query for selecting the columns of type text, it works fine but if I try to select a set type column, it throws an error:

# ERROR :
# Traceback (most recent call last):
#   File "/home/jack/PythonProject/main/KeywordSearch/test.py", line 114, in <module>
#     cassandraTest()
#   File "/home/jack/PythonProject/main/KeywordSearch/test.py", line 106, in cassandraTest
#     for item in user.UDIndex:
# TypeError: 'type' object is not iterable         

please help me , thank you!

--
You received this message because you are subscribed to the Google Groups "DataStax Python Driver for Apache Cassandra User Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-driver-user+unsub...@lists.datastax.com.

Reply all
Reply to author
Forward
0 new messages