如何从 Java 中的 Azure Blob 存储中读取文件夹结构为“dir1/dir2/dir3/20210301-20210331”的 csv 文件?

如何解决如何从 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;
        }

enter image description here

此代码将下载所有子目录的所有文件,要从月份的特定目录下载,您可以为目录名称添加日期匹配检查。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?