Write data
First, let’s add some data to the users
table that you created.
Send a POST
request to /v2/keyspaces/{keyspace_name}/{table_name}
to add data to the table.
The column name/value pairs are passed in the JSON body.
curl -s --location --request POST 'https://$ASTRA_CLUSTER_ID-$ASTRA_REGION.apps.astra.datastax.com/api/rest/v2/schemas/keyspaces/users_keyspace/users' \
--header "X-Cassandra-Token: $AUTH_TOKEN" \
--header 'Content-Type: application/json' \
--data '{
"firstname": "Mookie",
"lastname": "Betts",
"email": "mookie.betts@gmail.com",
"favorite color": "blue"
}'
curl -s --location --request POST 'https://$ASTRA_CLUSTER_ID-$ASTRA_REGION.apps.astra.datastax.com/api/rest/v2/schemas/keyspaces/users_keyspace/users' \
--header "X-Cassandra-Token: $AUTH_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 |
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 -s -X POST https://$ASTRA_CLUSTER_ID-$ASTRA_REGION.apps.astra.datastax.com/api/rest/v2/schemas/keyspaces/users_keyspace/users \
-H "accept: application/json" \
-H "X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN" \
-H "Content-Type: application/json" \
-d '{"firstname": "Janesha",
"lastname": "Doesha",
"favorite color": "grey",
"favorite_books": [ "Emma", "The Color Purple" ]
}'
{"firstname":"Janesha","lastname":"Doesha"}
LIST:
curl -s -X POST https://$ASTRA_CLUSTER_ID-$ASTRA_REGION.apps.astra.datastax.com/api/rest/v2/schemas/keyspaces/users_keyspace/users \
-H "X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN" \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-d '{"firstname": "Janesha",
"lastname": "Doesha",
"favorite color": "grey",
"top_three_tv_shows": [ "The Magicians", "The Librarians", "Agents of SHIELD" ]
}'
{"firstname":"Janesha","lastname":"Doesha"}
MAP:
curl -s -L -X POST https://$ASTRA_CLUSTER_ID-$ASTRA_REGION.apps.astra.datastax.com/api/rest/v2/keyspaces/users_keyspace/users \
-H "X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN" \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-d '{"firstname": "Janesha",
"lastname": "Doesha",
"favorite color": "grey",
"top_three_tv_shows": [ "The Magicians", "The Librarians", "Agents of SHIELD" ],
"evaluations": {"2020" : "good", "2019" : "okay"}
}'
{"firstname":"Janesha","lastname":"Doesha"}
TUPLE:
curl -s -X POST https://$ASTRA_CLUSTER_ID-$ASTRA_REGION.apps.astra.datastax.com/api/rest/v2/schemas/keyspaces/users_keyspace/users \
-H "accept: application/json" \
-H "X-Cassandra-Token: $AUTH_TOKEN" \
-H "Content-Type: application/json" \
-d '{"firstname": "Janesha",
"lastname": "Doesha",
"favorite color": "grey",
"current_country": [ "France", "2016-01-01", "2020-02-02" ]
}'
{"firstname":"Janesha","lastname":"Doesha"}
UDT:
curl -s -X POST https://$ASTRA_CLUSTER_ID-$ASTRA_REGION.apps.astra.datastax.com/api/rest/v2/schemas/keyspaces/users_keyspace/users \
-H "accept: application/json" \
-H "X-Cassandra-Token: $AUTH_TOKEN" \
-H "Content-Type: application/json" \
-d '{"firstname": "Janesha",
"lastname": "Doesha",
"favorite color": "grey",
"address": { "street": "1 Main St", "zip": "12345" }
}'
{"firstname":"Janesha","lastname":"Doesha"}