2017-11-08 14 views
0

내가 테이블의 스키마를 얻을 수있는 방법 파이썬의 BigQuery API - 테이블 스키마/헤더

import uuid 

from google.cloud import bigquery 


def query_shakespeare(): 
    client = bigquery.Client() 
    query_job = client.run_async_query(str(uuid.uuid4()), """ 
     #standardSQL 
     SELECT corpus AS title, COUNT(*) AS unique_words 
     FROM `publicdata.samples.shakespeare` 
     GROUP BY title 
     ORDER BY unique_words DESC 
     LIMIT 10""") 

    query_job.begin() 
    query_job.result() # Wait for job to complete. 

    destination_table = query_job.destination 
    destination_table.reload() 
    for row in destination_table.fetch_data(): 
     print(row) 


if __name__ == '__main__': 
    query_shakespeare() 

같은 쿼리의 예를 감안할 때 얻을? 행은 앞의 예에서 형태

Row(('august', -1, 'aaa', 333), {'col1': 0, 'col2': 1, 'col3': 2}) 

을 가지고 있지만 패키지 구글 - 클라우드의 BigQuery == 0.28.0 헤더 JSON을 추출 할 수있는 방법을 찾을 수 없습니다. 물론 테이블 스키마의 추출도 나에게 도움이 될 것이지만 현재 Google 문서는 마지막 버전에서 작동하지 않습니다 ...

답변

1

방금 ​​쿼리 한 테이블의 스키마가 필요하면 QueryJob에서 result 방법에서 얻을 수 있습니다 :

client = bq.Client() 
query = """ 
     #standardSQL 
     SELECT corpus AS title, COUNT(*) AS unique_words 
     FROM `publicdata.samples.shakespeare` 
     GROUP BY title 
     ORDER BY unique_words DESC 
     LIMIT 10""" 
query_job = client.query(query) 
result = query_job.result() 

schema = result.schema 

결과 :

[SchemaField(u'title', u'string', u'NULLABLE', None,()), 
SchemaField(u'unique_words', u'integer', u'NULLABLE', None,())] 

(당신이 당신의 질문에 제시된 코드 버전 0.27 관련이있다).

헤더 JSON을 가져 오는 질문에 대해서는 올바르게 이해했는지 확신 할 수 없지만 json이있는 곳을 찾기 위해 스키마가 필요합니다 (여기서는 추측하고 있습니다).