如何使用spring批处理写入azure blob?

bpsygsoo  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(388)

我正在尝试用spring批处理将csv逐行写入azure blob。
自动连接azure存储:

@Value("${azure.storage.ConnectionString}")
private String connectionString;

@Value("${azure.storage.container.name}")
private String containerName;

@Bean
@StepScope
public FlatFileItemWriter<Pojo> writer(
        @Value("#{stepExecutionContext['marketName']}") String marketName,
        @Value("#{jobParameters['startDate']}") String startDate,
        @Value("#{jobParameters['YYYY-MM']}") String yearMonth
) {
    // Create writer instance
    FlatFileItemWriter<Pojo> writer = new FlatFileItemWriter<>();

    String fullPath = yearMonth + "/" +  marketName + "/" +
            marketName + "_" + startDate;

    BlobContainerClient container = new BlobContainerClientBuilder()
            .connectionString(connectionString)
            .containerName(containerName)
            .buildClient();

    BlobClient blob = container.getBlobClient(fullPath);

    // Set output file location
    writer.setResource((Resource) blob);

    // All job repetitions should "append" to same output file
    writer.setAppendAllowed(true);

    // Name field values sequence based on object properties
    writer.setLineAggregator(new DelimitedLineAggregator<Pojo>() {
        {
            setDelimiter(",");
            setFieldExtractor(new BeanWrapperFieldExtractor<Pojo>() {
                {
                    setNames(new String[] {
                            "market",
                            "epsg",
                            "xbin",
                            "ybin",
                            "latitude",
                            "longitude",
                            "totalDownlinkVol",
                            "totalUplinkVol"
                    });
                }
            });
        }
    });
    return writer;
}

然后我得到一个错误:
org.springframework.beans.factory.beancreationexception:创建名为'scopedtarget.writer'的bean时出错,该bean在类路径资源[com/example/dbreader/configuration/batchconfig.class]中定义:通过工厂方法示例化bean失败;嵌套异常为org.springframework.beans.beaninstantiationexception:未能示例化[org.springframework.batch.item.file.flatfileitemwriter]:工厂方法“writer”引发异常;嵌套的异常是java.lang.classcastexception:com.azure.storage.blob.blobclient无法转换为org.springframework.core.io.resource
spring抱怨我不能将blob转换为资源。
有人知道如何在springbatchwriter中指向azure blob存储吗?
谢谢,马库斯。

xj3cbfub

xj3cbfub1#

这个 FlatFileItemWriter 需要 org.springframework.core.io.Resource 写入数据。如果您使用的api未实现此接口,则无法与 FlatFileItemWriter . 你需要提供一个 Resource 或者寻找一个实现它的库,比如azure spring boot starter storage client library for java。

相关问题