ML_DL/MUJAKJUNG (무작정 시리즈)

Elasticsearch, Airflow 활용하기 - 1. 환경설정

swwho 2025. 3. 3. 01:33
728x90
반응형

1. 가상환경 생성 및 라이브러리 설치 (conda activate 제발 먼저 하자)

  • Elasticsearch 로컬 다운로드
 

Download Elasticsearch

Download Elasticsearch or the complete Elastic Stack (formerly ELK stack) for free and start searching and analyzing in minutes with Elastic....

www.elastic.co

 

conda create -n ainews python>=3.12
conda activate ainews

# fastapi 관련
pip install fastapi pydantic uvicorn

# elastic 관련
pip install elasticsearch

# airflow 관련
mkdir airflow && cd airflow
pip install apache-airflow

2. 폴더 구조 생성

  • 각각 별도의 폴더를 구성하고, 각자가 하는 일은 해당 폴더 내에서 이루어질 수 있도록 한다.
│   .env
│   .gitignore
│   README.md
│   
├───airflow
├───backend
│   │   news_main.py
│   │   
│   ├───services
│   │ │ newsapi.py
│           
├───data
└───elastic
    │   article_to_es.py
    └── es_client.py

3. 뉴스 API 파일 생성

 

News API – Search News and Blog Articles on the Web

“Ascender AI has a mission to apply AI to the media, and NewsAPI is one of our most valuable resources. Ascender is redefining how users interact with complex information, and the NewsAPI feed is an essential showcase for our technologies.” Braddock Ga

newsapi.org

import requests
import os
from dotenv import load_dotenv

load_dotenv()
API_KEY = os.environ.get('API_KEY')
KEYWORD = "AI" 

def get_articles():
    url = ('https://newsapi.org/v2/top-headlines?'
        f'q={KEYWORD}&'
        'sortBy=publishedAt&' # 기사 업로드 순
        f'apiKey={API_KEY}')

    response = requests.get(url)
    return response.json()