大佬教程收集整理的这篇文章主要介绍了如何从 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;
}
此代码将下载所有子目录的所有文件,要从月份的特定目录下载,您可以为目录名称添加日期匹配检查。
以上是大佬教程为你收集整理的如何从 Java 中的 Azure Blob 存储中读取文件夹结构为“dir1/dir2/dir3/20210301-20210331”的 csv 文件?全部内容,希望文章能够帮你解决如何从 Java 中的 Azure Blob 存储中读取文件夹结构为“dir1/dir2/dir3/20210301-20210331”的 csv 文件?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。