【发布时间】:2021-08-31 03:53:48
【问题描述】:
我正在尝试使用 BigQuery Storage API 获取一个巨大的 BigQuery 表。目前,我正在使用一个流按顺序获取数据。该程序将在使用数十个虚拟 CPU 的服务器上运行,因此我想并行化表的获取以提高性能。
我正在使用的 bq 存储版本是google.cloud.bigquery.storage.v1
我在this post 中看到可以为 BALANCED 指定分片策略,以便并行计算多个流,但它看起来在 v1 中不存在。
这个选项似乎存在于 v1_beta 中,但我在 code of the repo 中找不到它。 这个选项还存在吗?或者我该如何实现并行会话?
from google.cloud.bigquery_storage import types
from google.cloud import bigquery_storage
def get_df_parallel():
num_cores = 12
bqclient = BigQueryClient()
bqstorageclient = bigquery_storage.BigQueryReadClient(credentials=CREDENTIALS)
stringify_table = f"..."
parent = "projects/{}".format(VARIABLES['PROJECT_ID'])
requested_session = types.ReadSession(
table=stringify_table,
data_format=types.DataFormat.ARROW,
)
read_session = bqstorageclient.create_read_session(
parent=parent,
read_session=requested_session,
max_stream_count=num_cores,
# module 'google.cloud.bigquery_storage_v1.gapic_types' has no attribute 'ShardingStrategy'
sharding_strategy=(types.ShardingStrategy.BALANCED),
)
readers = []
for stream in read_session.streams:
position = bigquery_storage.types.StreamPosition(stream=stream)
reader = bqstorageclient.read_rows(position)
readers.append(reader)
df = pd.concat([reader.to_dataframe(session) for reader in readers])
return df
【问题讨论】:
标签: python google-cloud-platform google-bigquery