如何使用 Azure Function v2 (python) 将 CSV 从 Azure Blob 批量插入到 Azure Sql

问题描述 投票:0回答:1

我有以下代码,将一行插入到 azure sql DB 中。如何修改此代码以使用 Azure Function v2 (python) 从 Azure Blob (csv) 批量插入到 Azure Sql?

import azure.functions as func
import logging
from azure.functions.decorators.core import DataType
import uuid

app = func.FunctionApp()

@app.function_name(name="HttpTrigger1")
@app.route(route="hello", auth_level=func.AuthLevel.ANONYMOUS)
@app.generic_output_binding(arg_name="toDoItems", type="sql", CommandText="dbo.ToDo", ConnectionStringSetting="SqlConnectionString",
    data_type=DataType.STRING)
def test_function(req: func.HttpRequest, toDoItems: func.Out[func.SqlRow]) -> func.HttpResponse:
        item_id = str(uuid.uuid4())  # Convert UUID to string
        toDoItems.set(func.SqlRow({"Id": item_id, "title": "name", "completed": False, "url": "www"}))
        return func.HttpResponse(f"Hello John")
python azure-functions azure-sql-database azure-blob-storage
1个回答
0
投票

要将 Azure Blob csv 文件中的数据批量插入 Azure Sql 数据库,我使用了 Azure Blob 触发器 v2 函数和

pyodbc
.

您可以使用下面给出的代码-

import azure.functions as func
import logging
import os
import pyodbc
import csv

app = func.FunctionApp()

@app.blob_trigger(arg_name="myblob", path="demo-container",
                               connection="afrinstore01_STORAGE") 
def blob_trigger(myblob: func.InputStream):
    logging.info(f"Python blob trigger function processed blob"
                f"Name: {myblob.name}"
                f"Blob Size: {myblob.length} bytes")
    conn_str = os.environ['SqlConnectionString']
    conn = pyodbc.connect(conn_str)
    cursor = conn.cursor()
    reader = csv.reader(myblob.read().decode('utf-8').splitlines())
    next(reader)

    # Insert the rows into the database
    rows = [(row[0], row[1], row[2], row[3]) for row in reader]
    cursor.executemany("INSERT INTO dbo.ToDo (Id, title, completed, url) VALUES (?, ?, ?, ?)", rows)

    conn.commit()
    cursor.close()
    conn.close()

我已经添加了连接字符串,如下所示-

local.settings.json-

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "AzureWebJobsFeatureFlags": "EnableWorkerIndexing",
    "afrinstore01_STORAGE": "DefaultEndpointsProtocol=https;AccountName=afrinstore01;AccountKey=cRWkg***********;EndpointSuffix=core.windows.net",
    "SqlConnectionString": "Driver={ODBC Driver 18 for SQL Server};Server=tcp:{serverName}.database.windows.net,1433;Database={databaseName};Uid={userName};Pwd={password};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=120;"
  }
}

要求.txt-

azure-functions
pyodbc

我的csv文件中有以下数据。

enter image description here

使用给定的代码,我可以将行插入到 Azure SQL 数据库中。

enter image description here

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.