【问题标题】:GCP Bigquery - query parameters in federated queriesGCP Bigquery - 联合查询中的查询参数
【发布时间】:2022-01-12 11:13:16
【问题描述】:

我未能实现此question 中提出的solution

我的sql代码:

declare bq_last_id string;
declare external_sql string;
set bq_last_id = 'select max(id) from bq_dataset.bq_table';
set external_sql = '"select * from mysql_table where id > ('|| bq_last_id ||')"';
execute immediate 'select * from external_query("my-gcp-project.my-region.my-connection-name",'|| external_sql || ');'

我收到以下错误

无效的表值函数external_query 获取查询失败 来自 MySQL 服务器的架构。错误:MysqlErrorCode(1142):SELECT 命令 在 [1:22] 拒绝用户 'XXX@'cloudsqlproxy~' 访问表 'XXX' [1:1]

如果我像这样硬编码 bq_last_id 的值,它可以正常工作。

set external_sql = '"select * from mysql_table where id > 123456"';

编辑 1: 我在执行语句之前打印了语句,它看起来像这样,我猜这看起来不错。

select * from external_query("my-gcp-project.my-region.my-connection-name", "select * from mysql_table where id > (select max(id) from bq_dataset.bq_table)");

编辑 2: 最后,我不得不重新插入数据,因此使用下面的答案和一个括号,我能够做到这一点。

declare bq_last_id int64; # Change the type here
declare external_sql string;
set bq_last_id = (select max(id) from bq_dataset.bq_table); # put the query in parenthesis to force BQ to evaluate it
set external_sql = '"select * from mysql_table where id > ('|| bq_last_id ||')"';
execute immediate 'insert into bq_dataset.bq_table (select * from external_query("my-gcp-project.my-region.my-connection-name",'|| external_sql || '))'

谢谢。

【问题讨论】:

  • 您确定生成的查询吗?您是否尝试在立即执行之前打印它?
  • 我添加了一个编辑来显示它在立即执行之前的样子。

标签: mysql google-cloud-platform google-bigquery google-cloud-sql


【解决方案1】:

感谢您的更新,这里的问题是:您使用 BQ 表在参考中查询您的 Cloud SQL 实例。那是行不通的。您必须先评估 max(id),然后再将其添加到查询中,就像这样

declare bq_last_id int64; # Change the type here
declare external_sql string;
set bq_last_id = (select max(id) from bq_dataset.bq_table); # put the query in parenthesis to force BQ to evaluate it
set external_sql = '"select * from mysql_table where id > ('|| bq_last_id ||')"';
execute immediate 'select * from external_query("my-gcp-project.my-region.my-connection-name",'|| external_sql || ');'

【讨论】:

    猜你喜欢
    • 2021-05-05
    • 1970-01-01
    • 2020-08-03
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    • 1970-01-01
    • 2015-11-28
    • 1970-01-01
    相关资源
    最近更新 更多