使用springbatch解析csv文件并在springboot的控制台上打印

68bkxrlz  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(359)

我的项目目标:
我必须使用spring批处理读取csv文件并提取特定的列信息,比如(列名:“msdin”)“msdin”可以在控制台上打印出来。但我的应用程序显示无法启动应用程序。
如果我的要求是读取csv文件并在控制台上打印,那么为什么我们需要在spring批处理的情况下配置数据源呢。
我试图找出问题,但没能解决。有人能帮我解决这个问题吗?

域类

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>
1u4esq0p

1u4esq0p1#

在spring boot应用程序类中,将以下代码段添加到@springbootapplication注解中:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })

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

相关问题