如何通过Synapse Analytics自托管集成运行时从REST API获取数据?
解决Azure Synapse通过自托管集成运行时访问内网REST API的问题
我们有一个仅能在企业内网访问的REST API数据源,本地运行测试代码可正常获取数据,但部署到Azure Synapse Analytics后无法访问。已为Synapse工作区配置自托管集成运行时,需要调整现有代码以实现正常访问。
原代码:
import requests import base64 # 用户和密码 username = "user" password = "pw" # API地址 url = "https://test.server.com/gate/pull/" # 基础认证 auth_string = f"{username}:{password}" auth_header = f"Basic {base64.b64encode(auth_string.encode()).decode()}" headers = { "Authorization": auth_header, "Content-Type": "application/json" } # GET请求 req = requests.get(url, headers=headers) json_data = req.json()
修改方案
要让代码通过自托管集成运行时访问内网API,需要给requests.get添加代理配置,指定自托管IR的默认代理地址(默认端口为8060,若你配置了其他端口请对应修改)。
修改后的代码:
import requests import base64 # 用户和密码 username = "user" password = "pw" # API地址 url = "https://test.server.com/gate/pull/" # 基础认证 auth_string = f"{username}:{password}" auth_header = f"Basic {base64.b64encode(auth_string.encode()).decode()}" headers = { "Authorization": auth_header, "Content-Type": "application/json" } # 配置自托管集成运行时代理 proxies = { "http": "http://localhost:8060", "https": "http://localhost:8060" } # GET请求(添加proxies参数) req = requests.get(url, headers=headers, proxies=proxies) json_data = req.json()
额外注意事项
- 确认自托管集成运行时已安装在能访问内网API的企业机器上,且在Synapse工作区中状态显示为在线
- 若你修改过自托管IR的监听端口,需把代码中的
8060换成实际端口 - 确保你在Synapse中使用的计算资源(如Spark池)已关联该自托管集成运行时
内容的提问来源于stack exchange,提问作者stats_guy
相关产品推荐
相关产品推荐

