转换CSV文件与Apache骆驼JSON文件通过地图一览 [英] Convert CSV file to JSON file with Apache Camel via List of Maps

查看:143
本文介绍了转换CSV文件与Apache骆驼JSON文件通过地图一览的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的目标是学习如何阅读CSV列出的地图,那么如何将其编组为JSON。

The goal is to learn how to read CSV to List of Maps, then how to marshal it to JSON.

有一次,我知道如何做到这一点,我会如何定义更多有用的途径。

Once I understand how to do it, I will get how to define more useful routes.

我用XML来定义路线,另一个限制是不创造任何改造大豆,但只使用现有的组件。

I use XML to define routes, another restriction is not to create any transformation beans, but use only the existing components.

我的理解是obvoiusly缺少一些概念。据我所知,你必须提供一个bean作为一个消费者,那么你可以将它传递;但该医生说有什么错的地图列表的CSV DATAFORMAT使用?

My understanding is obvoiusly lacks some concept. I understand that you have to provide a bean as a consumer, then you may pass it on; but what's wrong with the List of Maps that the doc says the csv dataformat uses?

    <dataFormats>
        <json id="jack" library="Jackson"/>
    </dataFormats>  

    <route>
        <from uri="file:///C:/tries/collApp/exchange/in?fileName=registerSampleSmaller.csv"/>
        <unmarshal>
            <csv />
        </unmarshal>            
        <marshal ref="jack">                
        </marshal>
        <to uri="file:///C:/tries/collApp/exchange/out?fileName=out.json"/>          
    </route>

默默地什么都不做。我只能看到锁文件是如何出现和消失。

silently does nothing. I can only see how the lock file appears and disappears.

谢谢!

PS /我期待着建立两条路线时,首先会读取CSV,转换 - 塑造它的平整自然,我的执着豆,比它传递给我的豆子。而第二个只会拯救我的豆子为JSON,似乎是一个容易的部分;但我首先需要做到这一点,了解它是如何工作

ps/ I am looking forward to create two routes, the first will read a csv, transform it - shaping it's flat nature to that of my persistent beans, than pass it to my beans. And the second will just save my beans as json, seems to be an easy part; but I first need to do this to understand how it works

推荐答案

我提供的,因为我是向前发展的答案。

I am providing an answer as I have moved forward.

我是在正确的轨道上,有只小错误。一人被热雷米的B评论到原始问题的注意。

I was on the right track, there were just small errors. One was noticed by Jérémie B in comments to an original questions.

这是因为我没有启用日志记录默默失败了,我做到了在我的pom.xml添加SLF4J是这样的:

It failed silently because I haven't enabled logging, I did it by adding slf4j like this in my pom.xml:

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${slf4j-version}</version>
    </dependency>    
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-jdk14</artifactId>
        <version>${slf4j-version}</version>
    </dependency>    
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>${slf4j-version}</version>
    </dependency>    

我看到了许多错误,甚至骆驼错误行为,但我必须设法使这条路线的工作:

I saw numerous errors, and even Camel buggy behaviour, but I have manages to make this route work:

    <dataFormats>
        <json id="jack" library="Jackson" prettyPrint="true"/>
    </dataFormats>       

    <route>

        <from uri="file:///C:/tries/collApp/exchange/in?fileName=registerSampleUtf.csv&amp;charset=UTF-8"/>
        <log message="file: ${body.class.name} ${body}" loggingLevel="WARN"/>
        <unmarshal>
            <csv delimiter=";"  useMaps="true" />
        </unmarshal>           
        <log message="unmarshalled: ${body.class.name} ${body}" loggingLevel="WARN"/>
        <marshal ref="jack"/>
        <log message="marshalled: ${body}" loggingLevel="WARN"/>
        <to uri="file:///C:/tries/collApp/exchange/out?fileName=out.json"/>         
    </route>

所以基本上,清洁错别字我不得不在

So basically, after cleaning typos I had to


  • 指定输入文件的字符集,

  • specify input file charset,

指定一个分隔符是Excel中用于创建我的CSV,

specify a delimiter that Excel used to create my csv,

告诉把它在地图上。

不幸的是这个特殊的code不起作用,可能是因为这是我给开发商comunity报道(无反应,但仍,<一个骆驼的bug href=\"http://camel.465427.n5.nabble.com/A-possible-bug-in-IOConverter-with-Win-1251-charset-td5778665.html\" rel=\"nofollow\">http://camel.465427.n5.nabble.com/A-possible-bug-in-IOConverter-with-Win-1251-charset-td5778665.html)

Unfortunately this particular code doesn't work, possibly due to a Camel bug which I reported to the developer comunity (no reaction yet still, http://camel.465427.n5.nabble.com/A-possible-bug-in-IOConverter-with-Win-1251-charset-td5778665.html)

虽然我向前移动,可能现在我绕过有缺陷的骆驼IOConverter,目前我在这个舞台上(这是不是一个问题的答案,只是为信息,骆驼怎么方便就可以):

Though I moved forward, probably now I am bypassing the flawed Camel's IOConverter, and currently I am on this stage (this is not as an answer to the question, just for the info, how handy Camel can be):

    <route>
        <from uri="file:///C:/tries/collApp/exchange/in?fileName=registerSampleSmaller1.csv&amp;charset=windows-1251"/>
        <split streaming="true">
            <method ref="csvSplitter" method="tokenizeReader"/>  <!-- aprepends the first line of file for every subsequent line -->
            <log message="splitted: ${body}" loggingLevel="DEBUG"/>
            <unmarshal>
                <csv delimiter=";"  useMaps="true" />
            </unmarshal>            
            <log message="unmarshalled: size: ${body.size()}, ${body}" loggingLevel="DEBUG"/>
            <filter>
                <simple>${body.size()} == 1</simple><!-- be sure to have spaces around an operator -->
                <log message="filtered: listItem: ${body[0]['PATRONYMIC']}, list: ${body}" loggingLevel="DEBUG"/>
                <transform>
                    <spel>#{
                        {
                        lastName:body[0]['LAST_NAME'],
                        firstName: body[0]['FIRST_NAME'],
                        patronymic: body[0]['PATRONYMIC'],
                        comment:body[0]['COMMENT6']
                        }
                        }</spel><!-- split the spel {:} map creation notation in multiline is crucial-->
                </transform>                
                <log message="transformed: ${body}" loggingLevel="DEBUG"/>
                <marshal ref="jack"/>
                <log message="marshalled: ${body}" loggingLevel="DEBUG"/>
                <to uri="file:///C:/tries/collApp/exchange/out?fileName=out${exchangeProperty.CamelSplitIndex}.json"/>          
            </filter>
        </split>
    </route>

我不得不写我自己的CSV分路器(对于所有的Uni code codepoints等),这基本上是将第一行的所有后续行,但现在我能CSV分成在streamish方式集JSONs的,或处理不同的对象,而不是编组。

I had to write my own CSV splitter (with respect to all Unicode codepoints etc), which is basically adds the first lines to all subsequent lines, but now I am able to split CSV into a set of JSONs in a streamish manner, or handle objects differently instead of marshalling.

这篇关于转换CSV文件与Apache骆驼JSON文件通过地图一览的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