第5章 既存の Kamelet カタログのカスタマイズ


このドキュメントでは、既存の kamelet のカスタマイズについて説明します。これには次の段階が含まれます。

  1. シンプルな kamelet の作成
  2. 既存の kamelet カタログのカスタマイズ
注記

Red Hat は、独自の kamelet をゼロから実装することをサポートしていません。

5.1. シンプルな Kamelet の作成

Apache Camel は 300 超のコンポーネントを提供しており、すでに利用可能なコンポーネントの 1 つから始めることで、Kamelet を簡単に作成できます。公式カタログで入手可能な Kamelet のほとんどは、Kamelet のプロパティーを Camel エンドポイントのパラメーターに再マッピングしただけのシンプルなものです。

Twitter 上のデータを検索して、特定のキーワードに関する情報のストリームを提供できる Kamelet を提供する次の例を考えてみましょう。このような Kamelet を作成するには、camel-twitter コンポーネントのオプションを修正せずに使用できます。

  1. シンプルな Kamelet を作成するには、Kamelet CLI を使用して新しい kamel リソースの構築を開始します。

    kamel init twitter-search-source.kamelet.yaml
    Copy to Clipboard Toggle word wrap

    これにより、次のような YAML ファイルが生成されます。

    twitter-search-source.kamelet.yaml

    apiVersion: camel.apache.org/v1alpha1
    kind: Kamelet
    metadata:
      name: twitter-search-source
      labels:
        camel.apache.org/kamelet.type: "source"
    spec:
      definition:
        title: "Timer"
        description: "Produces periodic events with a custom payload"
        required:
          - message
        properties:
          period:
            title: Period
            description: The time interval between two events
            type: integer
            default: 1000
          message:
            title: Message
            description: The message to generate
            type: string
      types:
        out:
          mediaType: text/plain
      template:
        from:
          uri: timer:tick
          parameters:
            period: "{{period}}"
          steps:
            - set-body:
                constant: "{{message}}"
            - to: "kamelet:sink"
    Copy to Clipboard Toggle word wrap

  2. ファイルを変更して、Twitter 上で指定されたキーワードを検索するルートを作成します。

    初期スキャフォールディング (timer-to-log) で提供されたルートは不正確であるため、次のように変更します。

    twitter-search-source.kamelet.yaml

    apiVersion: camel.apache.org/v1alpha1
    kind: Kamelet
    # ...
    spec:
    # ...
      template:
        from:
          uri: "twitter-search:{{keywords}}" (1)
          parameters:
            accessToken: "{{accessToken}}" (2)
            accessTokenSecret: "{{accessTokenSecret}}"
            consumerKey: "{{apiKey}}" (3)
            consumerSecret: "{{apiKeySecret}}"
          steps:
          - marshal: (4)
              json: {}
          - to: "kamelet:sink" (5)
    Copy to Clipboard Toggle word wrap

  3. keywords は、Camel Twitter-search のパスパラメーターです。一部のエンドポイントパラメーターは 1 対 1 でマッピングされています。Camel コンポーネント ConsumerKey には、Twitter 開発者ポータルでの実際の名前を反映するために apiKey という名前が付けられています。
  4. Camel Twitter コンポーネントは Java オブジェクトを生成します。それらを JSON にマーシャリングする必要があります。ソース Kamelet は、実行時に別のターゲットに置き換えられる特別なエンドポイント "kamelet:sink" にデータを送信します。
  5. 上記の YAML ルートテンプレートは、twitter-search コンポーネントを使用して Twitter 上を検索します。Kamelet の出力にはネットワーク経由で転送する値が必要であるため、JSON にマーシャリングステップを追加しました。Kamelet を完成させるには、パラメーターを JSON スキーマ形式で文書化する必要があります。これは spec definition 部分で指定されます。

    twitter-search-source.kamelet.yaml

    apiVersion: camel.apache.org/v1alpha1
    kind: Kamelet
    metadata:
      name: twitter-search-source
    # ...
    spec:
      definition:
        title: "Twitter Search Source" (1)
        description: |-
          Allows to get all tweets on particular keywords from Twitter.
    
          It requires tokens that can be obtained by creating an application
          in the Twitter developer portal: https://developer.twitter.com/.
        required: (2)
        - keywords
        - apiKey
        - apiKeySecret
        - accessToken
        - accessTokenSecret
        properties:
          keywords: (3)
            title: Keywords
            description: The keywords to use in the Twitter search (Supports Twitter standard operators)
            type: string
            example: "Apache Camel"
          apiKey:
            title: API Key
            description: The API Key from the Twitter application in the developer portal
            type: string
            format: password
            x-descriptors:
            - urn:alm:descriptor:com.tectonic.ui:password (4)
          apiKeySecret:
            title: API Key Secret
            description: The API Key Secret from the Twitter application in the developer portal
            type: string
            format: password
            x-descriptors:
            - urn:alm:descriptor:com.tectonic.ui:password
          accessToken:
            title: Access Token
            description: The Access Token from the Twitter application in the developer portal
            type: string
            format: password
            x-descriptors:
            - urn:alm:descriptor:com.tectonic.ui:password
          accessTokenSecret:
            title: Access Token Secret
            description: The Access Token Secret from the Twitter application in the developer portal
            type: string
            format: password
            x-descriptors:
            - urn:alm:descriptor:com.tectonic.ui:password
    # ...
    Copy to Clipboard Toggle word wrap

