gpt4 book ai didi

spring - 如何使用 Spring Batch 写入 Azure Blob?

转载 作者:行者123 更新时间:2023-12-03 03:50:31 25 4
gpt4 key购买 nike

我正在尝试使用 Spring Batch 将逐行 csv 写入 Azure Blob。

Autowiring 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: Error creating bean with name 'scopedTarget.writer' defined in class path resource [com/example/dbreader/configuration/BatchConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.batch.item.file.FlatFileItemWriter]: Factory method 'writer' threw exception; nested exception is java.lang.ClassCastException: com.azure.storage.blob.BlobClient cannot be cast to org.springframework.core.io.Resource

Spring 提示我无法将 Blob 转换为资源。

有人知道如何在 Spring Batch Writer 中指向 Azure Blob 存储吗?

谢谢,马库斯。

最佳答案

FlatFileItemWriter 需要 org.springframework.core.io.Resource 来写入数据。如果您使用的 API 未实现此接口(interface),则它无法与 FlatFileItemWriter 一起使用。您需要为 Azure 提供Resource 实现或查找实现它的库,例如 Azure Spring Boot Starter Storage client library for Java .

关于spring - 如何使用 Spring Batch 写入 Azure Blob?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66955919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com