问题
NOTE: My original problem is to delete all the records except for the last N
ones, but guessing such a thing is impossible with CKAN API, I've come up with the following solution:
- To get the
total
ammount of records usingdatastore_search
. - To get the ID of the
first
record in the previous search. - To obtain the IDs of all the records from the
first
record ID tototal-N
.
So, my question is: how to delete all the identified IDs in a single query? I've tried the following:
$ curl -X POST "http://demo.ckan.org/api/3/action/datastore_delete" -d '{"filters":{"_id":1,"_id":2,"_id":3},"force":"true","resource_id":"a43bfb04-7a8b-4624-a06a-25f4165e5b2a"}' -H "Authorization: xxx"
{"help": "http://demo.ckan.org/api/3/action/help_show?name=datastore_delete", "success": true, "result": {"filters": {"_id": 3}, "resource_id": "a43bfb04-7a8b-4624-a06a-25f4165e5b2a"}}
But only the record regarding the last ID (3
) was removed.
Of course, an array did not work as well:
$ curl -X POST "http://demo.ckan.org/api/3/action/datastore_delete" -d '{"filters":[{"_id":1},{"_id":2},{"_id":3}],"force":"true","resource_id":"a43bfb04-7a8b-4624-a06a-25f4165e5b2a"}' -H "Authorization: xxx"
{"help": "http://demo.ckan.org/api/3/action/help_show?name=datastore_delete", "success": false, "error": {"__type": "Validation Error", "filters": ["filters must be either a dict or null."]}}
Any idea? Or should I perform a query for each record aimed to be deleted?
回答1:
Auto-answering. After trying several combinations, I've found this works:
$ curl -X POST "http://demo.ckan.org/api/3/action/datastore_delete" -d '{"filters":{"_id":[1,2]},"force":"true","resource_id":"a43bfb04-7a8b-4624-a06a-25f4165e5b2a"}' -H "Authorization: xxx"
I.e. a dictionary has to be sent with a single (key,value) pair, the _id
and an array of values for the _id
, respectively.
来源:https://stackoverflow.com/questions/41227226/ckan-delete-a-list-of-records-within-the-datastore-in-a-single-query