ADLSFileIO.java org::apache::iceberg::azure::adlsv2::ADLSFileIO org::apache::iceberg::azure::adlsv2 /* *LicensedtotheApacheSoftwareFoundation(ASF)underone *ormorecontributorlicenseagreements.SeetheNOTICEfile *distributedwiththisworkforadditionalinformation *regardingcopyrightownership.TheASFlicensesthisfile *toyouundertheApacheLicense,Version2.0(the *"License");youmaynotusethisfileexceptincompliance *withtheLicense.YoumayobtainacopyoftheLicenseat * *http://www.apache.org/licenses/LICENSE-2.0 * *Unlessrequiredbyapplicablelaworagreedtoinwriting, *softwaredistributedundertheLicenseisdistributedonan *"ASIS"BASIS,WITHOUTWARRANTIESORCONDITIONSOFANY *KIND,eitherexpressorimplied.SeetheLicenseforthe *specificlanguagegoverningpermissionsandlimitations *undertheLicense. */ packageorg.apache.iceberg.azure.adlsv2; importcom.azure.core.http.HttpClient; importcom.azure.core.util.Context; importcom.azure.storage.file.datalake.DataLakeFileClient; importcom.azure.storage.file.datalake.DataLakeFileSystemClient; importcom.azure.storage.file.datalake.DataLakeFileSystemClientBuilder; importcom.azure.storage.file.datalake.models.DataLakeStorageException; importcom.azure.storage.file.datalake.models.ListPathsOptions; importjava.util.Collections; importjava.util.Map; importjava.util.concurrent.atomic.AtomicInteger; importorg.apache.iceberg.azure.AzureProperties; importorg.apache.iceberg.common.DynConstructors; importorg.apache.iceberg.io.BulkDeletionFailureException; importorg.apache.iceberg.io.DelegateFileIO; importorg.apache.iceberg.io.FileInfo; importorg.apache.iceberg.io.InputFile; importorg.apache.iceberg.io.OutputFile; importorg.apache.iceberg.metrics.MetricsContext; importorg.apache.iceberg.relocated.com.google.common.annotations.VisibleForTesting; importorg.apache.iceberg.util.SerializableMap; importorg.apache.iceberg.util.Tasks; importorg.apache.iceberg.util.ThreadPools; importorg.slf4j.Logger; importorg.slf4j.LoggerFactory; publicclassADLSFileIOimplementsDelegateFileIO{ privatestaticfinalLoggerLOG=LoggerFactory.getLogger(ADLSFileIO.class); privatestaticfinalStringDEFAULT_METRICS_IMPL= "org.apache.iceberg.hadoop.HadoopMetricsContext"; privatestaticfinalHttpClientHTTP=HttpClient.createDefault(); privateAzurePropertiesazureProperties; privateMetricsContextmetrics=MetricsContext.nullMetrics(); privateSerializableMap<String, String>properties; publicADLSFileIO(){} @VisibleForTesting ADLSFileIO(AzurePropertiesazureProperties){ this.azureProperties=azureProperties; } @Override publicInputFilenewInputFile(Stringpath){ returnnewADLSInputFile(path,fileClient(path),azureProperties,metrics); } @Override publicInputFilenewInputFile(Stringpath,longlength){ returnnewADLSInputFile(path,length,fileClient(path),azureProperties,metrics); } @Override publicOutputFilenewOutputFile(Stringpath){ returnnewADLSOutputFile(path,fileClient(path),azureProperties,metrics); } @Override publicvoiddeleteFile(Stringpath){ //Thereisnospecificcontractaboutwhetherdeleteshouldfail //andotherFileIOprovidersignorefailure.Logthefailurefor //nowasitisnotarequiredoperationforIceberg. try{ fileClient(path).delete(); }catch(DataLakeStorageExceptione){ LOG.warn("Failedtodeletepath:{}",path,e); } } @Override publicMap<String,String>properties(){ returnproperties.immutableMap(); } publicDataLakeFileSystemClientclient(Stringpath){ ADLSLocationlocation=newADLSLocation(path); returnclient(location); } @VisibleForTesting DataLakeFileSystemClientclient(ADLSLocationlocation){ DataLakeFileSystemClientBuilderclientBuilder= newDataLakeFileSystemClientBuilder().httpClient(HTTP); location.container().ifPresent(clientBuilder::fileSystemName); azureProperties.applyClientConfiguration(location.storageAccount(),clientBuilder); returnclientBuilder.buildClient(); } privateDataLakeFileClientfileClient(Stringpath){ ADLSLocationlocation=newADLSLocation(path); returnclient(location).getFileClient(location.path()); } @Override publicvoidinitialize(Map<String,String>props){ this.properties=SerializableMap.copyOf(props); this.azureProperties=newAzureProperties(properties); initMetrics(properties); } @SuppressWarnings("CatchBlockLogException") privatevoidinitMetrics(Map<String,String>props){ //ReportHadoopmetricsifHadoopisavailable try{ DynConstructors.Ctor<MetricsContext>ctor= DynConstructors.builder(MetricsContext.class) .hiddenImpl(DEFAULT_METRICS_IMPL,String.class) .buildChecked(); MetricsContextcontext=ctor.newInstance("adls"); context.initialize(props); this.metrics=context; }catch(NoClassDefFoundError|NoSuchMethodException|ClassCastExceptione){ LOG.warn( "Unabletoloadmetricsclass:'{}',fallingbacktonullmetrics",DEFAULT_METRICS_IMPL); } } @Override publicvoiddeleteFiles(Iterable<String>pathsToDelete)throwsBulkDeletionFailureException{ //Azurebatchoperationsarenotsupportedinallcases,e.g.withauser //delegationSAStoken,soavoidusingitfornow AtomicIntegerfailureCount=newAtomicInteger(); Tasks.foreach(pathsToDelete) .executeWith(ThreadPools.getWorkerPool()) .noRetry() .suppressFailureWhenFinished() .onFailure( (file,exc)->{ failureCount.incrementAndGet(); LOG.warn("Failedtodeletefile{}",file,exc); }) .run(this::deleteFile); if(failureCount.get()>0){ thrownewBulkDeletionFailureException(failureCount.get()); } } @Override publicIterable<FileInfo>listPrefix(Stringprefix){ ADLSLocationlocation=newADLSLocation(prefix); ListPathsOptionsoptions=newListPathsOptions(); options.setPath(location.path()); options.setRecursive(true); return()->{ try{ returnclient(location).listPaths(options,null).stream() .filter(pathItem->!pathItem.isDirectory()) .map( pathItem-> newFileInfo( pathItem.getName(), pathItem.getContentLength(), pathItem.getCreationTime().toInstant().toEpochMilli())) .iterator(); }catch(DataLakeStorageExceptione){ //otherFileIOimplementationsreturnanemptyiteratorifnothing //isfound,somimicthatbehaviorhere if(e.getStatusCode()!=404){ throwe; } returnCollections.emptyIterator(); } }; } @Override publicvoiddeletePrefix(Stringprefix){ ADLSLocationlocation=newADLSLocation(prefix); try{ client(location) .deleteDirectoryWithResponse(location.path(),true,null,null,Context.NONE) .getValue(); }catch(DataLakeStorageExceptione){ //otherFileIOimplementationsskipthedeleteifnothingisfound, //somimicthatbehaviorhere if(e.getStatusCode()!=404){ throwe; } } } }