【发布时间】:2021-05-26 08:12:21
【问题描述】:
我想创建一个可用于将行更新插入 MongoDB 的外部函数。我已经创建了这个函数,在本地使用 Postman 和发布后对其进行了测试。我遵循了https://docs.snowflake.com/en/sql-reference/external-functions-creating-azure-ui.html 的文档,起初,我使用了他们建议的 javascript 函数来测试和工作。但是,当我运行它 python 时,我得到一个错误。这是代码。
import logging
import azure.functions as func
import pymongo
import json
import os
from datetime import datetime
cluster = pymongo.MongoClient(os.environ['MongoDBConnString'])
db = cluster[f"{os.environ['MongoDB']}"]
collection = db[f"{os.environ['MongoDBCollection']}"]
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
name = req.params.get('name')
if not name:
try:
req_body = req.get_json()
except ValueError:
pass
else:
name = req_body.get('name')
if name:
return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
else:
collection.update_one(
filter={
'_id':req_body['_id']
},
update={
'$set': {'segment_ids': req_body['segment_ids']}
},
upsert=True)
return func.HttpResponse(
json.dumps({"status_code": 200,
"status_message": "Upsert Success",
"Timestamp": datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S"),
"_id": req_body['_id']}),
status_code=200,
mimetype="text/plain"
)
错误指出req_body 在定义之前被引用,在'_id':req_body['_id'] 行失败。在 Snowflake 中,我创建了一个名为 mongoUpsert(body variant) 的外部函数,我正在解析一个简单的查询以进行测试。
select mongoUpsert(object_construct('_id', 'someuuid', 'segment_ids;, array_construct(1,2,3,4)))
据我所知,由于某种原因,该函数没有收到我在 Snowflake 中解析的body。我不知道我做错了什么。谁能帮我?谁能解释一下 Snowflake 是如何发送参数(如正文、参数、标题)的,有没有办法指定我是否要解析正文或参数?
【问题讨论】:
标签: python sql azure-functions snowflake-cloud-data-platform