使用Python将数据从RESTAPI导入SQL DB的更好方法?

2024-09-26 17:38:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经编写了一些python代码来从RESTAPI提取数据并将其加载到Azure SQL数据库中。但这一过程大约需要半个小时才能完成20000条线路。有没有更有效的方法?我在想,也许可以将数据提取为json文件,并将其放在blob storate中,然后使用azure数据工厂将数据加载到SQL中,但不知道如何以这种方式编码

def manualJournalLineItems(tenantid):
endpoint = "api.xro/2.0/manualjournals/?page=1"

result = (getAPI(endpoint,token,tenantid))
page = 1
while result['ManualJournals']:
    endpoint = "api.xro/2.0/manualjournals/?page="+str(page)
    result = (getAPI(endpoint,token,tenantid))
    for inv in result['ManualJournals']:
        for li in inv['JournalLines']:
            cursor.execute("INSERT INTO [server].dbo.[Xero_ManualJournalLines](ManualJournalID,AccountID,Description,LineAmount,TaxAmount,AccountCode,Region) VALUES(?,?,?,?,?,?,?)",inv['ManualJournalID'],li['AccountID'],li.get('Description',''),li.get('LineAmount',0),li.get('TaxAmount',0),li.get('AccountCode',0),tenantid)
            conn.commit()
    page = int(page)+1

Tags: 数据tokenapisqlgetpageliresult
2条回答

找到了答案。将()值附加到列表中,并使用executemany()将该列表插入SQL

如果Python不是强制性要求,那么可以使用Data Factory。 您将需要使用以下组件创建管道:

  • “复制数据”活动
  • 源数据集(RESTAPI)
  • 接收器数据集(Azure SQL)

***我还可以知道您的REST API托管在哪里吗?是否通过应用程序服务在Azure中?如果没有,您还需要设置[自托管集成运行时]1

您可以参考步骤here,该步骤将数据从Blob存储复制到Azure SQL

您还可以按照下面我的截图创建RESTAPI作为源代码

  1. 创建一个新管道
  2. 在“活动”搜索框中键入“复制”。将“复制数据”活动拖到管道中 enter image description here

  3. 单击“源”选项卡,然后单击“新建”以创建新的源数据集。 enter image description here

  4. 在“数据源”搜索框中键入“REST”。 enter image description here

  5. 在“REST”数据集窗口中,单击“连接”选项卡。单击“新建”创建指向RESTAPI的链接服务。 enter image description here

  6. 这里将凭证填写到REST API。 enter image description here

  7. 继续设置接收器数据集以指向Azure SQL,并测试您的管道以确保其正常工作。希望有帮助

相关问题 更多 >

    热门问题