CKAN Data API

Zugriff auf Ressourcen per Web-Schnittstelle mit umfangreichen Suchmöglichkeiten. Weitere Information gibt es in der CKAN Data API und DataStore Dokumentation.

Code Beispiele

5 Ergebnisse ermitteln, die "jones" in irgendeinem Feld enthalten:
curl https://ckan-20395.mylab.th-luebeck.dev/de/api/action/datastore_search \
  -H"Authorization:$API_TOKEN" -d '
{
  "resource_id": "9bba828b-d24b-4d84-8a8f-4893c282e59e",
  "limit": 5,
  "q": "jones"
}'
const resp = await fetch(`https://ckan-20395.mylab.th-luebeck.dev/de/api/action/datastore_search`, {
    method: 'POST',
    headers: {
        'content-type': 'application/json',
        authorization: API_TOKEN
    },
    body: JSON.stringify({
        resource_id: '9bba828b-d24b-4d84-8a8f-4893c282e59e',
        limit: 5,
        q: 'jones'
    })
})
await resp.json()
$json = @'
{
  "resource_id": "9bba828b-d24b-4d84-8a8f-4893c282e59e",
  "limit": 5,
  "q": "jones"
}
'@
$response = Invoke-RestMethod https://ckan-20395.mylab.th-luebeck.dev/de/api/action/datastore_search`
  -Method Post -Body $json -Headers @{"Authorization"="$API_TOKEN"}
$response.result.records

(es wird die ckanapi Client-Bibliothek genutzt)

from ckanapi import RemoteCKAN

rc = RemoteCKAN('https://ckan-20395.mylab.th-luebeck.dev/de/', apikey=API_TOKEN)
result = rc.action.datastore_search(
    resource_id="9bba828b-d24b-4d84-8a8f-4893c282e59e",
    limit=5,
    q="jones",
)
print(result['records'])
library(httr2)

req <- request("https://ckan-20395.mylab.th-luebeck.dev/de/api/action/datastore_search")
result <- req %>% 
    req_headers(Authorization = API_TOKEN) %>% 
    req_body_json(list(
        resource_id = '9bba828b-d24b-4d84-8a8f-4893c282e59e',
        limit = 5,
        q = 'jones'))
    req_perform %>% 
    resp_body_json
