python-3.x - 将 BigQuery 表导出到 Google Storage 时如何避免标题
问题描述
我开发了下面的代码,它有助于将 BigQuery 表导出到 Google 存储桶中。我想将文件合并到没有标题的单个文件中,以便下一个进程将使用文件而没有任何问题。
def export_bq_table_to_gcs(self, table_name):
client = bigquery.Client(project=project_name)
print("Exporting table {}".format(table_name))
dataset_ref = client.dataset(dataset_name,
project=project_name)
dataset = bigquery.Dataset(dataset_ref)
table_ref = dataset.table(table_name)
size_bytes = client.get_table(table_ref).num_bytes
# For tables bigger than 1GB uses Google auto split, otherwise export is forced in a single file.
if size_bytes > 10 ** 9:
destination_uris = [
'gs://{}/{}{}*.csv'.format(bucket_name,
f'{table_name}_temp', uid)]
else:
destination_uris = [
'gs://{}/{}{}.csv'.format(bucket_name,
f'{table_name}_temp', uid)]
extract_job = client.extract_table(table_ref, destination_uris) # API request
result = extract_job.result() # Waits for job to complete.
if result.state != 'DONE' or result.errors:
raise Exception('Failed extract job {} for table {}'.format(result.job_id, table_name))
else:
print('BQ table(s) export completed successfully')
storage_client = storage.Client(project=gs_project_name)
bucket = storage_client.get_bucket(gs_bucket_name)
blob_list = bucket.list_blobs(prefix=f'{table_name}_temp')
print('Merging shard files into single file')
bucket.blob(f'{table_name}.csv').compose(blob_list)
你能帮我找到一种跳过标题的方法吗?
谢谢,
拉古纳特。
解决方案
我们可以通过使用 jobConfig 将 print_header 参数设置为 False 来避免 header。示例代码
job_config = bigquery.job.ExtractJobConfig(print_header=False)
extract_job = client.extract_table(table_ref, destination_uris,
job_config=job_config)
谢谢
推荐阅读
- r - ggplot面积图绘制奇怪
- android - ScrollView 末尾的 TableLayout 奇怪的行为
- html - iframe 未在模式弹出窗口中加载
- python - PYTHON 3.7 _tkinter.TclError:无效的命令名称“tixBalloon”
- python - Python Django 和 DecimalField
- angular - 将编号更改为带角度的双步进
- docker - 如何在 k8s 容器中禁止网络?
- assembly - 无法使用自定义引导加载程序使用 int13H 读取扇区
- kendo-ui - 在 Kendo UI grid.template 中使用 fetch 方法
- python - 评估回归神经网络模型的准确性