我有一个包含成千上万个带有JSON内容的文本文件的文件夹,我需要读取这些文件,将其转换为POJO,然后保存至MySQL数据库。
我打算使用springboot批处理应用程序。到目前为止,这是一个问题,我所做的研究仅显示读取多个csv文件或xml文件,而没有json数据。具体来说,我需要将此方法转换为将csv文件解析为json解析器
@Bean
Public FlatFileItemReader<Person> reader(){
FlatFileItemReader<Person> reader=new FlatFileItemReader<Person> reader();
reader.setLineMapper(new DefaultLineMapper<Person>(){{
setLineTokenizer(new DeLimitedLineTokenizer(){{
setNames(new String[] {"firstname","lastname","email","age"});
}});
setFieldSetMapper(new BeanWrapperFieldSetMapper<Person> {{
setTargetType(Person.class);
}});
return reader;
}
此代码解析一个json文件
JSONParser parser = new JSONParser();
Object object = parser.parse(new FileReader("c:\\directory path\\sample.txt"));
Person person=(Person)object;
该方法可能类似于
@Bean
Public FileReader<Person> reader(){
FileReader<Person> reader=new FileReader<Person>();
/****need help on what to do here**/
return reader;
}
还看到我正在读取目录中的所有文件,我正在以这种格式传递该目录的值
@Value(value="C:\\Users\\user\\Documents\\json_dump\NW_*.txt")
private Resource[] resources;
所以我需要有关如何使用此值(所有文件的目录)而不是我先前显示的内容(单个文件位置)的帮助
Object object = parser.parse(new FileReader("c:\\directory path\\sample.txt"));
答案 0 :(得分:0)
You can use the MultiResourceItemReader
with a JsonItemReader
as delegate. Here is a quick example:
@Bean
public JsonItemReader<Person> jsonItemReader(Resources[] resources) {
JsonItemReader<Person> delegate = new JsonItemReaderBuilder<Person>()
.jsonObjectReader(new JacksonJsonObjectReader<>(Person.class))
.name("personItemReader")
.build();
MultiResourceItemReader<Person> reader = new MultiResourceItemReader<Person>();
reader.setDelegate(delegate);
reader.setResources(resources);
return reader;
}
You can find more details about the JsonItemReader
in the reference documentation.
Hope this helps.
答案 1 :(得分:0)