google-bigquery - Dialogflow 与 BigQuery 集成 - 将整数参数传递给 BQ

标签 google-bigquery dialogflow-es

我正在创建一个从 BigQuery 检索数据的聊天机器人,但 Dialogflow 中的数据类型存在问题:

Dialogflow 参数为:评级 (@sys.number-int)、国家/地区(字符串)、部门(字符串)

当我执行以下代码时,似乎bigquery正在以字符串形式接收评级(国家和部门这些字符串在BigQuery中工作正常),所以这就是我尝试CAST但没有运气的原因。

有人可以帮助我将 INT64 变量从 Dialogflow 传递到 BigQuery 吗?

 function buyAgainPredictor(agent) {
    const OPTIONS = {
                query: 'WITH pred_table AS (SELECT CAST(`request.body.queryResult.outputContexts[0].parameters.Rating´ AS INT64) as Rating, "request.body.queryResult.outputContexts[0].parameters.Department" as Department,  "request.body.queryResult.outputContexts[0].parameters.Country" as Country)' +
                'SELECT cast(predicted_label as INT64) as predicted_label ' +
                'FROM ML.PREDICT(MODEL Customer_feedback.recommend_model,  TABLE pred_table)',
                timeoutMs: 10000,
                useLegacySql: false,
                queryParameters: {}

DialogFlow parameter definition

最佳答案

您没有替换字符串中的变量。在查询中尝试一下:

`WITH pred_table AS (
  SELECT ${request.body.queryResult.outputContexts[0].parameters.Rating} as Rating,
     "${request.body.queryResult.outputContexts[0].parameters.Department}" as Department,
     "${request.body.queryResult.outputContexts[0].parameters.Country}" as Country)

SELECT cast(predicted_label as INT64) as predicted_label 
     FROM ML.PREDICT(MODEL Customer_feedback.recommend_model, TABLE pred_table)`

通常,您不想在 SQL 查询 ( SQL injection ) 中使用用户输入,请查看 parameterized queries 上的文档。考虑到这一点,您可以尝试以下操作:

const OPTIONS = {
    query: `WITH pred_table AS (
                SELECT @rating as Rating,
                       @department as Department,  
                       @country as Country)

            SELECT cast(predicted_label as INT64) as predicted_label
                 FROM ML.PREDICT(MODEL Customer_feedback.recommend_model, TABLE pred_table)`,

    timeoutMs: 10000,
    useLegacySql: false,
    queryParameters: {
        rating: parameters.Rating,
        department: parameters.Department,
        country: parameters.Country
    }
}

关于google-bigquery - Dialogflow 与 BigQuery 集成 - 将整数参数传递给 BQ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54957658/

相关文章:

firebase - 我如何跟踪 firebase 和 bigquery 中的流行用户流?

node.js - 使用更新权限事件触发不同的意图

python - 有没有办法通过 webhook 覆盖对话流 session 中的上下文参数?

python - Dialogflow python 客户端版本控制

node.js - 我无法删除对 google 项目的操作

google-bigquery - 从 Google Cloud Storage 提供 Google 客户服务帐户的 keyFilename

google-api - 如何在Google BigQuery中进行表操作?

backend - 具有用户特定数据的对话流

python-3.x - 为什么执行成功的bigquery插入后它会存储在临时表中?

google-bigquery - 未找到函数 : group_concat