使用Python代码获取BigQuery查询的执行时间,并将其转换为易于理解的格式。
代码示例:
from google.cloud import bigquery
client = bigquery.Client()
query_job = client.query("""
SELECT *
FROM `bigquery-public-data.usa_names.usa_1910_2013`
WHERE state = 'TX'
""")
# 获取查询开始和结束时间
start_time = query_job.started
end_time = query_job.ended
# 计算并格式化查询执行时间
execution_time_seconds = (end_time - start_time).total_seconds()
minutes, seconds = divmod(execution_time_seconds, 60)
hours, minutes = divmod(minutes, 60)
execution_time_formatted = '{:02}:{:02}:{:02}'.format(int(hours), int(minutes), int(seconds))
print('查询执行时间为:{}'.format(execution_time_formatted))
该代码通过 BigQuery 客户端库获取查询开始和结束时间,并使用 Python 内置的 datetime
库计算查询执行时间。最后,使用字符串格式化操作将执行时间输出为易于理解的 HH:mm:ss 格式。