TristanBilot/bqfetch
TristanBilot/bqfetch has 27 GitHub stars and 3 forks. Star changes are not measured yet. Last checked 2026-11-14 04:46 UTC. Last push 2023-06-16.
측정된 스타 증가
- in 24h
- 기록 수집 중
- in 7 days
- 기록 수집 중
- in 15 days
- 기록 수집 중
- in 1 month
- 기록 수집 중
서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.
소개
A lightweight tool to fetch tables from BigQuery as pandas DataFrame very fast using BigQuery Storage API combined with multiprocessing
데이터README.md
Originalbqfetch
A lightweight tool to fetch tables from BigQuery as pandas DataFrames very fast using BigQuery Storage API combined with multiprocessing. This module also aims to fetch large tables that cannot fit into memory, by chunking the table in a smart and scalable way.
Installation
pip install bqfetch
pip install -r requirements.txt
Algorithm
- Fetch all distinct values from the given index
column. - Divide these indices in
chunksbased on the available memory and the number of cores on the machine. If multiprocessing:- Each chunk will be divided in multiple sub-chunks based on the parameter
nb_coresand the available memory. - For each sub-chunk, create a temporary table containing all the matching rows in the whole table.
- Fetch these temporary tables using BigQuery Storage as dataframes.
- Merge the dataframes.
- Delete temporary tables.
- Each chunk will be divided in multiple sub-chunks based on the parameter
If !multiprocessing:- Same process with only one temporary table and no parallel processes created.
Use case
Fetching a huge table of users using multiple cores
| id | Name | Age |
|---|---|---|
| 187 | Bartolomé | 30 |
| 188 | Tristan | 22 |
| ... | ... | ... |
>>> table = BigQueryTable("PROJECT", "DATASET", "TABLE")
>>> fetcher = BigQueryFetcher('/path/to/service_account.json', table)
>>> chunks = fetcher.chunks('id', by_chunk_size_in_GB=5)
>>> for chunk in chunks:
df = fetcher.fetch(chunk, nb_cores=-1, parallel_backend='billiard')
# ...
- First, we have to create a
BigQueryTableobject which contains the path to the BigQuery table stored in GCP. - A fetcher is created, given in parameter the absolute path to the service_account.json file, the file is mandatory in order to do operations in GCP.
- Chunks the whole table, given the
columnname and the chunk size. In this case, choosing the id column is perfect because this each value of this column appears the same number of times: 1 time. Concerning the chunks size, if by_chunk_size_in_GB=5, each chunk that will be fetched on the machine will be of size 5GB. Thus it has to fit into memory. You need to save 1/3 more memory because the size of a DataFrame object is larger than the raw fetched data. - For each chunk, fetch it.
nb_cores=-1 will use the number of cores available on the machine.parallel_backend='billiard' | 'joblib' | 'multiprocessing' specify the backend framework to use.
Fetch by number of chunks
It is also possible to use by_nb_chunks instead of by_chunk_size_in_GB. It will divided the table in N, so you cannot control more flexibly the size of each chunk.
>>> table = BigQueryTable("PROJECT", "DATASET", "TABLE")
>>> fetcher = BigQueryFetcher('/path/to/service_account.json', table)
>>> chunks = fetcher.chunks('id', by_nb_chunks=10)
>>> for chunk in chunks:
df = fetcher.fetch(chunk, nb_cores=-1, parallel_backend='billiard')
# ...
Verbose mode
>>> chunks = fetcher.chunks(column='id', by_nb_chunks=1, verbose=True)
# Available memory on device: 7.04GB
# Size of table: 2.19GB
# Prefered size of chunk: 3GB
# Size per chunk: 3GB
# Nb chunks: 1
# Nb values in "id": 96
# Chunk size: 3GB
# Nb chunks: 1
>>> for chunk in chunks:
>>> df = fetcher.fetch(chunk=chunk, nb_cores=1, parallel_backend='joblib', verbose=True)
# Use multiprocessing : False
# Nb cores: 1
# Parallel backend: joblib
# Time to fetch: 43.21s
# Nb lines in dataframe: 3375875
# Size of dataframe: 2.83GB
Warning
We recommend to use this tool only in the case where the table to fetch contains a column that can be easily chunked (divided in small parts). Thus the perfect column to achieve this is a column containing distinct values or values that appear ~ the same number of time. If some values appear thousands of times and some only fews, then the chunking will not be reliable because we need to make the assumption that each chunk will be approximatively the same size in order to estimate the needed memory necessary to fetch in an optimize way the table.
A good index colum:
This column contains distinct values so can be divided in chunks easily.
| Card number |
|---|
| 4390 3849 ... |
| 2903 1182 ... |
| 0562 7205 ... |
| ... |
A bad index colum:
This column can contains a lot of variance between values so the chunking will not be reliable.
| Age |
|---|
| 18 |
| 18 |
| 64 |
| 18 |
| ... |
More cores != faster
I remind you that adding more cores to the fetching process will not necessarily gain performance and most of the time it will even be slower. The reason is that the fetching is directly dependent on the Internet bandwidth available on your network, not the number of working cores or the computer power. However, you can do your own tests and in some cases the multiprocessing can gain time (ex: in the case where cloud machines allow only an amount of bandwidth by core, multiplying the number of cores will also multiplying the bandwidth, ex: GCP compute engines).
Contribution
bqfetch is open to new contributors, especially for bug fixing or implementation of new features. Do not hesitate to open an issue/pull request :)
License
Copyright (c) 2021-present, Tristan Bilot
관련 프로젝트 살펴보기
Asabeneh/30-Days-Of-Python
30 Days of Python 프로그래밍 챌린지는 Python 프로그래밍 언어를 30일 동안 배우기 위한 단계별 가이드입니다. 이 챌린지는 100일 이상 걸릴 수 있습니다. 자신의 속도에 맞춰 진행하세요. 다음 동영상도 도움이 될 수 있습니다: https://www.youtube.com/channel/UC7PNRuno1rzYPb1xLa4yktw
78,711 스타grafana/grafana
개방적이고 조합 가능한 옵저버빌리티 및 데이터 시각화 플랫폼입니다. Prometheus, Loki, Elasticsearch, InfluxDB, Postgres 등 여러 소스의 메트릭, 로그, 트레이스를 시각화합니다.
77,894 스타redis/redis
실시간 데이터 기반 애플리케이션을 구축하는 개발자에게 Redis는 가장 선호되고, 가장 빠르며, 기능이 가장 풍부한 캐시, 데이터 구조 서버이자 문서 및 벡터 쿼리 엔진입니다.
77,490 스타apache/superset
Apache Superset는 데이터 시각화 및 데이터 탐색 플랫폼입니다.
75,522 스타