cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Java code to read azure storage file in a jar type databricks job

ShenghaoWu
New Contributor II

I have a java application, packed as a jar, and will be used as jar dbx job.

This application need

1. read azure storage file, yaml format.

2. need to get passphrase, privatekey stored in dbx, in order to access a snowflake db

my questions are:

1. how to access/read azure storage file in my java code, does dbx have api to do that, any code example including authentication and read.

2. how to access/read secrets stored in dbx in my java code. any code example including authentication and read code.

2 REPLIES 2

Kaniz_Fatma
Community Manager
Community Manager

Hi @ShenghaoWu

  1. To access an Azure Storage file in your Java code, you can use the Azure Storage SDK for Java. This can be done within your Java application packaged as a JAR file that will be used as a dbx job. Here is an example of how to read an Azure Storage file in YAML format:
// Import the required Azure Storage SDK classes
import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobServiceClient;
import com.azure.storage.blob.BlobServiceClientBuilder;
import com.azure.storage.common.StorageSharedKeyCredential;

// Set the Azure Storage account name and key
String accountName = "your-storage-account-name";
String accountKey = "your-storage-account-key";

// Create a StorageSharedKeyCredential using the account name and key
StorageSharedKeyCredential credential = new StorageSharedKeyCredential(accountName, accountKey);

// Create a BlobServiceClient using the credential
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
    .credential(credential)
    .endpoint("https://" + accountName + ".blob.core.windows.net")
    .buildClient();

// Get a reference to the container
BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient("your-container-name");

// Read the YAML file
String blobName = "your-file.yaml";
String content = containerClient.getBlobClient(blobName).readContent();

2. To access secrets stored in dbx, you can use the Databricks Secrets API. Here's an example of how to do this in your Java code:

// Import the required Databricks API classes
import com.databricks.api.DatabricksApi;
import com.databricks.api.DatabricksApiClient;
import com.databricks.api.DatabricksApiException;

// Set the Databricks personal access token
String personalAccessToken = "your-databricks-personal-access-token";

// Create a DatabricksApiClient using the personal access token
DatabricksApiClient client = new DatabricksApiClient(personalAccessToken);

// Get a reference to the Databricks API
DatabricksApi api = client.getDatabricksApi();

// Read the secret value
String secretScope = "your-secret-scope";
String secretKey = "your-secret-key";
String secretValue;
try {
    secretValue = api.getSecret(secretScope, secretKey);
} catch (DatabricksApiException e) {
    // Handle any errors
    e.printStackTrace();
    return;
}

// Use the secret value in your application
System.out.println("Secret value: " + secretValue);

  

Hi @Kaniz_Fatma ,

Thanks for your info. I have several questions:
1. if my usecase is using SPN context, is there any thing need to change for the code.

2. what's the azure jar name and dbx jar name that I need to import, I am using gradle to build my application.

Join 100K+ Data Experts: Register Now & Grow with Us!

Excited to expand your horizons with us? Click here to Register and begin your journey to success!

Already a member? Login and join your local regional user group! If there isn’t one near you, fill out this form and we’ll create one for you to join!