Update rows in your table
Update data in your tables using Astra’s REST API.
After creating a table in your keyspace and adding rows, you can update data. Data can be replaced or partially updated. Two endpoints can be used, either the update rows endpoint or the replace rows endpoint.
In the REST API, adding new rows is initially done with a POST
command.
After a row is input, a PUT
command is used to replace the row data, or a
PATCH
command can be can be used to replace only some of the row data.
Prerequisites
Update data
Data changes, so often it is necessary to update an entire row.
To update a row, send a PUT
request to /api/rest/v2/keyspaces/{keyspace_name}/{table_name}/{path}
.
The {path}
is comprised of the primary key values.
In this example, the partition key is firstname
"Mookie" and the
clustering key is lastname
"Betts";
thus, we use /Mookie/Betts
as the {path}
in our request.
curl -s -L -X PUT 'https://$ASTRA_CLUSTER_ID-$ASTRA_REGION.apps.astra.datastax.com/api/rest/v2/keyspaces/users_keyspace/users/Mookie/Betts' \
-H "X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN" \
-H 'Content-Type: application/json' \
-d '{
"email": "mookie.betts.new-email@gmail.com",
"favorite_color": "red",
"favorite_books": [ "Native Son", "The Color Purple" ],
"top_three_tv_shows": [ "Supergirl", "The Magicians", "Agents of SHIELD" ],
"evaluations": [ {"key":"2020", "value":"good"}, {"key":"2019", "value":"okay"} ],
"current_country": [ "France", "2016-01-01", "2020-02-02" ],
"address": { "street": "500 Central Ave", "city": "Albuquerque", "state": "New Mexico", "zip": "54321" }
}'
{
"data": {
"email": "mookie.betts.new-email@gmail.com",
"favorite_color": "red",
"favorite_books": [
"Native Son",
"The Color Purple"
],
"top_three_tv_shows": [
"Supergirl",
"The Magicians",
"Agents of SHIELD"
],
"evaluations": [
{
"key": "2020",
"value": "good"
},
{
"key": "2019",
"value": "okay"
}
],
"current_country": [
"France",
"2016-01-01",
"2020-02-02"
],
"address": {
"street": "500 Central Ave",
"city": "Albuquerque",
"state": "New Mexico",
"zip": "54321"
}
}
}
Updates are upserts. If the row doesn’t exist, it will be created. If it does exist, it will be updated with the new row data. |
It is also possible to update only part of a row. To partially update, send
a PATCH
request to /api/rest/v2/keyspaces/{keyspace_name}/{table_name}/{path}
.
In this example, we realize we should not have changed the email address, and
we want to only change that one column:
curl -s -L -X PATCH 'https://$ASTRA_CLUSTER_ID-$ASTRA_REGION.apps.astra.datastax.com/api/rest/v2/keyspaces/users_keyspace/users/Mookie/Betts' \
-H "X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN" \
-H 'Content-Type: application/json' \
-d '{
"email": "mookie.betts.email@email.com"
}'
{"data":{"email":"mookie.betts.email@email.com"}}