简体   繁体   中英

How to fetch list datatype column value from cassandra in python

Can anyone help with query: how to fetch list datatype column value from cassandra in python

Sure; let's assume we're building a simple table to hold users' favorite movies:

CREATE TABLE fav_movies (
    name TEXT PRIMARY KEY,
    movies LIST<TEXT>);

Assuming that someone wanted to query my favorite movies, they start by defining my name as a local variable, creating a prepared statement, and then executing it.

name = "Aaron"
pStatement = session.prepare("SELECT movies FROM stackoverflow.fav_movies WHERE name=?");

rows = session.execute(pStatement,(name,)).one()

If there is a result set returned, the movies list could be set to a local variable and iterated through to show each value:

if rows:
    movies = rows.movies
    
    for movie in movies:
        print(movie)

Running this program would produce results similar to this:

$ python3 queryList.py pUlOXBMyAstraDBSecretOrUserameyPHxZpka 5kHKNIuMyAstraDBTokenOrPasswordQ684
Star Wars
Blade Runner
The Martian

The code use above can be found in my Git repo: https://github.com/aploetz/python_scripts/blob/master/queryList.py

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM