简体   繁体   English

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

[英]Parsing a fixed format flat file using Spring or Java 8

So i have this flat file which i need to parse to get the various fields located within it. 所以我有一个平面文件,我需要对其进行分析以获取位于其中的各个字段。 For example Position 0 to 2 is a header literal, position 3 to 10 is Name and so on. 例如,位置0到2是标题文字,位置3到10是Name,依此类推。 I'm having a hard time trying to figure out how to go about this. 我很难弄清楚该如何解决。 Is there anything in Spring Batch that i can use to parse this fixed length file? Spring Batch中有什么我可以用来解析此固定长度文件的内容吗? Sample flat file is given below: 平面文件示例如下:

H10000001140658760000       16778816471A          422              
I10000001140658760IT2016060310850028010700030719564-010               L00000072 

So in the above format, 因此,以上述格式,
H1 is the row literal, 0000001140658760 is the receipt number, H1是行文字, 0000001140658760是收据编号,
LMS is the supplier code and so on. LMS是供应商代码,依此类推。
I need to map these to various fields like 我需要将它们映射到各个领域,例如

  • position 0 to position 2 is the Literal, 位置0到位置2是文字,
  • position 3 to position 18 is receipt number, 位置18的位置3是收据编号,
  • position 18 to position 28 is supplier code. 从位置18到位置28是供应商代码。

Please advise. 请指教。

So i tried using BeanIO with Spring Batch Integration but i'm getting an error while trying to create the reader. 所以我尝试将BeanIO与Spring Batch Integration结合使用,但是在尝试创建阅读器时遇到错误。
I'm getting org.springframework.batch.item.ItemStreamException: Failed to initialize the reader. 我收到org.springframework.batch.item.ItemStreamException:无法初始化阅读器。 Below is my code: 下面是我的代码:

    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;
}

Below is the mapping file i'm using: 以下是我正在使用的映射文件:

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

Here's the Stack Trace: 这是堆栈跟踪:

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

You can do it by org.beanio.spring.BeanIOFlatFileItemReader 您可以通过org.beanio.spring.BeanIOFlatFileItemReader做到这org.beanio.spring.BeanIOFlatFileItemReader

batch-context.xml batch-context.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>

mapping.xml mapping.xml

<stream name="flatFileStream" format="fixedlength">
<record name="flatFileContents" class="com.test.YourPojo" order="1">
    <field name="header" length="2" />
    <field name="name" length="15" />
    <field name="supplierCode" length="10" />
</record>

com.test.YourPojo is the domain model. com.test.YourPojo是域模型。

Note : length in mapping.xml will vary according to your requirement. 注意:mapping.xml中的长度将根据您的要求而变化。

Spring Batch provides the FixedLengthTokenizer for this very use case. Spring Batch为此一个用例提供了FixedLengthTokenizer You can read more about it in the documentation here: http://docs.spring.io/spring-batch/apidocs/org/springframework/batch/item/file/transform/FixedLengthTokenizer.html 您可以在以下文档中阅读有关此内容的更多信息: http : //docs.spring.io/spring-batch/apidocs/org/springframework/batch/item/file/transform/FixedLengthTokenizer.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM