JDBC を使って Azure Data Explorer に接続する

Java Database Connectivity (JDBC) は、データベースへの接続とクエリの実行に使われる Java API です。 JDBC を使って Azure Data Explorer に接続できます。 この機能は、Microsoft SQL Serverをエミュレートする Azure Data Explorer の TDS 準拠エンドポイントによって可能になります。 エンドポイントでは、TDS バージョン 7.x と 8.0 がサポートされています。

詳細については、Azure Data Explorer でのSQL Server エミュレーションの概要に関するページを参照してください。

JDBC を使用して接続する

次の手順では、JDBC を使用して Azure Data Explorerに接続する方法について説明します。

  1. JAR、JAR、adal4jおよびそのすべての依存関係を使用mssql-jdbcしてアプリケーションを作成します。 のバージョンと 1.6.3 バージョンを使用する場合に必要な依存関係のmssql-jdbc一覧を7.0.0次にadal4j示します。

    mssql-jdbc-7.0.0.jre8.jar
    adal4j-1.6.3.jar
    accessors-smart-1.2.jar
    activation-1.1.jar
    asm-5.0.4.jar
    commons-codec-1.11.jar
    commons-lang3-3.5.jar
    gson-2.8.0.jar
    javax.mail-1.6.1.jar
    jcip-annotations-1.0-1.jar
    json-smart-2.3.jar
    lang-tag-1.4.4.jar
    nimbus-jose-jwt-6.5.jar
    oauth2-oidc-sdk-5.64.4.jar
    slf4j-api-1.7.21.jar
    
  2. JDBC ドライバー クラス com.microsoft.sqlserver.jdbc.SQLServerDriver を使用するためのアプリケーションを作成します。 次の形式の接続文字列で接続できます。 をクラスター名とクラスター リージョンに置き換え、<database_name>データベース名に置き換えます<cluster_name.region>

    jdbc:sqlserver://<cluster_name.region>.kusto.windows.net:1433;database=<database_name>;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.kusto.windows.net;loginTimeout=30;authentication=ActiveDirectoryIntegrated
    

JDBC ユーザー認証

ユーザー プリンシパルの JDBC で Microsoft Entra ID を使用してプログラムで認証する方法の例を次に示します。

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
import com.microsoft.aad.msal4j.*;

public class Sample {
  public static void main(String[] args) throws Exception {
    IAuthenticationResult authenticationResult = futureAuthenticationResult.get();
    SQLServerDataSource ds = new SQLServerDataSource();
    ds.setServerName("<cluster_DNS>");
    ds.setDatabaseName("<database_name>");
    ds.setHostNameInCertificate("*.kusto.windows.net"); // Or appropriate regional domain.
    ds.setAuthentication("ActiveDirectoryIntegrated");
    try (Connection connection = ds.getConnection();
         Statement stmt = connection.createStatement();) {
      ResultSet rs = stmt.executeQuery("<T-SQL_query>");
      /*
      Read query result.
      */
    } catch (Exception e) {
      System.out.println();
      e.printStackTrace();
    }
  }
}

JDBC アプリケーション認証

アプリケーション プリンシパルの JDBC で Microsoft Entra ID を使用してプログラムで認証する方法の例を次に示します。

import java.sql.*;
import com.microsoft.sqlserver.jdbc.*;
import com.microsoft.aad.msal4j.*;
import java.net.MalformedURLException;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class Sample {
  public static void main(String[] args) throws Throwable {
    // Can also use tenant name.
    String authorityUrl = "https://login.microsoftonline.com/<tenant_ID>";
    Set<String> scopes = new HashSet<>();
    scopes.add("https://<cluster_DNS>/.default");

    IConfidentialClientApplication clientApplication = ConfidentialClientApplication.builder("<application_client_ID>", ClientCredentialFactory.createFromSecret("<application_key>")).authority(authorityUrl).build();
    CompletableFuture<IAuthenticationResult> futureAuthenticationResult = clientApplication.acquireToken(ClientCredentialParameters.builder(scopes).build());
    IAuthenticationResult authenticationResult = futureAuthenticationResult.get();
    SQLServerDataSource ds = new SQLServerDataSource();
    ds.setServerName("<cluster_DNS>");
    ds.setDatabaseName("<database_name>");
    ds.setAccessToken(authenticationResult.accessToken());
    connection = ds.getConnection();
    statement = connection.createStatement();
    ResultSet rs = statement.executeQuery("<T-SQL_query>");
    /*
    Read query result.
    */
  }
}