簡體   English   中英

如何使用 java sdk 從我的語言環境上傳和下載文件到 azure adls?

[英]How to upload and download a file from my locale to azure adls using java sdk?

     String clientId ="***********";
     String authTokenEndpoint = "***";
     String clientKey = "****";
     AccessTokenProvider provider = new ClientCredsTokenProvider(authTokenEndpoint, clientId, 
           clientKey);
     String accountFQDN = "******";  // 
     ADLStoreClient client = ADLStoreClient.createClient(accountFQDN, provider);

1.如何使用ADLStoreClient obj上傳文件? 有沒有像

s3.putObject(BucketName, FileName, new File(Srcfileloc))

在 azure sdk 中。

2.Documentation 狀態就像創建一個文件並寫入它。 如果我的數據像圖像或大 tar 文件怎么辦!

3.我是否必須為 adls gen 1 和 adls gen2 遵循兩種不同的方法。 可以重復使用相同的代碼嗎?

Q1:如何將文件上傳到 Azure 數據湖

根據我的測試,我們可以使用以下代碼將文件從本地上傳到 Azure 數據湖。 我使用 800 MB tar.gz 文件進行測試

  1. 軟件開發工具包
 <dependency>
        <groupId>com.microsoft.azure</groupId>
        <artifactId>azure-data-lake-store-sdk</artifactId>
        <version>2.2.3</version>
      </dependency>
      <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-nop</artifactId>
        <version>1.7.21</version>
      </dependency>
  1. 代碼
AccessTokenProvider provider = new ClientCredsTokenProvider(authTokenEndpoint, clientId, clientKey);
        ADLStoreClient client = ADLStoreClient.createClient(accountFQDN, provider);

        String filepath="D:\\download\\ideaIU-2019.3.3.tar.gz";
        Path path = Paths.get(filepath);

        String filename = "test/" +path.getFileName();

        try{
            FileInputStream in = new FileInputStream(filepath);
            ADLFileOutputStream out =client.createFile(filename,IfExists.OVERWRITE);
            int bufSize = 4 * 1000 * 1000;
            out.setBufferSize(bufSize);
            byte[] buffer = new byte[bufSize];
            int n;

            while ((n = in.read(buffer)) != -1) {
                out.write(buffer, 0, n);

            }
            out.close();
            in.close();
        }
        catch (Exception e){
           // process exception


        }

在此處輸入圖片說明

更多詳情,請參考樣本

Q2:如何將文件上傳到 Azure 數據湖 Gen2

根據我的測試,我們可以使用下面的代碼

  1. 軟件開發工具包
<dependency>
      <groupId>com.azure</groupId>
      <artifactId>azure-storage-file-datalake</artifactId>
      <version>12.0.1</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.10.3</version>
    </dependency>
  1. 代碼
try {
          StorageSharedKeyCredential sharedKeyCredential =
                  new StorageSharedKeyCredential(accountName, accountKey);
          DataLakeServiceClientBuilder builder = new DataLakeServiceClientBuilder();
          builder.credential(sharedKeyCredential);
          builder.endpoint("https://" + accountName + ".dfs.core.windows.net");
          DataLakeServiceClient client = builder.buildClient();
          String fileSystem = "test";
          DataLakeFileSystemClient fileSystemClient = client.getFileSystemClient(fileSystem);
          DataLakeDirectoryClient directoryClient =fileSystemClient.getDirectoryClient("testFolder");
          String filepath="D:\\download\\ideaIU-2019.3.3.tar.gz";
          Path path = Paths.get(filepath);
          DataLakeFileClient fileClient = directoryClient.createFile(String.valueOf(path.getFileName()));
          fileClient.uploadFromFile(filepath, true);

      }catch(Exception ex){

          System.out.println(ex.getMessage());
      }

更多詳細信息,請參閱文檔示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM