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

瀏覽次數:508 次
跳到第一則未讀訊息

凸鎚大法師

未讀,
2018年3月26日 清晨6:16:162018/3/26
收件者: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

未讀,
2018年3月26日 清晨7:56:202018/3/26
收件者: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

凸鎚大法師

未讀,
2018年3月27日 凌晨2:28:542018/3/27
收件者:DataStax Python Driver for Apache Cassandra User Mailing List

you are right , thank your help

Sovanna

未讀,
2018年3月27日 下午5:22:252018/3/27
收件者: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.

回覆所有人
回覆作者
轉寄
0 則新訊息