- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个读者知道如何读取包含 14 列的 CSV 文件,我希望它能够接收包含许多列(~500)的文件并仅读取这 14 列,我知道解决方案应该包括 FieldSetMapper (根据这个问题: read only selective columns from csv file using spring batch )但我找不到合适的例子。这是我当前的读者:
@Bean
public FlatFileItemReader<RowInput> csvRowsReader() {
FlatFileItemReader<RowInput> reader = new FlatFileItemReader<>();
Resource resource = new FileSystemResource(new File(FileManager.getInstance().getInputFileLocation()));
reader.setResource(resource);
reader.setLinesToSkip(1);
reader.setLineMapper(new DefaultLineMapper<RowInput>(){{
setLineTokenizer(new DelimitedLineTokenizer(){{
setNames(new String[]{"Field_1", "Field_2", "Field_3", "Field_4", "Field_5",
"Field_6", "Field_7", "Field_8", "Field_9", "Field_10", "Field_11",
"Field_12", "Field_13", "Field_14"});
}});
setFieldSetMapper(new BeanWrapperFieldSetMapper<RowInput>(){{
setTargetType(RowInput.class);
}});
}});
reader.setLinesToSkip(1);
return reader;
}
异常(exception)是:
原因:org.springframework.batch.item.file.transform.In CorrectTokenCountException:记录中发现的 token 数量不正确:预期 14,实际 544
我尝试使用的FieldSetMapper:
public class InputFieldSetMapper implements FieldSetMapper<RowInput>{
public RowInput mapFieldSet(FieldSet fs) {
if (fs == null) {
return null;
}
RowInput input = new RowInput();
input.setField1(fs.readString("Field_1"));
input.setField2(fs.readString("Field_2"));
// and so on...
return input;
}
}
最佳答案
您需要在 LineTokenizer
上设置 includedFields
属性,以指定解析输入文件时要包含哪些字段。在你的情况下,它应该是这样的:
@Bean
public FlatFileItemReader<RowInput> csvRowsReader() {
FlatFileItemReader<RowInput> reader = new FlatFileItemReader<>();
Resource resource = new FileSystemResource(new File(FileManager.getInstance().getInputFileLocation()));
reader.setResource(resource);
reader.setLinesToSkip(1);
reader.setLineMapper(new DefaultLineMapper<RowInput>(){{
setLineTokenizer(new DelimitedLineTokenizer(){{
setNames(new String[]{"Field_1", "Field_2", "Field_3", "Field_4", "Field_5",
"Field_6", "Field_7", "Field_8", "Field_9", "Field_10", "Field_11",
"Field_12", "Field_13", "Field_14"});
setIncludedFields(0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10 ,11 ,12 ,13);
}});
setFieldSetMapper(new BeanWrapperFieldSetMapper<RowInput>(){{
setTargetType(RowInput.class);
}});
}});
return reader;
}
编辑:添加具有非顺序字段的示例
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
@Configuration
@EnableBatchProcessing
public class MyJob {
@Autowired
private JobBuilderFactory jobs;
@Autowired
private StepBuilderFactory steps;
@Bean
public FlatFileItemReader<Person> itemReader() {
return new FlatFileItemReaderBuilder<Person>()
.name("personItemReader")
.resource(new ClassPathResource("persons.csv"))
.delimited()
.includedFields(new Integer[] {0, 2})
.names(new String[] {"id", "lastName"})
.targetType(Person.class)
.build();
}
@Bean
public ItemWriter<Person> itemWriter() {
return items -> {
for (Person item : items) {
System.out.println("person = " + item);
}
};
}
@Bean
public Step step() {
return steps.get("step")
.<Person, Person>chunk(1)
.reader(itemReader())
.writer(itemWriter())
.build();
}
@Bean
public Job job() {
return jobs.get("job")
.start(step())
.build();
}
public static void main(String[] args) throws Exception {
ApplicationContext context = new AnnotationConfigApplicationContext(MyJob.class);
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
Job job = context.getBean(Job.class);
jobLauncher.run(job, new JobParameters());
}
public static class Person {
String id;
String firstName;
String lastName;
int age;
public Person() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"id='" + id + '\'' +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", age=" + age +
'}';
}
}
}
输入文件persons.csv
如下:
1,foo1,bar1,10
2,foo2,bar2,20
该示例展示了如何仅映射 id
和 lastName
字段。
希望这有帮助。
关于spring - 如何使用 Spring Batch FlatFileItemReader 仅读取 CSV 文件中的子集列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52869410/
我正在执行大型 ffdf 对象的子集,我注意到当我使用 subset.ff 时,它会生成大量 NA。我通过使用 ffwhich 尝试了另一种方法,结果要快得多,并且没有生成 NA。这是我的测试: li
我对 Prolog 有点陌生。我正在尝试编写一个函数subset(Set, Subset) 来确定Subset 是否是Set 的子集(duh)。另外,如果第二个参数没有实例化,它应该输出每个可能的子集
一、题目 给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。 解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。 二、示例 输入:nums = [1,2
我想从数据帧的操作中排除一列。当然,我可以在没有要排除的列的情况下复制数据框,但这似乎是一种解决方法。我认为必须有一种更简单的方法来进行子集化。 所以这个示例代码应该显示我在做什么。 df colMe
我有一个 SpatialPolygonsDataFrame我通过使用 readOGR 读取 shapefile 创建的在 rgdal包裹。我正在尝试使用它来使用 spsample 生成采样网格在 sp
我想解决一个简单的问题,但即使我尝试了很多不同的方法,我也找不到解决方案。我正在使用 SICStus Prolog (如果这很重要),并且我想获取列表的所有子列表/子集(我不知道哪个术语是正确的),其
我目前正在使用 shinyTable,它是 HandsonTable (https://github.com/trestletech/shinyTable) 的 shiny 兼容实现。巧合的是,我意识
我正在 Delphi 中构建一个表单,其中包含服务下拉列表和用于选择服务的附加组件网格。我获取的数据来自 API,并且我将服务的数据存储在 ADODataSet 中,如下所示: ID (integer
subset() 函数有问题。如何通过观察次数对我的数据框的一个因子进行子集化? NAME CLASS COLOR VALUE antonio
我想知道是否有任何简单的算法来比较一个散列是否是另一个散列的子集。 例如,如果 $HASH{A} = B; $HASH{B} = C; $HASH{C} = D; $HASH2{A} = B; $HA
这个问题在这里已经有了答案: Array combinations without repetition (1 个回答) 关闭 8 年前。 给定一个数组,如何在 postgresql 中找到一定大小
我有下一个程序。我应该如何在 main 中使用迭代器以显示总和为 0 的子集? 我的程序应该打印: 2 -2 5 -5 # include # include using namespace st
我正在寻找一个可以自定义的 Markdown 解析器,最好是 Javascript。特别是,我想删除使用实际 HTML 标签的选项。我尝试编辑摊牌的来源,但无法弄清楚。 Jquery 集成也很好,尽管
我有一个包含名称列表的文件(引用文件 1): Apple Bat Cat 我有另一个文件(引用文件 2),其中包含名称列表和详细信息引用: Apple bla blaa aaaaaaaaagggggg
我有两个带有排序行的文件。一个文件 (B) 是另一个文件 (A) 的子集。我想找到 A 中不在 B 中的所有行。理想情况下,我想创建一个包含这些行的文件 (C)。这在 Unix 中可能吗?我正在寻找一
我有一个包含肽序列的列的数据框,我只想保留字符串中没有内部“R”或“K”的行。 df1 <- data.frame( Peptide = c("ABCOIIJUHFSAUJHR", "AOFI
这个问题在这里已经有了答案: How to subset matrix to one column, maintain matrix data type, maintain row/column na
假设我有一个列表向量: library(tidyverse) d 2) # A tibble: 5 x 1 x 1 2 3 4 5 最佳答案 应该是 lengt
我自己从来没有运行过javadoc(无论是在命令行还是ant's javadoc task;我将使用ant)——我需要为我编写的库生成一个javadoc。 问题是我的 java 库被组织成几个包,在
假设一个多方加密方案,类似于答案:Encryption with multiple different keys? . 那是。一组键K可以用来破译密文。 有没有办法过期: K'⊆ K 这样 K \ K
我是一名优秀的程序员,十分优秀!