-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathetl_postgres.py
33 lines (29 loc) · 1.05 KB
/
etl_postgres.py
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
from psycopg2 import connect
from extract.pokemons import PokeApiRequest
from extract.abilities import multiextract_pokeapi
from load import postgres_load
if __name__ == '__main__':
# extract 100 pokemons from 1 to 100
poke_responses = [
PokeApiRequest('pokemon', str(i + 1)).fetch_pokeapi() for i in range(10)
]
response_filter = multiextract_pokeapi(poke_responses)
# rename order to ordering
for data_dict in response_filter:
data_dict['ordering'] = data_dict.pop('order')
conn = connect(dbname="postgres", user="postgres", host="db", password="postgres")
with conn.cursor() as cur:
postgres_load.create_table(
cur,
name='pokemons',
cols=[
'pokeid serial PRIMARY KEY',
'name varchar NOT NULL',
'ordering integer NOT NULL',
],
)
postgres_load.insert_row_multiple_times(
cur, name='pokemons', cols=['name', 'ordering'], values=response_filter
)
conn.commit()
conn.close()