![](/img/trans.png)
[英]How to download a file from azure adls gen 1 to my local using java sdk?
[英]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 文件進行測試
<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>
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
根據我的測試,我們可以使用下面的代碼
<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>
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.