【问题标题】:Parse csv file by using Spring Batch and print it on console in spring Boot使用 Spring Batch 解析 csv 文件并在 Spring Boot 的控制台上打印
【发布时间】:2021-04-27 06:07:42
【问题描述】:

我的项目目标:

我必须使用 Spring Batch 读取 csv 文件并提取特定列信息,例如 (Column Name :"msdin") "msdin" 可以在控制台上打印它。但是我的应用程序显示无法启动应用程序。

它要求我配置数据源。如果我的要求是读取 csv 文件并在控制台上打印,为什么我们需要在 Spring Batch 的情况下配置数据源。

我试图找出问题,但无法解决。谁能帮我解决这个问题?

域类

public class Customer implements Serializable {

    private Long id_type;
    private String id_number;
    private String customer_name;
    private String email_address;
    private LocalDate birthday;
    private String citizenship;
    private String address;
    private Long msisdn;
    private LocalDate kyc_date;
    private String kyc_level;
    private String goalscore;
    private String mobile_network;

    public Long getId_type() {
        return id_type;
    }

    public void setId_type(Long id_type) {
        this.id_type = id_type;
    }

    public String getId_number() {
        return id_number;
    }

    public void setId_number(String id_number) {
        this.id_number = id_number;
    }

    public String getCustomer_name() {
        return customer_name;
    }

    public void setCustomer_name(String customer_name) {
        this.customer_name = customer_name;
    }

    public String getEmail_address() {
        return email_address;
    }

    public void setEmail_address(String email_address) {
        this.email_address = email_address;
    }

    public LocalDate getBirthday() {
        return birthday;
    }

    public void setBirthday(LocalDate birthday) {
        this.birthday = birthday;
    }

    public String getCitizenship() {
        return citizenship;
    }

    public void setCitizenship(String citizenship) {
        this.citizenship = citizenship;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Long getMsisdn() {
        return msisdn;
    }

    public void setMsisdn(Long msisdn) {
        this.msisdn = msisdn;
    }

    public LocalDate getKyc_date() {
        return kyc_date;
    }

    public void setKyc_date(LocalDate kyc_date) {
        this.kyc_date = kyc_date;
    }

    public String getKyc_level() {
        return kyc_level;
    }

    public void setKyc_level(String kyc_level) {
        this.kyc_level = kyc_level;
    }

    public String getGoalscore() {
        return goalscore;
    }

    public void setGoalscore(String goalscore) {
        this.goalscore = goalscore;
    }

    public String getMobile_network() {
        return mobile_network;
    }

    public void setMobile_network(String mobile_network) {
        this.mobile_network = mobile_network;
    }
}

BatchConfiguration 类

@Configuration
@EnableBatchProcessing
public class BatchConfiguration {

    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    
    @Value("classPath:/data/gcash.csv")
    private Resource inputResource;

    public ItemReader<Customer> itemReader() {
        FlatFileItemReader<Customer> customerItemReader = new FlatFileItemReader<>();
        customerItemReader.setLineMapper(linemapper());
        customerItemReader.setLinesToSkip(1);
        customerItemReader.setResource(inputResource);
        return customerItemReader;
    }

    @Bean
    public LineMapper<Customer> linemapper() {
        DefaultLineMapper<Customer> linemapper = new DefaultLineMapper<>();
        final DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
        tokenizer.setDelimiter(";");
        tokenizer.setStrict(false);
        tokenizer.setNames(new String[] { "id_type", "id_number", "customer_name", "email_address", "birthday",
                "citizenship", "address", "msisdn", "kyc_date", "kyc_level", "goalscore", "mobile_network" });
        linemapper.setFieldSetMapper(new BeanWrapperFieldSetMapper<Customer>() {
            {
                setTargetType(Customer.class);
            }
        });

        return linemapper;

    }
}

错误堆栈

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-04-27 11:05:46.235 ERROR 22368 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
    If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
    If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.3</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.gcash.milo</groupId>
    <artifactId>GCash_Milo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>GCash_Milo</name>
    <description>Developing Milo project for GCash banking application.
    </description>
    <properties>
        <java.version>8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>com.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>4.0</version>
        </dependency>
        <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.54</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.10</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-integration</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-batch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.integration</groupId>
            <artifactId>spring-integration-sftp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.integration</groupId>
            <artifactId>spring-integration-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

【问题讨论】:

  • 你也可以添加你的 pom.xml,spring boot 会根据依赖自动配置应用程序
  • 已添加 pom.xml。请看一看。
  • 如果你不需要H2的依赖,去掉它。不要把它放在 pom 中
  • 你的类路径中有 h2,所以 Spring Boot 应该为你配置一个嵌入式数据源。你的主要班级在哪里?您是否遵循推荐的结构:docs.spring.io/spring-boot/docs/current/reference/html/…?请分享一个minimal complete example 重现该问题以便能够为您提供帮助。

标签: spring-boot spring-batch


【解决方案1】:

在你的 Spring Boot 应用程序类中,将以下 sn-p 添加到 @SpringBootApplication 注解:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })

@SpringBootApplcation 注解使用@EnableAutoConfiguration,它期望配置一个数据源

【讨论】:

  • 你能指导我如何将解析的 csv 文件数据打印到控制台吗?
  • 这就是我的回答!如果您需要进一步的帮助,请找到 spring boot 应用程序类并在您的问题中分享其代码。如果您不知道应用程序类在哪里,请展开文件夹结构并分享您的问题的屏幕截图。
猜你喜欢
  • 1970-01-01
  • 2018-08-16
  • 2019-12-15
  • 1970-01-01
  • 2015-10-27
  • 2016-06-15
  • 1970-01-01
  • 2018-01-16
  • 1970-01-01
相关资源
最近更新 更多