test

import com.azure.identity.DefaultAzureCredential;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.core.credential.TokenCredential;
import com.azure.core.util.Configuration;

public class AzureAccessTokenExample {
    public static void main(String[] args) {
        try {
            // Load Azure App Configuration
            Configuration configuration = Configuration.getGlobalConfiguration();

            // Set the Azure AD authority host (optional, can be omitted for default)
            String azureAuthorityHost = configuration.get("AZURE_AUTHORITY_HOST");
            
            // Create a DefaultAzureCredential using the Azure Identity library
            TokenCredential credential = new DefaultAzureCredentialBuilder()
                    .authorityHost(azureAuthorityHost) // Optional: Set the authority host
                    .build();

            // Specify the scope (optional, can be omitted for default)
            String scope = "https://management.azure.com/.default"; // Azure Management API scope

            // Obtain an Azure access token
            String accessToken = credential.getToken(scope).block().getToken();

            // Print the access token
            System.out.println("Azure Access Token: " + accessToken);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

-----

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

public class DatabricksJobStartExample {
    public static void main(String[] args) {
        try {
            // Databricks API URL
            String databricksApiUrl = "https://<YOUR-DATABRICKS-WORKSPACE>/api/2.0/jobs/runs/submit";

            // Databricks PAT (Personal Access Token)
            String databricksPat = "YOUR-DATABRICKS-PAT";

            // Define the JSON payload for starting the job
            String requestBody = "{\n" +
                    "    \"job_id\": 12345\n" +  // Replace with your Databricks job ID
                    "}";

            // Create an HTTP POST request
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(databricksApiUrl);

            // Add headers
            httpPost.addHeader("Authorization", "Bearer " + databricksPat);
            httpPost.addHeader("Content-Type", "application/json");

            // Set the request body
            StringEntity requestEntity = new StringEntity(requestBody);
            httpPost.setEntity(requestEntity);

            // Execute the request
            CloseableHttpResponse response = httpClient.execute(httpPost);

            // Check the response
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                System.out.println("Databricks job started successfully.");
            } else {
                System.err.println("Databricks job start failed. Status code: " + statusCode);
            }

            // Close the HttpClient
            httpClient.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

----------
import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.json.JSONObject; public class DatabricksJobStatusPolling { public static void main(String[] args) { try { // Databricks API URL for getting job status String databricksApiUrl = "https://<YOUR-DATABRICKS-WORKSPACE>/api/2.0/jobs/runs/get?run_id=<YOUR-RUN-ID>"; // Databricks PAT (Personal Access Token) String databricksPat = "YOUR-DATABRICKS-PAT"; // Desired job status (e.g., "TERMINATED" for completed jobs) String desiredStatus = "TERMINATED"; // Polling interval in milliseconds (e.g., 10 seconds) long pollingInterval = 10000; while (true) { // Create an HTTP GET request CloseableHttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(databricksApiUrl); // Add headers httpGet.addHeader("Authorization", "Bearer " + databricksPat); // Execute the request HttpResponse response = httpClient.execute(httpGet); // Check the response status code int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == 200) { // Parse the response JSON String responseBody = EntityUtils.toString(response.getEntity()); JSONObject jsonResponse = new JSONObject(responseBody); String currentStatus = jsonResponse.getString("state"); System.out.println("Job Status: " + currentStatus); if (currentStatus.equals(desiredStatus)) { System.out.println("Desired status reached. Job is complete."); break; // Exit the loop when desired status is reached } } else { System.err.println("Error checking job status. Status code: " + statusCode); } // Close the HttpClient httpClient.close(); // Sleep for the polling interval Thread.sleep(pollingInterval); } } catch (Exception e) { e.printStackTrace(); } }


import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; public class DatabricksJobStatusPolling { public static void main(String[] args) { try { // Databricks API URL for getting job status String databricksApiUrl = "https://<YOUR-DATABRICKS-WORKSPACE>/api/2.0/jobs/runs/get?run_id=<YOUR-RUN-ID>"; // Databricks PAT (Personal Access Token) String databricksPat = "YOUR-DATABRICKS-PAT"; // Desired job status (e.g., "TERMINATED" for completed jobs) String desiredStatus = "TERMINATED"; // Polling interval in milliseconds (e.g., 10 seconds) long pollingInterval = 10000; ObjectMapper objectMapper = new ObjectMapper(); while (true) { // Create an HTTP GET request CloseableHttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(databricksApiUrl); // Add headers httpGet.addHeader("Authorization", "Bearer " + databricksPat); // Execute the request HttpResponse response = httpClient.execute(httpGet); // Check the response status code int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == 200) { // Parse the response JSON JsonNode jsonResponse = objectMapper.readTree(response.getEntity().getContent()); String currentStatus = jsonResponse.get("state").asText(); System.out.println("Job Status: " + currentStatus); if (currentStatus.equals(desiredStatus)) { System.out.println("Desired status reached. Job is complete."); break; // Exit the loop when desired status is reached } } else { System.err.println("Error checking job status. Status code: " + statusCode); } // Close the HttpClient httpClient.close(); // Sleep for the polling interval Thread.sleep(pollingInterval); } } catch (Exception e) { e.printStackTrace(); } } }

Comments

Popular posts from this blog

Java Concurrency: Final

Java Object Size and Overhead