2019年1月31日木曜日

atlasmap.io に注目! (後編)

2018年12月21日に data mapperを使いマッピングをデザインしてから1ヵ月以上が経過していました。。
大変遅れてしまいましたが、やっとマッピングしたものを動かす記事を書きます。

日進月歩の勢いで開発が進んでいるプロジェクトなので、現時点での改善点をいくつかご紹介します。
 1: 1.39.0 から、maven repositoryで atlasmap-standalone jar が公開されました。
   http://central.maven.org/maven2/io/atlasmap/atlasmap-standalone/1.39.0/atlasmap-standalone-1.39.0.jar
   java -jar atlasmap-standalone-1.39.0.jar で web ui が起動します。ポート番号は前回と同様に 8585 です。

 2: camel-atlasmap コンポーネントを動かす為の修正や改善が複数入りました。コミュニティメンバーが親切に対応してくださったおかげで、使い易くなっています。
    https://github.com/atlasmap/atlasmap/issues/712
    https://github.com/atlasmap/atlasmap/issues/713
    https://github.com/atlasmap/atlasmap/issues/729

今回の動作検証を通し、不具合についてコミュニティメンバーへインプットしました。メンバーはどなたも親切で、何かインプットしてから製品に反映されるまでの速さにはとても驚きました。ディスカッションを通して内部構造を知るきっかけにもなり 良い事尽くしだったので、ぜひ皆さんもコミュニティーへ活発に参加されてみてはいかがでしょうか。

