Verwalten von Azure Data Lake Analytics mithilfe einer Java-App

Wichtig

Azure Data Lake Analytics am 29. Februar 2024 eingestellt. In dieser Ankündigung erhalten Sie weitere Informationen.

Für Datenanalysen kann Ihr organization Azure Synapse Analytics oder Microsoft Fabric verwenden.

In diesem Artikel erfahren Sie, wie Sie Azure Data Lake Analytics-Konten, -Datenquellen, -Benutzer und -Aufträge mit einer App verwalten, die unter Verwendung des Azure Java SDK geschrieben wurde.

Voraussetzungen

  • Java Development Kit (JDK) 8 (mit Java-Version 1.8).
  • IntelliJ oder eine andere geeignete Java-Entwicklungsumgebung. Für die Anleitungen in diesem Dokument wird IntelliJ verwendet.
  • Erstellen Sie eine Microsoft Entra Anwendung, und rufen Sie deren Client-ID, Mandanten-ID und Schlüssel ab. Weitere Informationen zu Microsoft Entra Anwendungen und Anweisungen zum Abrufen einer Client-ID finden Sie unter Erstellen einer Active Directory-Anwendung und eines Dienstprinzipals mithilfe des Portals. Der Antwort-URI und der Schlüssel stehen über das Portal zur Verfügung, sobald Sie die Anwendung erstellt und den Schlüssel generiert haben.

Authentifizieren mithilfe von Microsoft Entra ID

Der folgende Codeausschnitt enthält Code für die nicht interaktive Authentifizierung, bei der die Anwendung eigene Anmeldeinformationen bereitstellt.

Erstellen einer Java-Anwendung

  1. Öffnen Sie IntelliJ, und erstellen Sie mithilfe der Vorlage Befehlszeilen-App ein Java-Projekt.
  2. Klicken Sie mit der rechten Maustaste auf das Projekt auf der linken Seite des Bildschirms, und wählen Sie Frameworkunterstützung hinzufügen aus. Wählen Sie Maven und dann OK aus.
  3. Öffnen Sie die neu erstellte Datei pom.xml, und fügen Sie den folgenden Textausschnitt zwischen dem Tag </version> und dem Tag </project> hinzu:
<dependencies>
    <dependency>
      <groupId>com.azure.resourcemanager</groupId>
      <artifactId>azure-resourcemanager-datalakeanalytics</artifactId>
      <version>1.0.0-beta.1</version>
    </dependency>
    <dependency>
      <groupId>com.azure.resourcemanager</groupId>
      <artifactId>azure-resourcemanager-datalakestore</artifactId>
      <version>1.0.0-beta.1</version>
    </dependency>
    <dependency>
      <groupId>com.azure</groupId>
      <artifactId>azure-storage-file-datalake</artifactId>
      <version>12.7.2</version>
    </dependency>
    <dependency>
      <groupId>com.azure</groupId>
      <artifactId>azure-identity</artifactId>
      <version>1.4.1</version>
    </dependency>
</dependencies>

Wechseln Sie zu Datei > Einstellungen > Erstellen > Ausführung > Bereitstellung. Wählen Sie Buildtools > Maven > Importieren aus. Aktivieren Sie anschließend die Option Maven-Projekte automatisch importieren.

Öffnen Sie die Datei Main.java, und ersetzen Sie den vorhandenen Codeblock durch folgenden Code:

import com.azure.core.credential.TokenCredential;
import com.azure.core.management.AzureEnvironment;
import com.azure.core.management.profile.AzureProfile;
import com.azure.identity.ClientSecretCredential;
import com.azure.identity.ClientSecretCredentialBuilder;
import com.azure.resourcemanager.datalakeanalytics.DataLakeAnalyticsManager;
import com.azure.resourcemanager.datalakeanalytics.models.DataLakeAnalyticsAccount;
import com.azure.resourcemanager.datalakestore.DataLakeStoreManager;
import com.azure.resourcemanager.datalakestore.models.DataLakeStoreAccount;
import com.azure.storage.file.datalake.DataLakeFileClient;
import com.azure.storage.file.datalake.DataLakeFileSystemClient;
import com.azure.storage.file.datalake.DataLakeServiceClient;
import com.azure.storage.file.datalake.DataLakeServiceClientBuilder;
import com.azure.storage.file.datalake.models.PathAccessControl;
import com.azure.storage.file.datalake.models.PathPermissions;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collections;
import java.util.UUID;

public class Main {
    private static String adlsAccountName;
    private static String adlaAccountName;
    private static String resourceGroupName;
    private static String location;

    private static String tenantId;
    private static String subscriptionId;
    private static String clientId;
    private static String clientSecret;
    private static String fileSystemName;
    private static String localFolderPath;

    private static DataLakeAnalyticsManager analyticsManager;
    private static DataLakeStoreManager storeManager;
    private static DataLakeStoreAccount storeAccount;
    private static DataLakeAnalyticsAccount analyticsAccount;
    private static DataLakeServiceClient serviceClient;
    private static DataLakeFileSystemClient fileSystemClient;
    private static DataLakeFileClient fileClient;

