-
Notifications
You must be signed in to change notification settings - Fork 0
/
ovfiets.sql
61 lines (54 loc) · 1.99 KB
/
ovfiets.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
--version to parse directly from json
select * from
(
select
to_timestamp((row_to_json::jsonb->'extra'->>'fetchTime')::int) AT TIME ZONE 'Europe/Amsterdam' as fetch_time,
row_to_json::jsonb->'extra'->>'locationCode' as location_code,
(row_to_json::jsonb->'extra'->>'rentalBikes')::int as rental_bikes,
st_setsrid(st_point((row_to_json::jsonb->>'lng')::numeric,(row_to_json::jsonb->>'lat')::numeric),4326) as geom
from
(
select
row_to_json(jsonb_each(body::jsonb->'locaties'))::jsonb->'value' as row_to_json
from
(
select (get).* from http_client.get('http://fiets.openov.nl/locaties.json' )
)a
)b
)c
;
--a more robust version which uses json from raw table
--initial setup only:
create table ovfiets_raw(download_time timestamp, json_raw jsonb);
create table ovfiets_availability(fetch_time timestamp WITH TIME ZONE,location_code text,rental_bikes int,geom geometry(Point,4326));
-- fetch full json from api into raw table
insert into ovfiets_raw
select
now(), --current timestamp
body::jsonb
from
(select (get).*
from http_client.get('http://fiets.openov.nl/locaties.json' ))a
;
-- if you like a quick check: table ovfiets_raw;
-- parse bike availability and insert into availabilitytable
insert into ovfiets_availability
select * from
(
select
to_timestamp((row_to_json::jsonb->'extra'->>'fetchTime')::int) AT TIME ZONE 'Europe/Amsterdam' as fetch_time,
row_to_json::jsonb->'extra'->>'locationCode' as location_code,
(row_to_json::jsonb->'extra'->>'rentalBikes')::int as rental_bikes,
st_setsrid(st_point((row_to_json::jsonb->>'lng')::numeric,(row_to_json::jsonb->>'lat')::numeric),4326) as geom
from
(
select
row_to_json(jsonb_each(json_raw::jsonb->'locaties'))::jsonb->'value' as row_to_json
from
ovfiets_raw
)b
)c
where not exists (select 1 from ovfiets_availability o where c.fetch_time = o.fetch_time and c.location_code = o.location_code)
;
-- simple query to check the new data
select * from ovfiets_availability order by location_code, fetch_time desc;