話がそれましたが、いよいよ本題です。今回は camel-atlasmap を使い、データ変換を試してみます。ランタイムは springbootを使います。
(捕捉: karaf愛好家の方は、こちらのチケットを watchしてください。 https://github.com/atlasmap/atlasmap/issues/68)

atlasmap git repository を最新の状態にし、ビルドしてください。

$ cd ${ATLASMAP}
$ git pull
$ ./mvnw clean install -DskipTests

お手数ですが、再度前回の投稿で実施したマッピング作業をし、設定ファイルをエクスポートしてください。 issues/713 の修正を使った設定ファイルが必要だからです。

Red Hat Developer Studio(Eclipse) のウィザードで、新規 Camel プロジェクトを作成します。ランタイムに springboot を選択し、Fuseのバージョンには 2.21.0.fuse-720050-redhat-00001 を指定しました。
pom.xml に camel-atlasmap の dependency を追記します。

<dependency>
  <groupId>io.atlasmap</groupId>
  <artifactId>camel-atlasmap</artifactId>
  <version>1.40.0-SNAPSHOT</version>
</dependency>

オンライン リポジトリーには 1.40.0-SNAPSHOT が公開されていないので、ローカルリポジトリを参照する必要があります。

データ変換を試す為に、簡単な camel routeを作成しました。タイマーを使い1回メッセージを生成し、サンプル用の pojoを挿入後、camel-atlasmap で xmlに変換しています。変換前/変換後のデータはログから確認できます。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans.xsd  http://camel.apache.org/schema/spring  http://camel.apache.org/schema/spring/camel-spring.xsd">
    <bean class="sample.Product" id="sampleProduct">
        <property name="id">
            <value>1</value>
        </property>
        <property name="price">
            <value>1000</value>
        </property>
    </bean>
    <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
        <route id="simple-route">
            <from id="route-timer" uri="timer:timerName?repeatCount=1"/>
            <log id="route-log1" message="hello world"/>
            <setBody id="route-setBody">
                <simple>${ref:sampleProduct}</simple>
            </setBody>
            <log id="route-log2" message="Before Mapping >>> ${body}"/>
            <to id="_to1" uri="atlas:atlasmap-mapping.adm"/>
            <log id="route-log3" message="After Mapping >>> ${body}"/>
        </route>
    </camelContext>
</beans>

camel-atlasmap の定義はこの箇所です。

<to id="_to1" uri="atlas:atlasmap-mapping.adm"/>

出力した atlasmap-mapping.adm を指定しています。atlasmap-mapping.adm は、$PROJECT/src/main/resources 配下に設置しています。

プロジェクトを実行すると、このようなログが出力されるはずです。(ログは一部を抜粋しています) XMLに変換されている事が確認できます。

14:08:33.813 [main] INFO  org.mycompany.Application - Started Application in 4.41 seconds (JVM running for 4.691)
14:08:34.763 [Camel (MyCamel) thread #1 - timer://timerName] INFO  simple-route - hello world
14:08:34.768 [Camel (MyCamel) thread #1 - timer://timerName] INFO  simple-route - Before Mapping >>> sample.Product@5ed4bc
14:08:35.036 [Camel (MyCamel) thread #1 - timer://timerName] WARN  o.a.c.c.atlasmap.AtlasEndpoint - There's no source document with docId='JSONInstanceSource', returning default: docId='null', path='null'
14:08:35.037 [Camel (MyCamel) thread #1 - timer://timerName] WARN  o.a.c.c.atlasmap.AtlasEndpoint - Null or non-String source document: docId='JSONInstanceSource': docId='JSONInstanceSource', path='null'
14:08:35.037 [Camel (MyCamel) thread #1 - timer://timerName] WARN  o.a.c.c.atlasmap.AtlasEndpoint - There's no source document with docId='JSONSchemaSource', returning default: docId='null', path='null'
14:08:35.037 [Camel (MyCamel) thread #1 - timer://timerName] WARN  o.a.c.c.atlasmap.AtlasEndpoint - Null or non-String source document: docId='JSONSchemaSource': docId='JSONSchemaSource', path='null'
14:08:35.037 [Camel (MyCamel) thread #1 - timer://timerName] WARN  o.a.c.c.atlasmap.AtlasEndpoint - There's no source document with docId='XMLSchemaSource', returning default: docId='null', path='null'
14:08:35.037 [Camel (MyCamel) thread #1 - timer://timerName] WARN  o.a.c.c.atlasmap.AtlasEndpoint - Null or non-String source document: docId='XMLSchemaSource': docId='XMLSchemaSource', path='null'
14:08:35.037 [Camel (MyCamel) thread #1 - timer://timerName] WARN  o.a.c.c.atlasmap.AtlasEndpoint - There's no source document with docId='sample.Product', returning default: docId='null', path='null'
14:08:35.037 [Camel (MyCamel) thread #1 - timer://timerName] WARN  o.a.c.c.atlasmap.AtlasEndpoint - There's no source document with docId='XMLInstanceSource', returning default: docId='null', path='null'
14:08:35.037 [Camel (MyCamel) thread #1 - timer://timerName] WARN  o.a.c.c.atlasmap.AtlasEndpoint - Null or non-String source document: docId='XMLInstanceSource': docId='XMLInstanceSource', path='null'
14:08:35.037 [Camel (MyCamel) thread #1 - timer://timerName] WARN  o.a.c.c.atlasmap.AtlasEndpoint - No target document created for DataSource:[id=JSONInstanceTarget, uri=atlas:json:JSONInstanceTarget]: docId='JSONInstanceTarget', path='null'
14:08:35.037 [Camel (MyCamel) thread #1 - timer://timerName] WARN  o.a.c.c.atlasmap.AtlasEndpoint - No target document created for DataSource:[id=JSONSchemaTarget, uri=atlas:json:JSONSchemaTarget]: docId='JSONSchemaTarget', path='null'
14:08:35.039 [Camel (MyCamel) thread #1 - timer://timerName] INFO  simple-route - After Mapping >>> {JSONInstanceTarget=null, JSONSchemaTarget=null, XMLInstanceTarget=<?xml version="1.0" encoding="UTF-8" standalone="no"?>, sample=<?xml version="1.0" encoding="UTF-8" standalone="no"?><request><id>1</id><price>1000</price></request>, XMLSchemaTarget=<?xml version="1.0" encoding="UTF-8" standalone="no"?>}

ぜひお試しいただき、コミュニティーへフィードバックしていただければと思います。こちらへコメントしていただければ、自分が代理でコミュニティーへインプットすることもできます。お気軽にお知らせください。
引き続きよろしくお願い致します。