我正在尝试使用文档化的pandas.DataFrame.to_gbq()
函数将pandas.DataFrame
上载到google大查询。问题是,to_gbq()
需要2.3分钟,而直接上传到Google云存储GUI只需要不到一分钟。我计划上传一组数据帧(大约32个),每个都有相似的大小,所以我想知道什么是更快的选择。
这是我正在使用的脚本:
dataframe.to_gbq('my_dataset.my_table',
'my_project_id',
chunksize=None, # i've tryed with several chunksizes, it runs faster when is one big chunk (at least for me)
if_exists='append',
verbose=False
)
dataframe.to_csv(str(month) + '_file.csv') # the file size its 37.3 MB, this takes almost 2 seconds
# manually upload the file into GCS GUI
print(dataframe.shape)
(363364, 21)
我的问题是,什么更快?
pandas.DataFrame.to_gbq()
函数上载Dataframe
Dataframe
保存为csv,然后使用Python API将其作为文件上载到BigQueryDataframe
保存为csv,然后使用this procedure将文件上载到Google云存储,然后从BigQuery读取更新:
备选方案2,使用pd.DataFrame.to_csv()
和load_data_from_file()
似乎比备选方案1花费的时间长(3个循环平均多17.9秒):
def load_data_from_file(dataset_id, table_id, source_file_name):
bigquery_client = bigquery.Client()
dataset_ref = bigquery_client.dataset(dataset_id)
table_ref = dataset_ref.table(table_id)
with open(source_file_name, 'rb') as source_file:
# This example uses CSV, but you can use other formats.
# See https://cloud.google.com/bigquery/loading-data
job_config = bigquery.LoadJobConfig()
job_config.source_format = 'text/csv'
job_config.autodetect=True
job = bigquery_client.load_table_from_file(
source_file, table_ref, job_config=job_config)
job.result() # Waits for job to complete
print('Loaded {} rows into {}:{}.'.format(
job.output_rows, dataset_id, table_id))
谢谢你!
我使用以下代码对
Datalab
中的备选方案1和3进行了比较:下面是n={100001000000000}的结果:
从结果来看,方案3比方案1快。
相关问题 更多 >
编程相关推荐