GCP

BigQuery와 DataFlow를 활용한 Data ETL(GCP)

Somaz 2023. 10. 2. 14:08
728x90
반응형

 

Overview

지난번에는 BigQuery와 Data WareHouse에 대해서 알아보았다.

2023.05.21 - [GCP] - GCP BigQuery란? & Data Warehouse

 

 

 

이번 글에서는 BigQuery와 DataFlow를 활용한 Data ETL 과정에 대해 설명한다.

 

MongoDB에서 BigQuery로 데이터를 적재한 후, Google Sheet로 데이터를 전송하는 자동화된 워크플로우를 구축하는 것이 목표이다.

 

 

이 과정에서 활용할 GCP 리소스는 다음과 같다:

  • Dataflow: 데이터 변환 및 적재
  • BigQuery: 데이터 웨어하우스
  • Cloud Scheduler: 자동 실행 스케줄링
  • Cloud Function: 이벤트 기반 트리거 실행
  • Cloud Storage: 임시 데이터 저장소

 

 

mongodb → bigquery → google sheet 

출처 : https://medium.com/analytics-vidhya/etl-with-dataflow-bigquery-bfaf22fbd0d0

 

 

 

 

 

 

📅 관련 글

2023.04.06 - [GCP] - GCP란? - 서비스 계정 & Project 생성 / SDK(gcloud) 설치

2023.04.06 - [GCP] - GCP IAM이란?

2023.04.12 - [GCP] - GCP - SDK(gcloud) 계정 2개 등록하기

2023.05.05 - [GCP] - GCP vs AWS 리소스 비교

2023.05.19 - [GCP] - GCP BigQuery란? & Data Warehouse

2023.09.23 - [GCP] - BigQuery와 DataFlow를 활용한 Data ETL(GCP)

2023.10.03 - [GCP] - Shared VPC를 사용하여 GKE 클러스터 생성시 IAM 설정

2023.12.18 - [GCP] - GCP를 활용한 데이터 자동화(MongoDB, CloudSQL, GA, Dune)

2024.01.20 - [GCP] - Terraform 으로 GCS 생성후 Cloud CDN 생성(GCP)

2024.03.04 - [GCP] - GCP에서 딥러닝을 위한 GPU VM 서버 만들기(GCP)

2024.04.24 - [Migration] - AWS에서 GCP로 마이그레이션하는 방법 및 고려사항

 

 

 

 

 

 


 

 

Google Cloud Dataflow란?

Google Cloud Platform(GCP)에서 제공하는 완전 관리형 스트림 및 일괄 데이터 처리 서비스이다.

 

실시간 및 일괄 처리 사용 사례 모두에 대해 데이터 처리 파이프라인을 구축, 배포 및 실행하는 방법을 제공한다. Dataflow는 일괄 및 스트리밍 데이터 처리 파이프라인을 모두 구성하기 위한 오픈소스 통합 모델인 Apache Beam을 기반으로 구축되었다.

 

출처 : https://medium.com/bb-tutorials-and-thoughts/how-to-get-started-with-gcp-dataflow-822295dce7b4

 

GCP에서는 `Dataflow template`을 제공해준다.​ 사용한 템플릿은 MongoDB_to_BigQuery 이다.

따라서 `Dataflow template`을 활용해서 `MongoDB → Bigquery Table`로 변환해 줄 수 있다.

 

 

 

주요 특징

  • Fully Managed Services(완전 관리형 서비스)
  • Apache Beam SDK 기반
  • Auto Scaling(오토 스케일링)
  • Integration(통합)
  • Real-Time and Batch Processing(실시간 및 배치 처리)
  • Code Portability(코드 이식성=재사용성)
  • Debugging and Monitoring(디버깅 및 모니터링)

 

 

 


 

 

Google Cloud Function이란?

Google Cloud Functions(GCF)는 GCP에서 제공하는 서버리스 컴퓨팅 서비스이다. 개발자는 서버를 관리할 필요 없이 클라우드 이벤트에 의해 트리거되는 코드를 실행하게 할 수 있다.

출처 : https://hypemarc.com/gcp-cloud-functions

 

 

 

 

주요 특징

  • Serverless(서버리스)
  • Event-driven(이벤트 기반)
  • Scalability(확장성)
  • Languages Supported(지원되는 언어)
    • `Node.js, Python, Go, Java, .NET, Ruby, PHP`
  • Intergrated with GCP Services(GCP 서비스와 통합)
  • Stateless(실행 간 상태유지 X)
  • Short-lived(빠른 실행)
  • Pay-as-you-go(실행시간에 대해서만 비용 지불)
  • Security(보안)
  • Local Development & Testing(SDK를 활용해 로컬 테스트 가능)

 

 

 

 


 

 

Google Cloud Scheduler란?

Google Cloud Scheduler는 크론 작업 스케줄링 서비스이다. 기본적으로 Linux의 Cron과 비슷한 기능을 제공하지만 클라우드 환경에서의 확장성, 관리 및 통합 기능이 추가되어 있다.

출처 : https://medium.com/@sahaabhik9/scheduling-bigquery-procedures-using-cloud-scheduler-bq-scheduled-queries-e55acb4c33c2

 

 

 

 

주요 특징

  • Fully Managed Services(완전 관리형 서비스)
  • HTTP, Pub/Sub, App Engine Calls( HTTP, Pub/Sub, App Engine 호출)
  • CRON Grammar(CRON 문법)
  • Retry Failure(작업이 실패할 경우 재시도)
  • Time Zone Support(국가별 시간대 지원)
  • Monitoring and Logging(모니터링 및 로깅)
  • Security(보안)
  • Integration(통합)

 

 

 

 


 

 

