A command line interface and Python module for accessing the CKAN Action API
tested under Python 2.7, 3.6 and pypy
pip install ckanapi
The ckanapi command line interface lets you access local and remote CKAN instances for bulk operations and simple API actions.
Simple actions with string parameters may be called directly. The response is pretty-printed to STDOUT. e.g.:
$ ckanapi action group_list -r https://demo.ckan.org
[
"data-expolorer",
"example-group",
"geo-examples",
...
]
Use -r to specify the remote CKAN instance, and -a to provide an API KEY. Remote actions connect as an anonymous user by default.
Local CKAN actions may be run by specifying the config file with -c. If no remote server or config file is specified the CLI will look for a development.ini file in the current directory, much like paster commands.
Local CKAN actions are performed by the site user (default system administrator) when -u is not specified.
To perform local actions with a less privileged user use the -u option with a user name or a name that doesn't exist. This is useful if you don't want things like deleted datasets or private information to be returned.
Simple action arguments may be passed in KEY=STRING form for string values or in KEY:JSON form for JSON values.
E.g. to view a dataset using a KEY=STRING parameter:
$ ckanapi action package_show id=my-dataset-name
{
"name": "my-dataset-name",
...
}
E.g. to get the number of datasets for each organization using KEY:JSON parameters:
$ ckanapi action package_search 'facet.field:["organization"]' rows:0
{
"facets": {
"organization": {
"org1": 42,
"org2": 21,
...
}
},
...
}
Files may be passed for upload using the KEY@FILE form.
E.g. create a resource with a file attached
$ ckanapi action resource_create package_id=my-dataset-with-files \
upload@/path/to/file/to/upload.csv \
url=dummy-value # ignored but required by CKAN<2.6
Datasets, groups, organizations, users and related items may be dumped to JSON lines text files and created or updated from JSON lines text files.
E.g. dumping datasets from CKAN into a local file with 4 processes:
$ ckanapi dump datasets --all -O datasets.jsonl.gz -z -p 4 -r https://localhost
E.g. load datasets from a dataset dump file with 3 processes in parallel:
$ ckanapi load datasets -I datasets.jsonl.gz -z -p 3 -c /etc/ckan/production.ini
These bulk dumping and loading jobs can be run in parallel with multiple worker processes. The jobs in progress, the rate of job completion and any individual errors are shown on STDERR while the jobs run.
Bulk loading jobs may be resumed from the last completed record or split across multiple servers by specifying r