FileSystem exists check using Azure File Data Lake client library for Java

Viewed 394

We have getFileSystemClient API - https://docs.microsoft.com/en-us/java/api/overview/azure/storage-file-datalake-readme?view=azure-java-stable#create-a-datalakefilesystemclient

and create file system API - https://docs.microsoft.com/en-us/java/api/overview/azure/storage-file-datalake-readme?view=azure-java-stable#create-a-file-system

Before saving data into Azure Data Lake File System, how do I check if file-system already exist or not? If file-system does not exist, invoke "create file system API" else getFileSystemClient API.

2 Answers

Unfortunately, there is no such direct ways to check if file-system exists, you can use getFileName() to get the file name with out path and use try catch block.

Within a file system, this function creates a new file. If another file with the identical name already exists, it will be overwritten.

DataLakeFileSystemClient.createFileWithResponse(String fileName, String permissions, String umask, PathHttpHeaders headers, Map<String,String> metadata, DataLakeRequestConditions requestConditions, Duration timeout, Context context) Method | Microsoft Docs

DataLakeServiceClient does not provide an API to check if a file system exists. The following worked for me (may be there is a better way to do it):

DataLakeServiceClient dataLakeServiceClient = new DataLakeServiceClientBuilder()
            .endpoint("https://XXXX.dfs.core.windows.net")
            .credential(new DefaultAzureCredentialBuilder().build())
            .buildClient();

Optional<FileSystemItem> raw1 =
                dataLakeServiceClient.listFileSystems().stream().filter(x -> x.getName().equals("raw1")).findFirst();

if (raw1.isPresent()) {
   // proceed
}
Related