Ergebnisse, gefiltert nach den Inhalten einiger Felder
curl https://ckan-20395.mylab.th-luebeck.dev/de/api/action/datastore_search \
-H"Authorization:$API_TOKEN" -d '
{
  "resource_id": "9bba828b-d24b-4d84-8a8f-4893c282e59e",
  "filters": {
    "SATZ": "20",
    "zeile": "1"
  }
}'
const resp = await fetch(`https://ckan-20395.mylab.th-luebeck.dev/de/api/action/datastore_search`, {
    method: 'POST',
    headers: {
        'content-type': 'application/json',
        authorization: API_TOKEN
    },
    body: JSON.stringify({
        resource_id: '9bba828b-d24b-4d84-8a8f-4893c282e59e',
        filters: {
            zeile: "1",
            SATZ: "20"
        }
    })
await resp.json()
$json = @'
{
  "resource_id": "9bba828b-d24b-4d84-8a8f-4893c282e59e",
  "filters": {
    "SATZ": "20",
    "zeile": "1"
  }
}
'@
$response = Invoke-RestMethod https://ckan-20395.mylab.th-luebeck.dev/de/api/action/datastore_search`
  -Method Post -Body $json -Headers @{"Authorization"="$API_TOKEN"}
$response.result.records
from ckanapi import RemoteCKAN

ck = RemoteCKAN('https://ckan-20395.mylab.th-luebeck.dev/de/', apikey=API_TOKEN)
result = ck.action.datastore_search(
    resource_id="9bba828b-d24b-4d84-8a8f-4893c282e59e",
    filters={
        "SATZ": "20",
        "zeile": "1"
    },
)
print(result['records'])
library(httr2)

req <- request("https://ckan-20395.mylab.th-luebeck.dev/de/api/action/datastore_search")
result <- req %>% 
    req_headers(Authorization = API_TOKEN) %>% 
    req_body_json(list(
        resource_id = '9bba828b-d24b-4d84-8a8f-4893c282e59e', 
        filters = list(
            zeile = "1",
            SATZ = "20")))
    req_perform %>% 
    resp_body_json

Einen neue Datenbankeintrag einfügen:
curl https://ckan-20395.mylab.th-luebeck.dev/de/api/action/datastore_upsert \
-H"Authorization:$API_TOKEN" -d '
{
  "resource_id": "9bba828b-d24b-4d84-8a8f-4893c282e59e",
  "method": "insert",
  "records: [
    {
      "Cl in mg/l": "37.0",
      "Coli in /100": "35",
      "Datum": "15.10.91",
      "Farbe": "35",
      "Fe in mg/l": "0.03",
      "GH in degdH": "12.1",
      "GKZ in /ml": "50",
      "IDENT": "StWL",
      "KMnO4-Y": "17.6",
      "LF in uS/cm": "480",
      "Mn in mg/l": "0.01",
      "NH4 in mg/l": "0.10",
      "NO2 in mg/l": "0.07",
      "NO3 in mg/l": "0.70",
      "O2 in mg/l": "8.7",
      "PO4 in mg/l": "0.15",
      "SATZ": "20",
      "SO4 in mg/l": "54.9",
      "Temp. in C": "13.6",
      "pH-Wert": "7.80",
      "zeile": "1"
    }
  ]
}'
const resp = await fetch(`https://ckan-20395.mylab.th-luebeck.dev/de/api/action/datastore_upsert`, {
    method: 'POST',
    headers: {
        'content-type': 'application/json',
        authorization: API_TOKEN
    },
    body: JSON.stringify({
        resource_id: '9bba828b-d24b-4d84-8a8f-4893c282e59e',
        method: "insert",
        records: [{
            zeile: "1",
            SATZ: "20",
            IDENT: "StWL",
            Datum: "15.10.91",
            GH in degdH: "12.1",
            GKZ in /ml: "50",
            Cl in mg/l: "37.0",
            Coli in /100: "35",
            SO4 in mg/l: "54.9",
            Temp. in C: "13.6",
            NO3 in mg/l: "0.70",
            O2 in mg/l: "8.7",
            NO2 in mg/l: "0.07",
            KMnO4-Y: "17.6",
            NH4 in mg/l: "0.10",
            Farbe: "35",
            PO4 in mg/l: "0.15",
            pH-Wert: "7.80",
            Fe in mg/l: "0.03",
            LF in uS/cm: "480",
            Mn in mg/l: "0.01"
        }]
    })
await resp.json()
$json = @'
{
  "resource_id": "9bba828b-d24b-4d84-8a8f-4893c282e59e",
  "method": "insert",
  "records": [
    {
      "Cl in mg/l": "37.0",
      "Coli in /100": "35",
      "Datum": "15.10.91",
      "Farbe": "35",
      "Fe in mg/l": "0.03",
      "GH in degdH": "12.1",
      "GKZ in /ml": "50",
      "IDENT": "StWL",
      "KMnO4-Y": "17.6",
      "LF in uS/cm": "480",
      "Mn in mg/l": "0.01",
      "NH4 in mg/l": "0.10",
      "NO2 in mg/l": "0.07",
      "NO3 in mg/l": "0.70",
      "O2 in mg/l": "8.7",
      "PO4 in mg/l": "0.15",
      "SATZ": "20",
      "SO4 in mg/l": "54.9",
      "Temp. in C": "13.6",
      "pH-Wert": "7.80",
      "zeile": "1"
    }
  ]
}
'@
$response = Invoke-RestMethod https://ckan-20395.mylab.th-luebeck.dev/de/api/action/datastore_upsert`
  -Method Post -Body $json -Headers @{"Authorization"="$API_TOKEN"}
$response.result.records
from ckanapi import RemoteCKAN

ck = RemoteCKAN('https://ckan-20395.mylab.th-luebeck.dev/de/', apikey=API_TOKEN)
result = ck.action.datastore_upsert(
    resource_id="9bba828b-d24b-4d84-8a8f-4893c282e59e",
    method="insert",
    records=[{
        "Cl in mg/l": "37.0",
        "Coli in /100": "35",
        "Datum": "15.10.91",
        "Farbe": "35",
        "Fe in mg/l": "0.03",
        "GH in degdH": "12.1",
        "GKZ in /ml": "50",
        "IDENT": "StWL",
        "KMnO4-Y": "17.6",
        "LF in uS/cm": "480",
        "Mn in mg/l": "0.01",
        "NH4 in mg/l": "0.10",
        "NO2 in mg/l": "0.07",
        "NO3 in mg/l": "0.70",
        "O2 in mg/l": "8.7",
        "PO4 in mg/l": "0.15",
        "SATZ": "20",
        "SO4 in mg/l": "54.9",
        "Temp. in C": "13.6",
        "pH-Wert": "7.80",
        "zeile": "1"
    }]
)
library(httr2)

req <- request("https://ckan-20395.mylab.th-luebeck.dev/de/api/action/datastore_upsert")
result <- req %>% 
    req_headers(Authorization = API_TOKEN) %>% 
    req_body_json(list(
        resource_id = '9bba828b-d24b-4d84-8a8f-4893c282e59e', 
        method = 'insert',
        records = list(list(
            zeile = "1",
            SATZ = "20",
            IDENT = "StWL",
            Datum = "15.10.91",
            GH in degdH = "12.1",
            GKZ in /ml = "50",
            Cl in mg/l = "37.0",
            Coli in /100 = "35",
            SO4 in mg/l = "54.9",
            Temp. in C = "13.6",
            NO3 in mg/l = "0.70",
            O2 in mg/l = "8.7",
            NO2 in mg/l = "0.07",
            KMnO4-Y = "17.6",
            NH4 in mg/l = "0.10",
            Farbe = "35",
            PO4 in mg/l = "0.15",
            pH-Wert = "7.80",
            Fe in mg/l = "0.03",
            LF in uS/cm = "480",
            Mn in mg/l = "0.01"))))
    req_perform %>% 
    resp_body_json
Eine Datenbankeintrag aktualisieren:
curl https://ckan-20395.mylab.th-luebeck.dev/de/api/action/datastore_upsert \
-H"Authorization:$API_TOKEN" -d '
{
  "resource_id": "9bba828b-d24b-4d84-8a8f-4893c282e59e",
  "method": "update",
  "records: [
    {
      "Cl in mg/l": "37.0",
      "Coli in /100": "35",
      "Datum": "15.10.91",
      "Farbe": "35",
      "Fe in mg/l": "0.03",
      "GH in degdH": "12.1",
      "GKZ in /ml": "50",
      "IDENT": "StWL",
      "KMnO4-Y": "17.6",
      "LF in uS/cm": "480",
      "Mn in mg/l": "0.01",
      "NH4 in mg/l": "0.10",
      "NO2 in mg/l": "0.07",
      "NO3 in mg/l": "0.70",
      "O2 in mg/l": "8.7",
      "PO4 in mg/l": "0.15",
      "SATZ": "20",
      "SO4 in mg/l": "54.9",
      "Temp. in C": "13.6",
      "_id": 1,
      "pH-Wert": "7.80",
      "zeile": "1"
    }
  ]
}'
const resp = await fetch(`https://ckan-20395.mylab.th-luebeck.dev/de/api/action/datastore_upsert`, {
    method: 'POST',
    headers: {
        'content-type': 'application/json',
        authorization: API_TOKEN
    },
    body: JSON.stringify({
        resource_id: '9bba828b-d24b-4d84-8a8f-4893c282e59e',
        method: "update",
        records: [{
            _id: 1,
            zeile: "1",
            SATZ: "20",
            IDENT: "StWL",
            Datum: "15.10.91",
            GH in degdH: "12.1",
            GKZ in /ml: "50",
            Cl in mg/l: "37.0",
            Coli in /100: "35",
            SO4 in mg/l: "54.9",
            Temp. in C: "13.6",
            NO3 in mg/l: "0.70",
            O2 in mg/l: "8.7",
            NO2 in mg/l: "0.07",
            KMnO4-Y: "17.6",
            NH4 in mg/l: "0.10",
            Farbe: "35",
            PO4 in mg/l: "0.15",
            pH-Wert: "7.80",
            Fe in mg/l: "0.03",
            LF in uS/cm: "480",
            Mn in mg/l: "0.01"
        }]
    })
await resp.json()
$json = @'
{
  "resource_id": "9bba828b-d24b-4d84-8a8f-4893c282e59e",
  "method": "update",
  "records": [
    {
      "Cl in mg/l": "37.0",
      "Coli in /100": "35",
      "Datum": "15.10.91",
      "Farbe": "35",
      "Fe in mg/l": "0.03",
      "GH in degdH": "12.1",
      "GKZ in /ml": "50",
      "IDENT": "StWL",
      "KMnO4-Y": "17.6",
      "LF in uS/cm": "480",
      "Mn in mg/l": "0.01",
      "NH4 in mg/l": "0.10",
      "NO2 in mg/l": "0.07",
      "NO3 in mg/l": "0.70",
      "O2 in mg/l": "8.7",
      "PO4 in mg/l": "0.15",
      "SATZ": "20",
      "SO4 in mg/l": "54.9",
      "Temp. in C": "13.6",
      "_id": 1,
      "pH-Wert": "7.80",
      "zeile": "1"
    }
  ]
}
'@
$response = Invoke-RestMethod https://ckan-20395.mylab.th-luebeck.dev/de/api/action/datastore_upsert`
  -Method Post -Body $json -Headers @{"Authorization"="$API_TOKEN"}
$response.result.records
from ckanapi import RemoteCKAN

ck = RemoteCKAN('https://ckan-20395.mylab.th-luebeck.dev/de/', apikey=API_TOKEN)
result = ck.action.datastore_upsert(
    resource_id="9bba828b-d24b-4d84-8a8f-4893c282e59e",
    method="update",
    records=[{
        "Cl in mg/l": "37.0",
        "Coli in /100": "35",
        "Datum": "15.10.91",
        "Farbe": "35",
        "Fe in mg/l": "0.03",
        "GH in degdH": "12.1",
        "GKZ in /ml": "50",
        "IDENT": "StWL",
        "KMnO4-Y": "17.6",
        "LF in uS/cm": "480",
        "Mn in mg/l": "0.01",
        "NH4 in mg/l": "0.10",
        "NO2 in mg/l": "0.07",
        "NO3 in mg/l": "0.70",
        "O2 in mg/l": "8.7",
        "PO4 in mg/l": "0.15",
        "SATZ": "20",
        "SO4 in mg/l": "54.9",
        "Temp. in C": "13.6",
        "_id": 1,
        "pH-Wert": "7.80",
        "zeile": "1"
    }]
)
library(httr2)

req <- request("https://ckan-20395.mylab.th-luebeck.dev/de/api/action/datastore_upsert")
result <- req %>% 
    req_headers(Authorization = API_TOKEN) %>% 
    req_body_json(list(
        resource_id = '9bba828b-d24b-4d84-8a8f-4893c282e59e', 
        method = 'update',
        records = list(list(
            _id = 1,
            zeile = "1",
            SATZ = "20",
            IDENT = "StWL",
            Datum = "15.10.91",
            GH in degdH = "12.1",
            GKZ in /ml = "50",
            Cl in mg/l = "37.0",
            Coli in /100 = "35",
            SO4 in mg/l = "54.9",
            Temp. in C = "13.6",
            NO3 in mg/l = "0.70",
            O2 in mg/l = "8.7",
            NO2 in mg/l = "0.07",
            KMnO4-Y = "17.6",
            NH4 in mg/l = "0.10",
            Farbe = "35",
            PO4 in mg/l = "0.15",
            pH-Wert = "7.80",
            Fe in mg/l = "0.03",
            LF in uS/cm = "480",
            Mn in mg/l = "0.01"))))
    req_perform %>% 
    resp_body_json
"method" hat als Standard "upsert", das bedeutet, Zeilen werden eingefügt oder aktualisiert gemäß der übergebenen Primärschlüssel.

Eine Datenbankeintrag löschen:
curl https://ckan-20395.mylab.th-luebeck.dev/de/api/action/datastore_records_delete \
-H"Authorization:$API_TOKEN" -d '
{
  "resource_id": "9bba828b-d24b-4d84-8a8f-4893c282e59e",
  "filters": {
    "_id": 1
  }
}'
const resp = await fetch(`https://ckan-20395.mylab.th-luebeck.dev/de/api/action/datastore_records_delete`, {
    method: 'POST',
    headers: {
        'content-type': 'application/json',
        authorization: API_TOKEN
    },
    body: JSON.stringify({
        resource_id: '9bba828b-d24b-4d84-8a8f-4893c282e59e',
        filters: {
            _id: 1
        }
    })
await resp.json()
$json = @'
{
  "resource_id": "9bba828b-d24b-4d84-8a8f-4893c282e59e",
  "filters": {
    "_id": 1
  }
}
'@
$response = Invoke-RestMethod https://ckan-20395.mylab.th-luebeck.dev/de/api/action/datastore_records_delete`
  -Method Post -Body $json -Headers @{"Authorization"="$API_TOKEN"}
$response.result.records
from ckanapi import RemoteCKAN

ck = RemoteCKAN('https://ckan-20395.mylab.th-luebeck.dev/de/', apikey=API_TOKEN)
result = ck.action.datastore_records_delete(
    resource_id="9bba828b-d24b-4d84-8a8f-4893c282e59e",
    filters={
        "_id": 1
    }
)
library(httr2)

req <- request("https://ckan-20395.mylab.th-luebeck.dev/de/api/action/datastore_records_delete")
result <- req %>% 
    req_headers(Authorization = API_TOKEN) %>% 
    req_body_json(list(
        resource_id = '9bba828b-d24b-4d84-8a8f-4893c282e59e', 
        filters = list(
            _id = 1)))
    req_perform %>% 
    resp_body_json

Einige API Endpunkte können mit einem GET Befehl angefragt werden.

Abfragebeispiel (erste 5 Ergebnisse)

https://ckan-20395.mylab.th-luebeck.dev/de/api/action/datastore_search?resource_id=9bba828b-d24b-4d84-8a8f-4893c282e59e&limit=5

Abfragebeispiel (Ergebnisse die 'jones' enthalten)

https://ckan-20395.mylab.th-luebeck.dev/de/api/action/datastore_search?resource_id=9bba828b-d24b-4d84-8a8f-4893c282e59e&q=jones