如何从BigQuery API获取列名?

时间:2019-04-19 05:29:50

标签: google-bigquery

我可以使用以下代码获取列值:

jupyter notebook

但是,如最后一行所示,这需要直接的列名。现在,我有多个查询,并且希望在外观中运行它们并显示结果。有没有类似os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'C:\\Users\xxx\Desktop\key.json' bq_client = Client() query = "SELECT msts, coreuserid, spend_usd FROM `project.f_purchase` where dt = '2019-04-02' limit 5" query_job = bq_client.query(query) results = query_job.result() for row in results: print("{}, {}, {}".format(row.msts, row.uid, row.spend_amount)) 的方式?另外,查询的结果列数也不同。

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

对于每个BigQuery Python client documentation,您可以按如下所示遍历行对象,而无需指定确切的列名:

for row in query_job:  # API request - fetches results
    # Row values can be accessed by field name or index
    assert row[0] == row.name == row["name"]
    print(row)

此外,您始终可以按照此SchemaField values

所述使用answer
result = ["{0} {1}".format(schema.name,schema.field_type) for schema in table.schema]

这是一个使用BigQuery公共数据集的示例,该示例说明了如何在不指定字段名称的情况下访问字段:

from google.cloud import bigquery
from pprint import pprint
import json

client = bigquery.Client()

query = (
    "SELECT state,max(gender) as gender FROM `bigquery-public-data.usa_names.usa_1910_2013` "
    'GROUP BY state '
    "LIMIT 10"
)
query_job = client.query(
    query,
    # Location must match that of the dataset(s) referenced in the query.
    location="US",
)  # API request - starts the query

for num, row in enumerate(query_job, start=1):  # API request - fetches results
    # Row values can be accessed by field name or index
    # assert row[0] == row.name == row["name"]
    print("{} AS {}, {} AS {}".format(row[0], query_job._query_results._properties['schema']['fields'][0]['name'], row[1], query_job._query_results._properties['schema']['fields'][1]['name']))

    #print(row[0], row[1])

print(json.dumps(query_job._query_results._properties['schema']['fields'][0]['name']))
print(query_job._query_results._properties)
#pprint(vars(query_job._query_results._properties))

哪个会产生以下输出:

superQuery:bin tamirklein$ python test.py
AK AS state, M AS gender
AL AS state, M AS gender
AR AS state, M AS gender
AZ AS state, M AS gender
CA AS state, M AS gender
CO AS state, M AS gender
CT AS state, M AS gender
DC AS state, M AS gender
DE AS state, M AS gender
FL AS state, M AS gender

答案 1 :(得分:1)

您还可以将 for 循环中的行转换为 dict(通过 dict(row))。然后键是列名,你可以对字典做任何你能做的事情——迭代键(列名)、值(列值)或两者,而无需预先明确知道列名。