简体   繁体   中英

Copy row from Cassandra database and then insert it using Python

I'm using plugin DataStax Python Driver for Apache Cassandra.

I want to read 100 rows from database and then insert them again into database after changing one value. I do not want to miss previous records.

I know how to get my rows:

rows = session.execute('SELECT * FROM columnfamily LIMIT 100;')
for myrecord in rows:
    print(myrecord.timestamp)

I know how to insert new rows into database:

stmt = session.prepare('''
       INSERT INTO columnfamily (rowkey, qualifier, info, act_date, log_time)
       VALUES (, ?, ?, ?, ?)
      IF NOT EXISTS
      ''')
results = session.execute(stmt, [arg1, arg2, ...])

My problems are that:

  1. I do not know how to change only one value in a row.
  2. I don't know how to insert rows into database without using CQL. My columnfamily has more than 150 columns and writing all their names in query does not seem as a best idea.

To conclude: Is there a way to get rows, modify one value from every one of them and then insert this rows into database without using only CQL?

First, you need to select only needed columns from Cassandra - it will be faster to transfer the data. You need to include all columns of primary key + column that you want to change.

After you get the data, you can use UPDATE command to update only necessary column (example from documentation):

UPDATE cycling.cyclist_name
   SET comments ='='Rides hard, gets along with others, a real winner'
   WHERE id = fb372533-eb95-4bb4-8685-6ef61e994caa

You can also use prepared statement to make it more performant...

But be careful - the UPDATE & INSERT in CQL are really UPSERTs, so if you change columns that are part of primary key, then it will create new entry...

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