-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Mongo * minor * minor
- Loading branch information
Showing
14 changed files
with
843 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import logging | ||
from datetime import datetime | ||
|
||
import pymongo | ||
import requests | ||
from airflow.decorators import dag | ||
|
||
from airflow.operators.python import PythonOperator | ||
from airflow.providers.mongo.hooks.mongo import MongoHook | ||
from pymongo import MongoClient, UpdateOne | ||
from pymongo.collection import Collection | ||
from pymongo.database import Database | ||
|
||
|
||
@dag( | ||
dag_id="artic", | ||
schedule=None, | ||
start_date=datetime(2024, 6, 21), | ||
catchup=False, | ||
) | ||
def artic() -> None: | ||
pass | ||
|
||
|
||
def get_art_data_and_write_to_mongo(): | ||
endpoint = "https://api.artic.edu/api/v1/artworks?limit=100" | ||
|
||
hook = MongoHook(mongo_conn_id="mongo_default") | ||
client: MongoClient = hook.get_conn() | ||
db: Database = client.get_database("artic") | ||
collection: Collection = db.get_collection("art") | ||
|
||
collection.create_index([("id", pymongo.ASCENDING)], unique=True) | ||
|
||
page = 1 | ||
pieces = 0 | ||
while True: | ||
logging.info(f"working on page {page}") | ||
resp = requests.get(endpoint, timeout=10) | ||
resp_json = resp.json() | ||
|
||
data = resp_json.get("data", []) | ||
if len(data) == 0: | ||
break | ||
|
||
pieces += len(data) | ||
|
||
# Upsert to mongo | ||
ops = [ | ||
UpdateOne({"id": piece["id"]}, {"$set": piece}, upsert=True) | ||
for piece in data | ||
] | ||
collection.bulk_write(ops) | ||
|
||
pagination = resp_json.get("pagination", {}) | ||
endpoint = pagination.get("next_url") | ||
|
||
if not endpoint: | ||
break | ||
|
||
page += 1 | ||
|
||
logging.info(f"Finished. Made {page} API calls. Upserted {pieces}.") | ||
|
||
|
||
dag = artic() | ||
|
||
get_art_data = PythonOperator( | ||
task_id="get_art_data", dag=dag, python_callable=get_art_data_and_write_to_mongo | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
-c https://raw.githubusercontent.com/apache/airflow/constraints-2.9.1/constraints-3.11.txt | ||
apache-airflow-providers-amazon==8.20.0 | ||
apache-airflow-providers-apache-spark==4.7.2 | ||
apache-airflow-providers-mongo==4.0.0 | ||
apache-airflow-providers-slack==8.6.2 | ||
delta-spark==3.2.0 | ||
deltalake==0.17.3 | ||
duckdb==0.10.2 | ||
polars==0.20.31 | ||
pyspark==3.5.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
FROM mongodb/mongodb-community-server:7.0.9-ubuntu2204 |
Oops, something went wrong.