2017-06-15 55 views
0

我有一組ID(〜200k),我需要使用這些ID獲取BigQuery表中的所有行。我試圖在python中構建一個列表,並使用@作爲參數傳遞給SQL查詢,但我得到TypeError: 'ArrayQueryParameter' object is not iterable錯誤。這裏是我試過的代碼(非常類似於https://cloud.google.com/bigquery/querying-data#running_parameterized_queries):將數組參數傳遞給SQL以用於Python中的BigQuery

id_list = ['id1', 'id2'] 
query = """ 
    SELECT id 
    FROM `my-db` 
    WHERE id in UNNEST(@ids) 
""" 
query_job = client.run_async_query(
    str(uuid.uuid4()), 
    query, 
    query_parameters=(
     bigquery.ArrayQueryParameter('ids', 'ARRAY<STRING>', id_list) 
    ) 
) 

回答

3

可能這裏的問題是你沒有傳遞一個元組到函數。

嘗試關閉括號,像這樣前加一個逗號:

id_list = ['id1', 'id2'] 
query = """ 
    SELECT id 
    FROM `my-db` 
    WHERE id in UNNEST(@ids) 
""" 
query_job = client.run_async_query(
    str(uuid.uuid4()), 
    query, 
    query_parameters=(
     bigquery.ArrayQueryParameter('ids', 'STRING', id_list), 
    ) 
) 

在Python如果你這樣做:

t = (1) 

,然後運行:

type(t) 

你會發現結果爲int。但如果你這樣做:

t = (1,) 

然後它會產生一個元組。

+0

工作就像一個魅力:)我不能相信我花了兩天的時間!謝謝你。 – thn

1

您需要使用'STRING',而不是'ARRAY<STRING>'爲數組元素類型,例如:

query_parameters=(
    bigquery.ArrayQueryParameter('ids', 'STRING', id_list) 

querying data話題的例子是:

def query_array_params(gender, states): 
    client = bigquery.Client() 
    query = """ 
     SELECT name, sum(number) as count 
     FROM `bigquery-public-data.usa_names.usa_1910_2013` 
     WHERE gender = @gender 
     AND state IN UNNEST(@states) 
     GROUP BY name 
     ORDER BY count DESC 
     LIMIT 10; 
     """ 
    query_job = client.run_async_query(
     str(uuid.uuid4()), 
     query, 
     query_parameters=(
      bigquery.ScalarQueryParameter('gender', 'STRING', gender), 
      bigquery.ArrayQueryParameter('states', 'STRING', states))) 
    query_job.use_legacy_sql = False 

    # Start the query and wait for the job to complete. 
    query_job.begin() 
    wait_for_job(query_job) 
    print_results(query_job.results()) 
+0

我也試過,得到了同樣的錯誤。 – thn

+0

您還在設置'query_job.use_legacy_sql = False',對吧? –

+0

是的,我將它設置爲False。 – thn