在google bigquery中,如何使用google python客户端使用javascript UDF

时间:2017-03-16 05:03:59

标签: python google-bigquery standard-sql

我正在使用标准SQL和javascript UDF在bigquery中编写查询。我能够使用WebUI和bigquery命令行工具实现这一点,但我的要求是使用google python客户端进行此查询。无法实现这一目标。请有人帮忙。

from google.cloud import bigquery
bigquery_client = bigquery.Client()
client = bigquery.Client()
query_results = client.run_sync_query("""

CREATE TEMPORARY FUNCTION CategoriesToNumerical(a array<STRING>,b    array<STRING>)
RETURNS string
LANGUAGE js AS """

var values = {};

 var counter = 0;

 for(i=0;i<a.length;i++)

 { var temp;

   temp = a[i];

   a[i] = counter;

   values[temp] = counter;

   counter ++;
  }

for(i=0;i<b.length;i++)
  {

for(var key in values)
  {
   if(b[i] == key)
   {
    b[i] = values[key];
   }
  }
}

return b;
""";

SELECT
CategoriesToNumerical(ARRAY(SELECT DISTINCT ProspectStage from   lsq.lsq_dest),ARRAY(SELECT ProspectStage from lsq.lsq_dest)) as prospectstageds

;""")

query_results.use_legacy_sql = False

query_results.run()

page_token = None

while True:
    rows1, total_rows, page_token = query_results.fetch_data(
        max_results=100,
        page_token=page_token)
    for row1 in rows1:
        print "row",row1
    if not page_token:
        break

这对我不起作用。请有人帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

问题似乎是你有两套冲突的&#34;&#34;&#34;。将其中一个替换为三个&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39;&#39

所以而不是

query_results = client.run_sync_query("""

CREATE TEMPORARY FUNCTION CategoriesToNumerical(a array<STRING>,b    array<STRING>)
RETURNS string
LANGUAGE js AS """
  javacript code
"""
SELECT *
FROM 
"""

query_results = client.run_sync_query('''

CREATE TEMPORARY FUNCTION CategoriesToNumerical(a array<STRING>,b    array<STRING>)
RETURNS string
LANGUAGE js AS """
  javacript code
"""
SELECT *
FROM 
'''