2016-08-11 94 views
-1

我必須編寫一個批處理作業,它將使用作爲目標的Teradata DB &,使用Spring批處理框架的Postgres DB。任何人都可以讓我知道或者向我推薦一個關於如何儘可能實現這個目標的教程?做spring批處理框架支持teradata?

如果不是,我可以探索的其他選擇是什麼?

UPDATE

我有一個春天批處理作業,試圖使其與甲骨文(來源)& Postgres的(目標)的工作,以後我會改變源Teradata的。但是,我得到的錯誤,我認爲是由於,在Oracle DB中查找作業序列(序列無法創建,因爲數據庫用戶具有隻讀訪問權限)的事實。如何製作作業,在目標數據庫(Postgres)中創建序列?

錯誤:

at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.executeLocalJobs(JobLauncherCommandLineRunner.java:227) 
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.launchJobFromProperties(JobLauncherCommandLineRunner.java:121) 
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.run(JobLauncherCommandLineRunner.java:115) 
at org.springframework.boot.SpringApplication.runCommandLineRunners(SpringApplication.java:672) 
... 5 common frames omitted 
Caused by: java.sql.SQLException: ORA-02289: sequence does not exist 

at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112) 
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331) 
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:288) 
at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:745) 
at oracle.jdbc.driver.T4CStatement.doOall8(T4CStatement.java:210) 
at oracle.jdbc.driver.T4CStatement.executeForDescribe(T4CStatement.java:804) 
at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1049) 
at oracle.jdbc.driver.T4CStatement.executeMaybeDescribe(T4CStatement.java:845) 
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1154) 
at oracle.jdbc.driver.OracleStatement.executeQuery(OracleStatement.java:1313) 
at org.springframework.jdbc.support.incrementer.AbstractSequenceMaxValueIncrementer.getNextKey(AbstractSequenceMaxValueIncrementer.java:69) 
... 40 common frames omitted 

2016-08-12 12:34:57.467 INFO 11660 --- [main] s.c.a.AnnotationConfigApplicationContext : Closing org.spring[email protected]1cea91 b: startup date [Fri Aug 12 12:34:22 IST 2016]; root of context hierarchy 
2016-08-12 12:34:57.469 INFO 11660 --- [main] o.s.j.e.a.AnnotationMBeanExporter   : Unregistering JMX-exposed beans on shutdown 

** ** BatchConfiguration.java

@Configuration 
@EnableBatchProcessing 
@ComponentScan(basePackageClasses = MyBatchConfigurer.class) 
public class BatchConfiguration { 

@Autowired 
public JobBuilderFactory jobBuilderFactory; 

@Autowired 
public StepBuilderFactory stepBuilderFactory; 

@Autowired 
@Qualifier("oracleDBDataSource") 
public DataSource dataSource1; 

@Autowired 
@Qualifier("postgresDataSource") 
public DataSource dataSource2; 



//Read an Event from oracle DB 
@Bean 
public ItemReader<Event> reader(){ 

    JdbcCursorItemReader<Event> databaseReader = new JdbcCursorItemReader<>(); 
    String sqlQ = "select * from test_event"; 
    databaseReader.setDataSource(dataSource1); 
    databaseReader.setSql(sqlQ); 
    databaseReader.setRowMapper(new BeanPropertyRowMapper<>(Event.class));  
    return databaseReader; 
} 
//EventItemProcessor 
@Bean 
public EventItemProcessor processor() { 

    return new EventItemProcessor(); 
} 

@Bean 
public JdbcBatchItemWriter<Event> writer() { 

    JdbcBatchItemWriter<Event> writer = new JdbcBatchItemWriter<Event>(); 
    writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Event>()); 
    writer.setSql("insert into EVENTS (event_id, event_title, event_start_date, event_end_date, event_type,need_offline_sync, event_status, created_by, created_date, last_updated_by,last_updated_date) VALUES (:event_id, :event_title, :event_start_date, :event_end_date, :event_type,:need_offline_sync, :event_status, :created_by, :created_date, :last_updated_by, :last_updated_date)"); 
    writer.setDataSource(dataSource2); 
    return writer; 
} 
//Postgres Data source for Target 
@Bean(name="postgresDataSource")  
public DataSource postgresDataSource() throws SQLException { 

    final SimpleDriverDataSource dataSource = new SimpleDriverDataSource();    
    dataSource.setDriver(new org.postgresql.Driver()); 
    dataSource.setUrl("jdbc:postgresql://localhost:5432/batch"); 
    dataSource.setUsername("batch"); 
    dataSource.setPassword("batch");     
    return dataSource; 
} 

