I’m trying to build a DWH to be usable by BI tools, and the idea is to collect data from OpenWeatherMap (OWM) API for current and forecast JSON responses. I’m also trying to get 950 month aggregated historical data and trying to consolidate both sets based on location (co-ordinates and city). What should be the best way to approach it?
Below is the approach I was thinking to take:
Query QWM API using coordinates from Historical Data → Push JSON docs to a db and join with Hist → Build fact/dimension tables based on measures (total rainfall, max/mean temps, etc.) for dims (per month, per day, per 3 hours).
What platform would be better in this case? Should I involve any Pub/Sub (Kinesis) model in this, or just a traditional RDS based ETL should suffice?I’m planning of writing any code in Python/SQL, what would be the ideal way to manage it? Should I go Airflow, or just stick with CRON (or batch) based polling? I wanted to build something that can start small, and can be scaled and maintained easily in the future.
AWS Athena. This is basically managed Presto. You put your JSON data in S3 and Athena let’s you query it directly. Pay-per-query.
AWS Redshift. This is basically the data warehouse version of Postgres. You still put your JSON data in S3, but then you copy it into Redshift. Pay-per-server.
AWS Redshift Spectrum. This is a hybrid of Redshift and Athena. Both pay-per-server (you need at least 1 Redshift node) and pay-per-query.
AWS RDS. Using an OLTP database to perform OLAP workloads. If you’re more comfortable with MySQL or regular Postgres this can get you started more quickly but it isn’t a good fit on its own.
AWS EMR. This is the AWS version of Apache Spark. Put your data in S3 (do you see a pattern yet?) and then write spark jobs to calculate your aggregates. If you need some sort of complex processing while calculating the aggregates that SQL can’t do alone, this is probably a good fit. That being said, SQL can do a lot of heavy lifting these days. Once you’ve calculated your aggregates you could dump them into an RDS instance for querying.
If this is a personal project, where cost is the primary driver, I’d go with Athena. It should be dead simple to set up. Create a script to hit the API, compress the data, and save the results to S3. Throw your dimensional tables into S3/Athena and then query away. Athena now supports Create Table As Select so you would only have to run your aggregate queries once and just save the results as a new table. Because it’s pay-per-query, you only pay for S3 storage cost when you’re not using it.
Airflow - I am a strong believer that Airflow is major overkill for anything but large projects with 5+ data sources. If you need scheduling, a simple server with cron works great, or lambdas triggered with a cloudwatch schedule.
Kinesis - You should consider Kinesis Data Firehose as a way to have AWS do all your ETL work for you. With this set up, your script would hit the API and push the JSON data to a Kinesis Firehose. The stream then buffers up batches of data, writes them to S3, and optionally copies the data into Redshift for you. Plain jane Kinesis isn’t appropriate here.
I’d recommend taking a look at Athena. The benefits are low cost, easy to get started, and it scales on demand. The gotchas are - you need to compress and partition your data, performance won’t be amazing if your JSON objects are really fat (5kb+), and converting to the alternate Athena formats like ORC or Parquet are a pain.
Thanks, but for now my dataset would be very limited (few hundred megs) so I was thinking of it would be premature using it this early on. I can see it being grown enough maybe in a few years but as of now, I don’t see it growing to even 1TB for at least 2-3 years.
At the risk of not answering the question, you may want to consider packaging your ETL jobs as Docker containers and running them with AWS Fargate. Unless they are very long-running/resource-intensive, it will keep your costs down relative to keeping cron:ing instances around.
AWS Athena rules for this kind of work, particularly now that it has CTAS support. Be advised however, that Athena has various warts, notably that it has ~600ms baseline latency, that it does not accept its own CSV output as input, that it has a limited set of data types, that some regions have top-of-the-hour issues, that it is hard to know what errors are transient.
Create a simple lambda that gets the data from the API and saves it to Kinesis stream
Create a schedule for the lambda appropriate for your needs
Firehose this stream to S3 bucket (this should make the data a bit more optimized for later use by Athena as well!)
Analyze your data with AWS Athena.
This all would take you like an hour to write and set up (if you have some experience you can have a cloud formation or CDK infra code as well in this time). Schedule for lambdas is trivial, lambda will be really cheep, easy to maintain and iterate. And if you ever decide that you need something more fancy it won’t be a problem since you have all your data on S3, you can do all the fancy stuff like EMR, Reshift and so on.
On top of this, after you get your data in S3 and Athena, transform it into parquet with snappy compression. Depending on your queries, you will scan less data and faster than just having the data in json.
Also consider this piece I wrote, on why data lakes / data warehouse are difficult, when structuring the solution.
Good point and thanks! Being new to Lambda is a bit of a challenge in itself, and the more I try to look for, the more I keep finding the API building use cases in docs. Would you have any suggestions (or any docs you may have come across) on how I should maybe translate a simple GET request for a PUT in Firehose? Below is some of my code I’m trying to port, and any pointers would be great:
import requests
import json
# Get weather from OWM and use args for correct type.
def get_weather(gtype, lat, lon, key):
if gtype == 'current':
apitype = "weather?"
elif gtype == 'forecast':
apitype = "forecast?"
else:
print("Undefined GET type: use 'current' or 'forecast'.")
try:
api = "http://api.openweathermap.org/data/2.5/" + apitype
PARAMS = {'lat': lat, 'lon': lon, 'appid': key}
except:
return 'Invalid GET request'
with requests.session() as s:
rc = s.get(url=api, params=PARAMS)
data = rc.json()
return data
# Write data to json files and would love to turn this into a PUT for Firehose
def write_to_current(location, gtype, lat, lon, key):
with open(location + '/current.json', 'w') as outfile:
json.dump(get_weather(gtype, lat, lon, key), outfile)
return 'Current write complete.'
Then you will need a bit of code to trigger this, possibly something that will generate the longitude and altitude if I understand your use case correctly.