Add rows to your table

Insert data into your tables using Astra’s REST API.

After creating a table in your keyspace, you can add rows of data. Use the Add rows endpoint to add rows to the table you created in the last step. Use the Add rows endpoint to add rows to the table you created in the last step.

Write data

First, let’s add some data to the users table that you created. Send a POST request to /api/rest/v2/keyspaces/{keyspace_name}/{table_name} to add data to the table. The column name/value pairs are passed in the JSON body. You must include the columns that you specified as the partition key and any clustering keys, if defined.

  • cURL command (/v2)

  • Result

curl -s --location --request POST 'http://$ASTRA_CLUSTER_ID-$ASTRA_REGION.apps.astra.datastax.com/api/rest/v2/keyspaces/users_keyspace/users' \
--header "X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN" \
--header 'Content-Type: application/json' \
--data '{
    "firstname": "Mookie",
    "lastname": "Betts",
    "email": "mookie.betts@gmail.com",
    "favorite_color": "blue"
}'
curl -s --location --request POST 'http://$ASTRA_CLUSTER_ID-$ASTRA_REGION.apps.astra.datastax.com/api/rest/v2/keyspaces/users_keyspace/users' \
--header "X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN" \
--header 'Content-Type: application/json' \
--data '{
    "firstname": "Janesha",
    "lastname": "Doesha",
    "email": "janesha.doesha@gmail.com",
    "favorite_color": "grey"
}'
{"firstname":"Mookie","lastname":"Betts"}
{"firstname":"Janesha","lastname":"Doesha"}

Notice that, unlike schema creation, data queries do not require tables in the URI, only the {tableName}.

Collections, tuples, and UDTs

Some data types require special handling to write the data. Examples of set, list, map, tuple, and udt are shown, using the column schema created earlier.

SET:

  • cURL command (/v2)

  • Result

curl -s -X PATCH http://$ASTRA_CLUSTER_ID-$ASTRA_REGION.apps.astra.datastax.com/api/rest/v2/keyspaces/users_keyspace/users/Janesha/Doesha \
-H  "accept: application/json" \
-H  "X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN" \
-H  "Content-Type: application/json" \
-d '{
  "favorite_books": [ "Emma", "The Color Purple" ]
}'
{
  "data": {
    "favorite_books": [
      "Emma",
      "The Color Purple"
    ]
  }
}

LIST:

  • cURL command (/v2)

  • Result

curl -s -X PATCH http://$ASTRA_CLUSTER_ID-$ASTRA_REGION.apps.astra.datastax.com/api/rest/v2/keyspaces/users_keyspace/users/Janesha/Doesha \
-H  "X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN" \
-H  "Content-Type: application/json" \
-H  "Accept: application/json" \
-d '{
  "top_three_tv_shows": [ "The Magicians", "The Librarians", "Agents of SHIELD" ]
}'
{
  "data": {
    "top_three_tv_shows": [
      "The Magicians",
      "The Librarians",
      "Agents of SHIELD"
    ]
  }
}

LIST of MAP:

  • cURL command (/v2)

  • Result

curl -s -L -X PATCH http://$ASTRA_CLUSTER_ID-$ASTRA_REGION.apps.astra.datastax.com/api/rest/v2/keyspaces/users_keyspace/users/Janesha/Doesha \
-H  "X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN" \
-H  "Content-Type: application/json" \
-H  "Accept: application/json" \
-d '{
  "evaluations": [ {"key":"2020", "value":"good"}, {"key":"2019", "value":"okay"} ]
}'
{
  "data": {
    "evaluations": [
      {
        "key": "2020",
        "value": "good"
      },
      {
        "key": "2019",
        "value": "okay"
      }
    ]
  }
}

Notice that in the list, there are two key:value pairs in curly brackets. Those pairs are each a map, and the list has two mapped items.

TUPLE:

  • cURL command (/v2)

  • Result

curl -s -X PATCH http://$ASTRA_CLUSTER_ID-$ASTRA_REGION.apps.astra.datastax.com/api/rest/v2/keyspaces/users_keyspace/users/Janesha/Doesha \
-H  "accept: application/json" \
-H  "X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN" \
-H  "Content-Type: application/json" \
-d '{
  "current_country": [ "France", "2016-01-01", "2020-02-02" ]
}'
{
  "data": {
    "current_country": [
      "France",
      "2016-01-01",
      "2020-02-02"
    ]
  }
}

A tuple looks like a set or list, but has a fixed number of items in the tuple grouping.

UDT:

  • cURL command (/v2)

  • Result

curl -s -X PATCH http://$ASTRA_CLUSTER_ID-$ASTRA_REGION.apps.astra.datastax.com/api/rest/v2/keyspaces/users_keyspace/users/Janesha/Doesha \
-H  "accept: application/json" \
-H  "X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN" \
-H  "Content-Type: application/json" \
-d '{
  "address": { "street": "1 Main St", "city": "San Francisco", "state": "California", "zip": "12345" }
}'
{
  "data": {
    "address": {
      "street": "1 Main St",
      "city": "San Francisco",
      "state": "California",
      "zip": "12345"
    }
  }
}

Inserting UDTs resembles a mix of the curly brackets of maps, and the fixed number of items of a tuple.