Azure函数无法使用Python从CosmosDB读取数据

ej83mcc0  于 12个月前  发布在  Python
关注(0)|答案(1)|浏览(150)

我正在尝试将Azure函数部署到函数应用程序中,并尝试从CosmosDB读取Azure函数中的数据。
代码无法从az-task部署的函数中的CosmosDB获取数据,但如果我在本地运行相同的函数,它就能够正确获取数据。
如果我们使用VS代码部署Azure函数,那么它可以正常工作。
获取数据的代码段。

def getting_meta_data(nodecellid):
    logging.info('inside getting meta data function')
    logging.info('---------- %s',nodecellid)
    logging.info(type(nodecellid))
    endpoint = 'https://subscription-cosmosdb.documents.azure.com:443/'
    key = 'key'
    client = CosmosClient(endpoint, key)

    # Connect to the database and container
    container_name = 'container-name'
    database_name = 'database-name'
    database = client.get_database_client(database_name)
    container = database.get_container_client(container_name)
    
    data_dict = {}
    logging.info("&&&&& %s",nodecellid)
    for item in container.query_items(
                    query = f"SELECT * FROM r WHERE r['NodeCellId'] = '{nodecellid}'",
                    enable_cross_partition_query=True):
        print(item)
        data_dict = json.loads(json.dumps(item, indent=True))
        logging.info("++++++++",json.dumps(item, indent=True))  
    logging.info("//////",'meta-data value %s',data_dict) 
    
    logging.info('completed meta-data ')
    if data_dict == {}:
        logging.info('empty meta-data)
        return -1
    return data_dict

nodecellid = 'nodecellid'
getting_meta_data(nodecellid)

字符串
当我尝试使用VS部署函数从cosmosDB获取数据时,它能够获取代码,即使两个函数的内容(一个使用VS代码部署,一个使用Azure函数代码工具部署)
这两个函数代码包含相同的代码,但它们有一些区别,我将在下面提到。

VS code deployed code
**__init.py__** file which contains the code
**function.json** - which contains the binding details to the Azure function.
**sample.dat**
**readme.md**

Azure func core tools deployed code
**function_app.py** file which contains the code
**host.json** - Function App settings
**oryx-manifest.toml**


我不知道我做错了什么。
相同的代码在本地工作,在VS代码上部署Azure函数,但不在func核心工具中部署Azure函数。
我附上更多的信息日志。

Result: Failure Exception: TypeError: not all arguments converted during string formatting Stack: File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py", line 493, in _handle__invocation_request call_result = await self._loop.run_in_executor( File "/usr/local/lib/python3.9/concurrent/futures/thread.py", line 58, in run result = self.fn(*self.args, **self.kwargs) File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py", line 762, in _run_sync_func return ExtensionManager.get_sync_invocation_wrapper(context, File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/extension.py", line 215, in _raw_invocation_wrapper result = function(**args) File "/home/site/wwwroot/function_app.py", line 45, in ChronosAzureFunction tagging(blob_client) File "/home/site/wwwroot/function_app.py", line 147, in tagging getting_meta_data(nodecellid_test) File "/home/site/wwwroot/function_app.py", line 369, in getting_meta_data logging.info("++++++++",json.dumps(item, indent=True)) File "/usr/local/lib/python3.9/logging/__init__.py", line 2097, in info root.info(msg, *args, **kwargs) File "/usr/local/lib/python3.9/logging/__init__.py", line 1446, in info self._log(INFO, msg, args, **kwargs) File "/usr/local/lib/python3.9/logging/__init__.py", line 1589, in _log self.handle(record) File "/usr/local/lib/python3.9/logging/__init__.py", line 1599, in handle self.callHandlers(record) File "/usr/local/lib/python3.9/logging/__init__.py", line 1661, in callHandlers hdlr.handle(record) File "/usr/local/lib/python3.9/logging/__init__.py", line 952, in handle self.emit(record) File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py", line 829, in emit msg = self.format(record) File "/usr/local/lib/python3.9/logging/__init__.py", line 927, in format return fmt.format(record) File "/usr/local/lib/python3.9/logging/__init__.py", line 663, in format record.message = record.getMessage() File "/usr/local/lib/python3.9/logging/__init__.py", line 367, in getMessage msg = msg % self.args

tp5buhyn

tp5buhyn1#

我使用CLI将代码部署到Azure,并在我的Azure门户中正常运行。我遵循了以下步骤:

示例数据:


的数据

  • 我在我的本地机器上尝试了下面的代码,它工作成功。
def getting_meta_data(nodecellid):
    logging.info('inside getting meta data function')
    logging.info('---------- %s', nodecellid)
    logging.info('Type of nodecellid: %s', type(nodecellid))

    endpoint = '*****'
    key = '*****'
    client = CosmosClient(endpoint, key)

   
    container_name = 'SecondCont'
    database_name = 'NewDb'
    database = client.get_database_client(database_name)
    container = database.get_container_client(container_name)

    data_dict = {}
    logging.info("&&&&& %s", nodecellid)

    for item in container.query_items(
        query=f"SELECT * FROM c WHERE c.NodeCellId = '{nodecellid}'",
        enable_cross_partition_query=True):
        data_dict = json.loads(json.dumps(item, indent=True))
        print(data_dict)
        logging.info("++++++++ %s", json.dumps(item, indent=True))

    logging.info("////// meta-data value %s", data_dict)

    logging.info('completed meta-data')

    if not data_dict:
        logging.info('empty meta-data')
        return -1

    return data_dict

@app.blob_trigger(arg_name="myblob", path="mycontainer/", connection="BlobStorageConnectionString") 
def blob_trigger(myblob: func.InputStream):
    logging.info(f"Python blob trigger function processed blob")
    nodecellid = 'nodecellid1'
    getting_meta_data(nodecellid)

字符串

  • 我将我的项目文件夹转换为zip文件夹并上传到Azure云环境。

  • 下面是我用来部署到Azure函数应用程序中的命令。

az functionapp deployment source config-zip -g <your resource group> -n oct31func --src pythonBlobProj.zip

部署状态:


  • 单击blob_trigger并单击Code + Test运行代码。

  • 在monitor.

    中检查从Cosmos DB检索数据的最终状态

相关问题