gpt4 book ai didi

azure - 如何使用 Scala 中的服务主体连接到 Azure SQL Server

转载 作者:行者123 更新时间:2023-12-02 08:20:01 25 4
gpt4 key购买 nike

我正在寻找一种使用服务主体和 SCALA 连接到 Azure 数据库的方法。

我希望能够生成 token 并在 JDBC 中使用它来连接到数据库。 Python 示例:

import adal

# Located in App Registrations from Azure Portal
tenant_id = "<< tenant id >> "

# Located in App Registrations from Azure Portal
resource_app_id_url = "https://database.windows.net/"

# Authority
authority = "https://login.windows.net/" + tenant_id

context = adal.AuthenticationContext(authority)
token = context.acquire_token_with_client_credentials(resource_app_id_url, service_principal_id, service_principal_secret)
access_token = token["accessToken"]

jdbc_df = spark.read \
.format("com.microsoft.sqlserver.jdbc.spark") \
.option("url", url) \
.option("dbtable", dbtable) \
.option("accessToken", access_token) \
.option("encrypt", "true") \
.option("hostNameInCertificate", "*.database.windows.net") \
.load()

Another SO Example .

谢谢

最佳答案

这是经过几天的搜索后的答案!

import com.microsoft.aad.adal4j.AuthenticationContext;
import com.microsoft.aad.adal4j.ClientCredential;
import java.util.concurrent.Executors;


val AUTHORITY_URL = "https://login.microsoftonline.com/"+tenant_id
val principalId = service_principal_id
val principalSecret = service_principal_secret
val URL="jdbc:sqlserver://xx.database.windows.net:1433;database=xx;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30"



val service = Executors.newFixedThreadPool(1);
val context = new AuthenticationContext(AUTHORITY_URL, true, service);
val credential = new ClientCredential(principalId, principalSecret);
val result = context.acquireToken("https://database.windows.net/", credential, null).get();

// println("***********************")
// println(result.getAccessToken())
// println("***********************")


val df= spark.read
.format("com.microsoft.sqlserver.jdbc.spark")
.option("url", URL)
.option("dbtable", "xx")
.option("accessToken", result.getAccessToken())
.load()

关于azure - 如何使用 Scala 中的服务主体连接到 Azure SQL Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69538639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com