Spring Boot 如何使用rest模板从Azure Blob中读取大CSV文件

gwbalxhn  于 2022-11-23  发布在  Spring
关注(0)|答案(1)|浏览(127)

我需要从Azure blob读取大型CSV文件,处理记录并将这些记录存储到数据库中。目前我正在使用休息模板来完成此操作。对于小文件,它工作正常。但对于大文件,它会发出内存不足错误。
1.如何使用rest模板从Azure blob读取大型CSV文件。
1.我需要以块的形式读取数据。
1.处理数据块并将其插入数据库。

nzk0hqpo

nzk0hqpo1#

第一个提示:不要将文件加载到内存中。这就是为什么你会出现内存不足的异常。相反,读取缓冲区。
如果您使用的是Spring的RestTemplate,请按如下方式打开下载流:

InputStream fileDownloadUrlStream = new URL(downloadUrl).openStream();
return new BufferedInputStream(fileDownloadUrlStream);

您可以尝试使用此example来完成它。
但也有一个Azure存储客户端,您可以将其添加到Maven(或Gradle)项目(检查最新版本)。

<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-storage-blob</artifactId>
    <version>12.3.0</version>
</dependency>

<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure-storage</artifactId>
    <version>8.6.3</version>
</dependency>

使用分块是一种很好的方法。以下是如何使用分块Blob客户端从Azure存储下载文件分块的示例:

String connectionString = getAzureBlobStorageConnectionKeyName(accountName);
 BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString(connectionString).buildClient();
 BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(containerId); 
 BlobClient blobClient = containerClient.getBlobClient(blobName);
 BlockBlobClient blockBlobClient = blobClient.getBlockBlobClient();
 blockBlobClient.downloadWithResponse(new FileOutputStream(file),
                                      new BlobRange(0L, downloadSize * 1024 * 1024), // convert chunk size to MB
                                      null,
                                      null,
                                      false,
                                      Duration.ofMinutes(1L),
                                      null);

相关问题