DataFrame.to_gbq(destination_table, project_id=None, chunksize=None, reauth=False, if_exists='fail', auth_local_webserver=False, table_schema=None, location=None, progress_bar=True, credentials=None) [source]
将DataFrame写入Google BigQuery表。
此功能需要pandas-gbq软件包。
有关身份 验证的说明,请参见如何使用Google BigQuery进行身份验证指南。
参数: | destination_table : 要写入的表格名称,格式为 project_id : Google BigQuery帐户项目编号。 在环境中可用时为可选。 chunksize : 要从dataframe插入每个块的行数。 设置为 reauth : 强制Google BigQuery重新验证用户身份。 如果使用多个帐户,这将很有用。 if_exists : 目标表存在时的行为。值可以是以下之一:
如果存在表, 则引发
如果存在表,则将其删除,重新创建并插入数据。
如果存在表,则插入数据。如果不存在则创建。 auth_local_webserver : 获取用户凭据时, pandas-gbq的0.2.0版本中的新功能。 table_schema :字典类型的list,可选 与DataFrame列对应的BigQuery表字段的列表, 例如。如果未提供schema, 它将根据DataFrame列的dtypes生成。 有关字段的可用名称,请参阅BigQuery API文档。
location : 加载作业应运行的位置。 有关可用位置的列表, 请参见BigQuery位置文档。 该位置必须与目标数据集的位置匹配。 pandas-gbq的0.5.0版本中的新功能。。 progress_bar : 使用库tqdm逐块显示上传的进度条。 pandas-gbq的0.5.0版本中的新功能。 credentials : 用于访问Google API的凭据。使用此参数可以覆盖默认凭据, 例如 直接使用Compute Engine 或服务帐户 pandas-gbq的0.8.0版本中的新功能。 0.24.0版中的新功能。 |
例子,
from datalab.context import Contextimport datalab.storage as storageimport datalab.bigquery as bqimport pandas as pdfrom pandas import DataFrameimport time# Dataframe to writemy_data = [{1,2,3}]for i in range(0,100000): my_data.append({1,2,3})not_so_simple_dataframe = pd.DataFrame(data=my_data,columns=['a','b','c'])#Alternative 1start = time.time()not_so_simple_dataframe.to_gbq('TestDataSet.TestTable', Context.default().project_id, chunksize=10000, if_exists='append', verbose=False )end = time.time()print("time alternative 1 " + str(end - start))#Alternative 3start = time.time()sample_bucket_name = Context.default().project_id + '-datalab-example'sample_bucket_path = 'gs://' + sample_bucket_namesample_bucket_object = sample_bucket_path + '/Hello.txt'bigquery_dataset_name = 'TestDataSet'bigquery_table_name = 'TestTable'# Define storage bucketsample_bucket = storage.Bucket(sample_bucket_name)# Create or overwrite the existing table if it existstable_schema = bq.Schema.from_dataframe(not_so_simple_dataframe)# Write the DataFrame to GCS (Google Cloud Storage)%storage write --variable not_so_simple_dataframe --object $sample_bucket_object# Write the DataFrame to a BigQuery tabletable.insert_data(not_so_simple_dataframe)end = time.time()print("time alternative 3 " + str(end - start))