0

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?

Jo.Hen
  • 55
  • 6

1 Answers1

0

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...

Alex Ott
  • 80,552
  • 8
  • 87
  • 132