我正在创建一个从 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: {}
最佳答案
您没有替换字符串中的变量。在查询
中尝试一下:
`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/