如何解决如何从 Java 中的 Azure Blob 存储中读取文件夹结构为“dir1/dir2/dir3/20210301-20210331”的 csv 文件?
我已在我的 azure blob 存储帐户中安排了导出,这是一个每月运行一次,会在 dir1 / dir2 / dir3 / StartDateOfMonth-EndDateOfMonth
等文件夹下创建一个 csv 文件。
1- 我想用 java 读取这个文件而不下载它。
2 - 想要使用 spring batch master-worker 模式并行读取。
面临的问题:-
1- 我没有使用下面的行获得绝对路径
CloudAppendBlob cloudAppendBlob= container.getAppendBlobReference("blob_file_name");
log.info("cloudAppendBlob.getUri().getPath() = {}",cloudAppendBlob.getUri().getPath());
2- 如果有人帮助我了解如何在 Spring Batch master-worker 模式中进行操作,那将对我非常有帮助。 [我知道的普通 Spring Batch master-worker 模式用于 CSV 从本地路径读取它的文件]
解决方法
1- 我想用 java 读取这个文件而不下载它。
您可以使用 Spring Batch 提供的文件项读取器(平面文件、xml 文件、json 文件等)之一,并使用 org.springframework.core.io.UrlResource
对其进行配置。这是一个简单的例子:
UrlResource resource = new UrlResource("remote/url/to/your/file");
FlatFileItemReader<String> itemReader = new FlatFileItemReaderBuilder<String>()
.resource(resource)
// set other properties
.build();
2 - 想要使用 spring batch master-worker 模式并行读取。
您可以使用 Spring Batch 提供的远程分区技术,其中每个文件都在一个分区中处理(即每个文件一个工作程序)。 Spring Batch 提供了专门为此设计的 MultiResourcePartitioner。您可以在 Partitioning 部分和完整示例 here 中找到更多详细信息。
,我找到了一种解决方案,用于从 Java 中的 Azure Blob 存储下载 .csv 文件,文件夹结构为“dir1/dir2/dir3/StartDateOfMonth-EndDateOfMonth”
@Override
public List listBlobs(String containerName) {
List uris = new ArrayList<>();
String fileName=null;
try {
CloudBlobContainer container = cloudBlobClient.getContainerReference(containerName);
Iterable<ListBlobItem> blobs = container.listBlobs("$Directory",true); //for $Directory please find screenshot I have given below. this is the name that you provide during the creation of Export in your Azure Storage account
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString(environment.getProperty("azure.storage.ConnectionString")).buildClient();
BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(containerName);
FileOutputStream fout = null;
for (ListBlobItem fileBlob : blobs) {
log.info("fileBlob instanceof CloudBlob = {}",fileBlob instanceof CloudBlob);
if (fileBlob instanceof CloudBlob) {
CloudBlob cloudBlob = (CloudBlob) fileBlob;
uris.add(cloudBlob.getName());
log.info("File Name is = {}",cloudBlob.getName());
BlobClient blobClient = containerClient.getBlobClient(cloudBlob.getName());
System.out.println(blobClient.getBlobUrl());
System.out.println(blobClient.getBlobUrl().trim());
if (blobClient.exists()) {
Path p = Paths.get(cloudBlob.getName());
String file = p.getFileName().toString();
String directory = p.getParent().toString();
log.info("Downloading Blob File = {} from Directory {}",file,directory);
File dir = new File("$LOCAL_PATH"+directory);
dir.mkdirs();
fout = new FileOutputStream("$LOCAL_PATH" + cloudBlob.getName());
blobClient.download(fout);
CloudAppendBlob cloudAppendBlob= container.getAppendBlobReference(cloudBlob.getName());
uris.add(cloudAppendBlob.getUri().toURL());
log.info("cloudAppendBlob.getUri().getPath() = {}",cloudAppendBlob.getUri().toURL());
}
}
}
for (ListBlobItem blobItem : container.listBlobs()) {
uris.add(blobItem.getUri().toURL());
//System.out.println("blobItem.getUri().getPath()= "+blobItem.getUri().getPath());
}
} catch (StorageException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return uris;
}
此代码将下载所有子目录的所有文件,要从月份的特定目录下载,您可以为目录名称添加日期匹配检查。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。