mongodb → bigquery → google sheet  Dataflow

 

 

 

Dataflow에 대한 과정은 아래와 같다.

Enable API →BigQuery Service Account 생성 및 권한 부여 → Google Sheet 생성 및 권한부여

 BigQuery Dataset 생성 → Cloud-function용 storage 생성 →

mongodb bigquery table workflow → bigquery table googlesheet workflow

 

 

차트는 Mermaid Live Editor를 활용해서 그렸다.

flowchart TD
    Z["Enable API Service"] --> A["Create BigQuery Service Account and Grant Permission"]
    A --> B["Create Google Sheet and Grant Permission"]
    B --> C["Create BigQuery Dataset"]
    C --> D["Create Storage for Cloud-function"]
    D --> E1["mongodb → bigquery table workflow"]
    E1 --> F1["Compress Zip file (source code)"]
    E1 --> G1["Upload Zip file (source code) to bucket"]
    E1 --> H1["Create cloud function"]
    E1 --> I1["Create cloud scheduler"]
    D --> E2["bigquery table → googlesheet workflow"]
    E2 --> F2["Compress Zip file (source code)"]
    E2 --> G2["Upload Zip file (source code) to bucket"]
    E2 --> H2["Create cloud function"]
    E2 --> I2["Create cloud scheduler"]

mongodb bigquery google sheet Dataflow 과정

 

 

 

 

 


 

 

IaC 도구를 활용한 Dataflow 구축

 

IaC 도구를 활용해서 Dataflow를 구축할 수 있다.  2가지의 IaC 도구를 사용하여 구축해 볼 예정이다.

  • Terraform
  • Pulumi

 

 

 

Terraform

소스코드는 Github에 전부 작성하였다. 

https://github.com/somaz94/terraform-infra-gcp/tree/main/project/somaz-bigquery-project

 

 

생성되는 순서는 `mongodb  → bigquery →  google sheet Dataflow` 과정을 참고하면 된다. 혹시 리소스 생성에 대한 dependency에 걸리면 `depends on`을 활용하여 수정해주면 된다.

 

 

디렉토리 구조는 아래와 같다.  `main.py`는 상황에 맞게 사용할 수 있도록 여러가지 Version으로 작성하였다.

 

 

README를 참고하여 진행하면 된다.

somaz-bigquery-project/
├── README.md
├── bigquery-to-google-sheet
│   ├── bigquery.json
│   ├── main.py
│   ├── main.py.all_data
│   ├── main.py.date
│   ├── main.py.single_db
│   ├── main.py.time_UTC
│   └── requirements.txt
├── bigquery.tfvars
├── cloud-storage.tf
├── locals.tf
├── mongodb-bigquery-googlesheet-workflow.tf
├── mongodb-to-bigquery
│   ├── MongoDB_to_BigQuery
│   ├── main.py
│   ├── main.py.local
│   ├── main.py.single_db
│   └── requirement.txt
├── provider.tf
├── terraform-backend.tf
└── variables.tf

 

 

 

 

 

Pulumi

Pulumi 소스코드도 마찬가지로 Github에 전부 작성하였다. 

https://github.com/somaz94/pulumi-study/tree/main/gcp/python/bigdata-flow-functions

 

 

생성되는 순서는 Terraform과 동일하다.

 

 

디렉토리 구조는 아래와 같다.  `main.py`는 상황에 맞게 사용할 수 있도록 여러가지 Version으로 작성하였다.

README를 참고하여 진행하면 된다.

bigdata-flow-functions/
├── Pulumi.yaml
├── README.md
├── __main__.py
├── bigquery-to-google-sheet
│   ├── bigquery.json
│   ├── main.py
│   ├── main.py.all_data
│   ├── main.py.date
│   ├── main.py.single_db
│   ├── main.py.time_UTC
│   └── requirements.txt
├── bq_dataset.py
├── bq_sheet_archive.py
├── bq_sheet_function.py
├── config.py
├── mdb_bq_archive.py
├── mdb_bq_function.py
├── mongodb-to-bigquery
│   ├── main.py
│   ├── main.py.local
│   ├── main.py.single_db
│   └── requirements.txt
├── requirements.txt
├── scheduler_manager.py
├── storage.py
└── utils.py

 

 

 

 

 


 

 

 

 

마무리

본 글에서는 MongoDB에서 BigQuery, BigQuery에서 Google Sheet로 데이터를 전송하는 ETL 자동화 과정을 설명했다. 이를 통해 데이터를 효율적으로 변환하고 저장하는 방법을 학습할 수 있었다.

 

 

이번 과정을 정리하면 다음과 같다:

  • Dataflow를 활용하여 MongoDB 데이터를 BigQuery로 적재
  • Cloud Functions를 활용하여 BigQuery 데이터를 Google Sheet로 전송
  • Cloud Scheduler로 자동화 실행
  • Terraform 및 Pulumi로 IaC 적용

 

 

 

다음 단계에서는 실습을 통해 각 리소스를 직접 배포하고 테스트해보는 것이 중요하다.

 

 

 

 

 

 

 


Reference

https://medium.com/analytics-vidhya/etl-with-dataflow-bigquery-bfaf22fbd0d0

 

https://github.com/somaz94/terraform-infra-gcp/tree/main/project/somaz-bigquery-project

 

https://console.cloud.google.com/storage/browser/dataflow-templates/latest

 

https://github.com/somaz94/pulumi-study/tree/main/gcp/python/bigdata-flow-functions

 

https://github.com/somaz94/terraform-infra-gcp/tree/main/project/somaz-bigquery-project

 

https://github.com/somaz94

 

somaz94 - Overview

somaz94 has 15 repositories available. Follow their code on GitHub.

github.com

 

728x90
반응형