Kamelet に関するテキスト形式の一般情報は、以下のとおりです。

  1. Kamelet を作成するために必要なパラメーターのリスト
  2. 各パラメーターの仕様 (フラット構造で、ネストされたオプションは許可されない)
  3. 特定の UI (OpenShift コンソール) 向けのオプションでのグラフィカルカスタマイズ

    最終的な Kamelet は次のようになります。

    twitter-search-source.kamelet.yaml

    apiVersion: camel.apache.org/v1alpha1
    kind: Kamelet
    metadata:
      name: twitter-search-source
      annotations:
        camel.apache.org/kamelet.icon: "data:image/svg+xml;base64,..." # Truncated
        camel.apache.org/provider: "Apache Software Foundation"
      labels:
        camel.apache.org/kamelet.type: "source"
        camel.apache.org/kamelet.group: "Twitter"
    spec:
      definition:
        title: "Twitter Search Source"
        description: |-
          Allows to get all tweets on particular keywords from Twitter.
    
          It requires tokens that can be obtained by creating an application
          in the Twitter developer portal: https://developer.twitter.com/.
        required:
        - keywords
        - apiKey
        - apiKeySecret
        - accessToken
        - accessTokenSecret
        properties:
          keywords:
            title: Keywords
            description: The keywords to use in the Twitter search (Supports Twitter standard operators)
            type: string
            example: "Apache Camel"
          apiKey:
            title: API Key
            description: The API Key from the Twitter application in the developer portal
            type: string
            format: password
            x-descriptors:
            - urn:alm:descriptor:com.tectonic.ui:password
          apiKeySecret:
            title: API Key Secret
            description: The API Key Secret from the Twitter application in the developer portal
            type: string
            format: password
            x-descriptors:
            - urn:alm:descriptor:com.tectonic.ui:password
          accessToken:
            title: Access Token
            description: The Access Token from the Twitter application in the developer portal
            type: string
            format: password
            x-descriptors:
            - urn:alm:descriptor:com.tectonic.ui:password
          accessTokenSecret:
            title: Access Token Secret
            description: The Access Token Secret from the Twitter application in the developer portal
            type: string
            format: password
            x-descriptors:
            - urn:alm:descriptor:com.tectonic.ui:password
      types:
        out:
          mediaType: application/json
      template:
        from:
          uri: "twitter-search:{{keywords}}"
          parameters:
            accessToken: "{{accessToken}}"
            accessTokenSecret: "{{accessTokenSecret}}"
            consumerKey: "{{apiKey}}"
            consumerSecret: "{{apiKeySecret}}"
          steps:
          - marshal:
              json: {}
          - to: "kamelet:sink"
    Copy to Clipboard Toggle word wrap

    カタログ上で共有した Kamelet や OpenShift クラスター上に作成した Kamelet を使用できます。

以下に例を示します。

シンプルなバインディングとともにクラスターに適用します。

  1. Camel K オペレーターが動作するには、Openshift クラスターを有効にし、namespace に接続する必要があります。
  2. Kamelet を作成します。

    kubectl apply -f twitter-search-source.kamelet.yaml
    Copy to Clipboard Toggle word wrap
  3. 次の例に示すようにバインディングを作成します。

    twitter-search-source-binding.yaml`

    apiVersion: camel.apache.org/v1alpha1
    kind: KameletBinding
    metadata:
      name: twitter-search-source-binding
    spec:
      source:
        ref:
          kind: Kamelet
          apiVersion: camel.apache.org/v1alpha1
          name: twitter-search-source
        properties:
          keywords: "Apache Camel"
          apiKey: "your own"
          apiKeySecret: "your own"
          accessToken: "your own"
          accessTokenSecret: "your own"
      sink:
        uri: "log:info"
    Copy to Clipboard Toggle word wrap

  4. これは以下を使用して作成できます。

    kubectl apply -f twitter-search-source-binding.yaml
    Copy to Clipboard Toggle word wrap
  5. 作成したら、次を使用してバインディングのログを確認できます。

    kamel logs twitter-search-source-binding
    Copy to Clipboard Toggle word wrap
  6. 上記の手順を実行すると、統合が作成された後に、ログにいくつかのツイートが記録されるはずです。
  7. さまざまなコンテキスト (Knative、Kafka など) での使用方法の詳細は、Kamelets ユーザーガイド を参照してください。
トップに戻る
Red Hat logoGithubredditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。 最新の更新を見る.

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

Theme

© 2025 Red Hat