使用Spring或Java 8解析固定格式的平面文件

时间:2016-06-28 21:49:05

标签: java spring parsing spring-batch

所以我有这个平面文件,我需要解析它以获取其中的各个字段。例如,位置0到2是标题文字,位置3到10是名称,依此类推。 我很难弄清楚如何解决这个问题。 Spring Batch中有什么东西可以用来解析这个固定长度的文件吗? 样本平面文件如下:

H10000001140658760000       16778816471A          422              
I10000001140658760IT2016060310850028010700030719564-010               L00000072 

所以在上面的格式中,
H1是行文字,0000001140658760是收据编号,
LMS是供应商代码,依此类推 我需要将这些映射到各种领域,如

  • 位置0到位置2是Literal,
  • 位置3到位置18是收据编号,
  • 位置18至位置28是供应商代码。

请告知。

所以我尝试将BeanIO与Spring Batch Integration结合使用,但在尝试创建阅读器时遇到错误 我收到org.springframework.batch.item.ItemStreamException:无法初始化阅读器。 以下是我的代码:

    public BeanIOFlatFileItemReader<String> createBeanIOFlatFileItemReader(File file) throws Exception {
    BeanIOFlatFileItemReader<String> reader = new BeanIOFlatFileItemReader<>();

    reader.setStreamFactory(StreamFactory.newInstance());
    reader.setResource(new FileSystemResource(file));
    reader.setStreamMapping(new ClassPathResource("mapping.xml"));
    reader.setStreamName("file");//this name shud be same as the name of the stream in the mapping.xml
    reader.afterPropertiesSet();// Adding this solved the issue!
    reader.open(new ExecutionContext());
    return reader;
}

以下是我正在使用的映射文件:

<beanio xmlns="http://www.beanio.org/2012/03" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.beanio.org/2012/03 http://www.beanio.org/2012/03/mapping.xsd">

 <stream name="file" format="fixedlength" minOccurs="0" maxOccurs="unbounded">
<record name="employee" class="Employee"  order="1">
</record>
<record name="company"  class="Company"  order="2">
</record>
 </stream>
</beanio>

这是堆栈跟踪:

org.springframework.batch.item.ItemStreamException: Failed to initialize the reader
at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:147)
at com.gateway.fileloader.fileprocessor.ArchiveFileProcessor.createBeanIOFlatFileItemReader(ArchiveFileProcessor.java:211)
at com.gateway.fileloader.fileprocessor.ArchiveFileProcessor.doProcess(ArchiveFileProcessor.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.expression.spel.support.ReflectiveMethodExecutor.execute(ReflectiveMethodExecutor.java:113)
at org.springframework.expression.spel.ast.MethodReference.getValueInternal(MethodReference.java:129)
at org.springframework.expression.spel.ast.MethodReference.access$000(MethodReference.java:49)
at org.springframework.expression.spel.ast.MethodReference$MethodValueRef.getValue(MethodReference.java:347)
at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:88)
at org.springframework.expression.spel.ast.SpelNodeImpl.getTypedValue(SpelNodeImpl.java:131)
at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:330)
at org.springframework.integration.util.AbstractExpressionEvaluator.evaluateExpression(AbstractExpressionEvaluator.java:166)
at org.springframework.integration.util.MessagingMethodInvokerHelper.processInternal(MessagingMethodInvokerHelper.java:317)
at org.springframework.integration.util.MessagingMethodInvokerHelper.process(MessagingMethodInvokerHelper.java:155)
at org.springframework.integration.handler.MethodInvokingMessageProcessor.processMessage(MethodInvokingMessageProcessor.java:93)
at org.springframework.integration.handler.ServiceActivatingHandler.handleRequestMessage(ServiceActivatingHandler.java:89)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:99)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:127)
at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:116)
at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:147)
at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:120)
at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:77)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:442)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:392)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:115)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:45)
at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:105)
at org.springframework.integration.endpoint.SourcePollingChannelAdapter.handleMessage(SourcePollingChannelAdapter.java:161)
at org.springframework.integration.endpoint.AbstractPollingEndpoint.doPoll(AbstractPollingEndpoint.java:251)
at org.springframework.integration.endpoint.AbstractPollingEndpoint.access$000(AbstractPollingEndpoint.java:57)
at org.springframework.integration.endpoint.AbstractPollingEndpoint$1.call(AbstractPollingEndpoint.java:176)
at org.springframework.integration.endpoint.AbstractPollingEndpoint$1.call(AbstractPollingEndpoint.java:173)
at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller$1.run(AbstractPollingEndpoint.java:330)
at org.springframework.integration.util.ErrorHandlingTaskExecutor$1.run(ErrorHandlingTaskExecutor.java:55)
at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)
at org.springframework.integration.util.ErrorHandlingTaskExecutor.execute(ErrorHandlingTaskExecutor.java:51)
at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller.run(AbstractPollingEndpoint.java:324)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

Caused by: java.lang.IllegalArgumentException: No stream mapping configured for name 'fileDummy'
at org.beanio.internal.DefaultStreamFactory.getStream(DefaultStreamFactory.java:129)
at org.beanio.internal.DefaultStreamFactory.createReader(DefaultStreamFactory.java:70)
at org.beanio.spring.BeanIOFlatFileItemReader.doOpen(BeanIOFlatFileItemReader.java:129)
at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:144)
... 48 more

2 个答案:

答案 0 :(得分:3)

您可以INSERT INTO other_table (name, age, sex, city, id, number, nationality) SELECT name, age, sex, city, p.id, number, n.nationality FROM table_1 p INNER JOIN table_2 a ON a.id = p.id INNER JOIN table_3 b ON b.id = p.id ... INNER JOIN table_n x ON x.id = p.id

执行此操作

分批context.xml中

org.beanio.spring.BeanIOFlatFileItemReader

mapping.xml

<bean id="fixedWidthFileReader" class="org.beanio.spring.BeanIOFlatFileItemReader" scope="step">
    <property name="streamMapping" value="classpath:META-INF/mapping.xml"/>
    <property name="streamName" value="flatFileStream" />
    <property name="resource" value="resource/inputFile"/></bean>

com.test.YourPojo是域模型。

注意:mapping.xml中的长度将根据您的要求而有所不同。

答案 1 :(得分:1)

Spring Batch为这个用例提供了printing data in console ["test.txt", "test1.txt", "test2.txt", "test3.txt", "test4.txt"] 。您可以在此处的文档中了解有关它的更多信息:http://docs.spring.io/spring-batch/apidocs/org/springframework/batch/item/file/transform/FixedLengthTokenizer.html