In previous section we learn creating user, keyspaces and managed table. Now we will learning how to insert, update, and deleting data in cassandra using cassandra.Operation DML ( Data Manipulation Language) similar to RDMS (Relational Database Management System). Using Keyword INSERT INTO for inserting data, keyword UPDATE table_name SET columns WHERE conditional for updating data, keyword DELETE FROM table_name WHERE conditional for deleting data.

Insert Data

Example Syntax

example insert data

Insert Data With Options (TTL and TIMESTAMP)

  • TTL ( Time to Live) in seconds, data will deleted in seconds
  • TIMESTAMP
Insert Data Using TTL

TTL input is in seconds. TTL column values are automatically marked as deleted (with a tombstone) after the requested amount of time has expired. TTL marks the inserted values, not the column itself, for expiration. Any subsequent update of the column resets the TTL to the TTL specified in the update. By default, values never expire. You cannot set data in a counter column to expire

Insert Data Using spesific TIMESTAMP(writetime())

The TIMESTAMP input is in microseconds. If not specified, the time (in microseconds) that the write occurred to the column is used.
Example

Check the writetime

if we dont spesific TIMESTAMP cassandra will created it automaticly
example value 1515041320143727

Insert Data Collections

when we need insert data collection type, we using formating JSON
1. List
example [‘value’,’value’]
2. Map
example {‘key1′:’value’,’key2′:’value 2′}
3. Set
example {‘value’,’value2′}

Example code

Update Data

Update row in cassandra db using is a upsert operation, upsert is a change in the database that updates specified column in a row if the column exists or inserts the new column if it does not exists.

Synopsis syntax

Example

Update Collections type

  1. List
    set list_column=['data','data2','data3'] //replace datas
    set list_column=list_column+ ['data4'] // add new data
    set list_column=list_column+ ['data4'] // delete in data in list
    set list_column[3] =['new data'] // replace data using index
  2. Map
    set map_column={'newKey':'newValue'} //replace data
    set map_column=map_column + {'newKey':'newValue'} // add new value
  3. Set
    set set_column ={} //remove all set datas
    set set_column={'newDataSet'} // replace set data
    set set_column=set_column + {'newData2'} // add new data in set
    set set_column=set_column - {'newData2'} // remove spesific data

example code

Deleting Data

Synopsys syntax

example