    public static void main(String[] args) throws Exception {
        adlsAccountName = "<DATA-LAKE-STORE-NAME>";
        adlaAccountName = "<DATA-LAKE-ANALYTICS-NAME>";
        resourceGroupName = "<RESOURCE-GROUP-NAME>";
        location = "East US 2";

        tenantId = "<TENANT-ID>";
        subscriptionId = "<SUBSCRIPTION-ID>";
        clientId = "<CLIENT-ID>";
        clientSecret = "<CLIENT-SECRET>";
        fileSystemName = "<DATALAKE-FILE-SYSTEM-NAME>";

        localFolderPath = "C:\\local_path\\";

        // ----------------------------------------
        // Authenticate
        // ----------------------------------------
        AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);
        ClientSecretCredential creds = new ClientSecretCredentialBuilder()
                .clientId(clientId).tenantId(tenantId).clientSecret(clientSecret)
                .authorityHost("https://login.microsoftonline.com/" + tenantId + "/oauth2/token")
                .build();
        setupClients(creds, profile);

        // ----------------------------------------
        // List Data Lake Store and Analytics accounts that this app can access
        // ----------------------------------------
        System.out.println(String.format("All ADL Store accounts that this app can access in subscription %s:", subscriptionId));
        storeManager.accounts().list().forEach(acct -> System.out.println(acct.name()));

        System.out.println(String.format("All ADL Analytics accounts that this app can access in subscription %s:", subscriptionId));
        analyticsManager.accounts().list().forEach(acct -> System.out.println(acct.name()));
        waitForNewline("Accounts displayed.", "Creating files.");

        // ----------------------------------------
        // Create a file in Data Lake Store: input1.csv
        // ----------------------------------------
        createFile("input1.csv", "123,abc", true);
        waitForNewline("File created.", "Submitting a job.");

        // ----------------------------------------
        // Submit a job to Data Lake Analytics
        // ----------------------------------------
        String script = "@input = EXTRACT Row1 string, Row2 string FROM \"/input1.csv\" USING Extractors.Csv(); OUTPUT @input TO @\"/output1.csv\" USING Outputters.Csv();";
        UUID jobId = submitJobByScript(script, "testJob", creds);
        waitForNewline("Job submitted.", "Getting job status.");

        // ----------------------------------------
        // Download job output from Data Lake Store
        // ----------------------------------------
        downloadFile("output1.csv", localFolderPath + "output1.csv");
        waitForNewline("Job output downloaded.", "Deleting file.");

        deleteFile("output1.csv");
        waitForNewline("File deleted.", "Done.");
    }

    public static void setupClients(TokenCredential creds, AzureProfile profile) {

        analyticsManager = DataLakeAnalyticsManager.authenticate(creds, profile);

        storeManager = DataLakeStoreManager.authenticate(creds, profile);

        createAccounts();

        serviceClient = new DataLakeServiceClientBuilder().endpoint(storeAccount.endpoint()).credential(creds).buildClient();

        fileSystemClient = serviceClient.createFileSystem(fileSystemName);

    }

    public static void waitForNewline(String reason, String nextAction) {
        if (nextAction == null)
            nextAction = "";

        System.out.println(reason + "\r\nPress ENTER to continue...");
        try {
            System.in.read();
        } catch (Exception e) {
        }

        if (!nextAction.isEmpty()) {
            System.out.println(nextAction);
        }
    }

    // Create accounts
    public static void createAccounts() {
        // Create ADLS account
        storeAccount = storeManager.accounts().define(adlsAccountName)
                .withRegion(location)
                .withExistingResourceGroup(resourceGroupName)
                .create();

        analyticsAccount = analyticsManager.accounts().define(adlaAccountName)
                .withRegion(location).withExistingResourceGroup(resourceGroupName)
                .withDefaultDataLakeStoreAccount(adlsAccountName)
                .withDataLakeStoreAccounts(Collections.EMPTY_LIST)
                .create();
    }

    // Create a file
    public static void createFile(String path, String contents, boolean force) {
        byte[] bytesContents = contents.getBytes();

        DataLakeFileClient fileClient = fileSystemClient.createFile(path,force);
        PathAccessControl accessControl = fileClient.getAccessControl();
        fileClient.setPermissions(PathPermissions.parseOctal("744"), accessControl.getGroup(), accessControl.getOwner());
        fileClient.upload(new ByteArrayInputStream(bytesContents), bytesContents.length);
    }

    // Delete a file
    public static void deleteFile(String filePath) {
        fileSystemClient.getFileClient(filePath).delete();
    }

    // Download a file
    private static void downloadFile(String srcPath, String destPath) throws IOException {

        fileClient = fileSystemClient.getFileClient(srcPath);
        OutputStream outputStream = new FileOutputStream(destPath);
        fileClient.read(outputStream);
        outputStream.close();
    }

}

Geben Sie Werte für die im Codeausschnitt aufgerufenen Parameter an:

  • adlsAccountName
  • adlaAccountName
  • resourceGroupName
  • location
  • tenantId
  • subscriptionId
  • clientId
  • clientSecret
  • fileSystemName
  • localFolderPath

Nächste Schritte