//Oracle Data source for source 

@Primary 
@Bean(name="oracleDBDataSource")  
public DataSource oracleDBDataSource() throws SQLException { 

    final SimpleDriverDataSource dataSource = new SimpleDriverDataSource();   
    dataSource.setDriver(new oracle.jdbc.driver.OracleDriver()); //(new oracle.jdbc.pool.OracleDataSource()); 
    dataSource.setUrl("jdbc:oracle:thin:@xxxxxxx.com:1521/xxxxx"); 
    dataSource.setUsername("xxxx"); 
    dataSource.setPassword("xxxxx"); 
    return dataSource; 
} 

@Bean 
public JdbcTemplate jdbcTemplate(final DataSource dataSource) { 

    return new JdbcTemplate(dataSource); 
} 


@Bean 
public JobExecutionListener listener() { 

    return new JobCompletionNotificationListener(new JdbcTemplate(dataSource2)); 
} 

// end::listener[] 

// tag::jobstep[] 
@Bean 
public Job importUserJob() { 

    return jobBuilderFactory.get("importUserJob") 
      .incrementer(new RunIdIncrementer()) 
      .listener(listener()) 
      .flow(step1()) 
      .end() 
      .build(); 
} 



@Bean 
public Step step1() { 

    return stepBuilderFactory.get("step1") 
      .<Event, Event> chunk(10) 
      .reader(reader()) 
      .processor(processor()) 
      .writer(writer()) 
      .build(); 
} 
// end::jobstep[] 

}

+0

在應用標籤之前,請閱讀標籤信息! * spring *不是*批處理文件*! – aschipfl

+0

謝謝,編輯標籤到spring-batch – pstimeforj

回答

0

你提的問題非常廣泛,幾乎未見研究工作。不過,可以在彈簧批處理作業中配置數據庫讀取器和數據庫書寫器。

您必須配置兩個數據源(源數據源是您的Teradata DB,目標數據源是您的Postgres數據庫)。看到這個問題和答案信息如何使用彈簧批量來做到這一點:Use of multiple DataSources in Spring Batch

然後,你必須創建一個閱讀器與源數據源和一個作家與目標數據源。

略調整標準Spring Batch的引導教程,您的情況:

@Configuration 
@EnableBatchProcessing 
public class BatchConfiguration { 

    @Autowired 
    public JobBuilderFactory jobBuilderFactory; 

    @Autowired 
    public StepBuilderFactory stepBuilderFactory; 

    @Autowired 
    @Qualifier("source") 
    public DataSource source; 

    @Autowired 
    @Qualifier("target") 
    public DataSource target; 

    @Bean 
    public ItemReader<Person> reader() { 
     JdbcCursorItemReader<Person> reader = new JdbcCursorItemReader<Person>(); 
     String sql = "select * from Person"; 
     reader.setSql(sql); 
     reader.setDataSource(source); 
     reader.setRowMapper(rowMapper()); 
     return reader; 
    }  

    @Bean 
    public JdbcBatchItemWriter<Person> writer() { 
     JdbcBatchItemWriter<Person> writer = new JdbcBatchItemWriter<Person>(); 
     writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Person>()); 
     writer.setSql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)"); 
     writer.setDataSource(target); 
     return writer; 
    }   

    @Bean 
    public Job yourJob() { 
     return jobBuilderFactory.get("yourJob") 
       .incrementer(new RunIdIncrementer())      
       .flow(step1()) 
       .end() 
       .build(); 
    } 

    @Bean 
    public Step step1() { 
     return stepBuilderFactory.get("step1") 
       .<Person, Person> chunk(10) 
       .reader(reader())      
       .writer(writer()) 
       .build(); 
    }  
} 

在這個例子中的其餘配置請結帳https://spring.io/guides/gs/batch-processing/和更詳細的解釋結帳本教程http://www.codingpedia.org/ama/spring-batch-tutorial-with-spring-boot-and-java-configuration/(你可以找到一個例子RowMapper也在那裏)

+0

感謝您的迴應。我更擔心從Spring批次(依賴關係等)連接到Teradata DB服務器並擁有多個數據源。在開始使用Teradata DB之前,我試圖讓多數據源作業與Oracle數據庫一起作爲源和Postgres數據庫作爲目標。我遵循了多源Spring批處理場景中提到的不同建議。我仍然有錯誤。將更新代碼和錯誤。如果您發現任何問題,請告訴我。 – pstimeforj