После сокращения кода запрашивает таблицу и выводит схему таблицы:
# pip install google-cloud-bigquery
from google.cloud import bigquery
client = bigquery.Client()
dataset_ref = client.dataset("chicago_crime", project="bigquery-public-data")
dataset = client.get_dataset(dataset_ref)
table_ref = dataset_ref.table("crime")
table = client.get_table(table_ref)
table.schema[:4]
output:
[SchemaField('unique_key', 'INTEGER', 'REQUIRED', 'Unique identifier for the record.', ()),
SchemaField('case_number', 'STRING', 'NULLABLE', 'The Chicago Police Department RD Number (Records Division Number), which is unique to the incident.', ()),
SchemaField('date', 'TIMESTAMP', 'NULLABLE', 'Date when the incident occurred. this is sometimes a best estimate.', ()),
SchemaField('block', 'STRING', 'NULLABLE', 'The partially redacted address where the incident occurred, placing it on the same block as the actual address.', ())
Код, который перечисляет поля (1,3), следующий:
from operator import itemgetter
fields_list=itemgetter(1,3)(table.schema)
client.list_rows(table, selected_fields=fields_list, max_results=5).to_dataframe()
вывод:
case_number block
0 JC299491 114XX S CHAMPLAIN AVE
1 JC273204 053XX N LOWELL AVE
Как явно указать название поля, что-то в этом роде?
fields_list=['case_number', 'block']