Jobs: Loading Large Tables

Some tables from the database are quite large and the API provides them in a different way:

  1. The standard request is rejected with code 98

  2. However, setting the query parameter job=true starts a background task.

  3. The final table is then available under data/resultfile instead of data/tablefile.

``pystatis`` is handling all these details and the whole process for you!. Thus, you will only notice a larger runtime when requesting larger tables (currently about 1 minute of extra timeout).

[1]:
import logging

from pystatis import Table

logging.basicConfig(level=logging.INFO)

Let’s assume we want to download the table “AI001-2-5”. If the table is not cached yet, a warning message tells us that a background job has been started.

[2]:
t = Table(name="AI001-2-5")
t.get_data()
WARNING:pystatis.http_helper:Die Tabelle ist zu groß, um direkt abgerufen zu werden. Es wird eine Verarbeitung im Hintergrund gestartet.
INFO:pystatis.http_helper:Database selected: regio
INFO:pystatis.http_helper:Code 99: Der Bearbeitungsauftrag wurde erstellt. Die Tabelle kann in Kürze als Ergebnis mit folgendem Namen abgerufen werden: AI001-2-5_411043199
WARNING:pystatis.http_helper:Verarbeitung im Hintergrund erfolgreich gestartet. Job-ID: AI001-2-5_411043199.
INFO:pystatis.http_helper:Code 0: erfolgreich
INFO:pystatis.http_helper:Code 0: erfolgreich
INFO:pystatis.http_helper:Code 0: erfolgreich
INFO:pystatis.http_helper:Code 0: erfolgreich
INFO:pystatis.http_helper:Code 0: erfolgreich
INFO:pystatis.http_helper:Code 0: erfolgreich
INFO:pystatis.http_helper:Verarbeitung im Hintergrund abgeschlossen. Ergebnis kann jetzt abgerufen werden über /data/resultfile und Job-ID: AI001-2-5_411043199.
INFO:pystatis.cache:Data was successfully cached under /Users/miay/.pystatis/data/AI001-2-5/d973b28b323fa878b812/20250412.zip.
INFO:pystatis.http_helper:Database selected: regio
INFO:pystatis.http_helper:Code 0: erfolgreich

Requesting the same table again will provide the table instantly from the cache.

[3]:
t = Table(name="AI001-2-5")
t.get_data()
hit
INFO:pystatis.http_helper:Database selected: regio
INFO:pystatis.http_helper:Code 0: erfolgreich
[4]:
t.data.head()
[4]:
Jahr Amtlicher Gemeindeschlüssel (AGS)__Code Amtlicher Gemeindeschlüssel (AGS) Anteil Siedlungs- und Verkehrsfläche an Gesamtfläche__Prozent Anteil Sport-, Freizeit-, Erholungsfläche an Gesamtfläche__Prozent Anteil der Fläche für Landwirtschaft an Gesamtfläche__Prozent Anteil der Fläche für Siedlung an Gesamtfläche__Prozent Anteil der Fläche für Verkehr an Gesamtfläche__Prozent Anteil der Fläche für Wald an Gesamtfläche__Prozent Freiraumfläche je EW__qm Siedlungs- und Verkehrsfläche je EW__qm
0 2022 01001000 Flensburg, kreisfreie Stadt 53.5 4.4 22.1 39.8 13.8 6.6 284.1 327.9
1 2022 01002000 Kiel, kreisfreie Stadt, Landeshauptstadt 58.8 10.3 23.4 44.9 13.9 5.6 197.4 281.5
2 2022 01003000 Lübeck, kreisfreie Stadt, Hansestadt 37.9 6.2 30.2 28.9 9.2 14.7 607.5 371.9
3 2022 01004000 Neumünster, kreisfreie Stadt 51.8 5.5 37.7 41.7 10.1 4.3 434.2 467.1
4 2022 01051001 Albersdorf 19.7 1.4 62.7 14.0 5.7 14.7 3640.3 894.6
[7]:
t.data.shape
[7]:
(13564, 11)