Datalab to BigQuery - 将变量值插入SQL

时间:2017-12-18 03:22:06

标签: python sql google-bigquery jupyter-notebook google-cloud-datalab

我正在尝试使用Google Datalab - BigQuery magic命令通过datalab笔记本将数据插入到BigQuery表中。 当我运行此代码时,它可以正常显示在表中的数据;

INSERT mydataset.sampletable (word, count)
VALUES('testing', 7)

但是,我想要字符串'测试'并且在他们自己的变量中编号为7,然后我将其插入到BQ中。我已经设定了:

test = 'testing'
size = 7

然后我尝试运行

%%bq query
INSERT mydataset.sampletable (word, count)
VALUES (test, size)

这会导致错误消息:" invalidQuery:无法识别的名称:在[2:9]测试" 如何在SQL函数中使用我的变量?

谢谢,

1 个答案:

答案 0 :(得分:1)

You need to use the correction syntax/notation. The way you are doing it is just issuing a straight SQL command to BigQuery i.e. it knows nothing about those parameters you are trying to set/use. I tested and the following works as expected (you need to split it into 2 Datalab commands):

%%bq query -n params_test
INSERT `grey-sort-challenge.dataflow_on_a_tram.melbourne_titles` (year,month,day,wikimedia_project,language,title,views)
VALUES(2017,1,1,'wp','en',@title,100)

%%bq execute -q params_test
parameters:
- name: title
  type: STRING
  value: the_dude_abides_in_melbourne

Results (I ran it 4 times in Datalab):

enter image description here

See here and here.