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.

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group