3.3. Data Grid コンソールと CLI からのキャッシュのクエリー

Data Grid コンソールと Data Grid コマンドラインインターフェイス (CLI) を使用すると、インデックス付きおよびインデックスなしのリモートキャッシュをクエリーできます。また、任意の HTTP クライアントを使用して、RESTAPI を介してキャッシュにインデックスを付けてクエリーを実行することもできます。

この手順では、Person インスタンスを保存するリモートキャッシュをインデックス化してクエリーする方法を説明します。

前提条件

  • 稼働中の Data Grid Server インスタンスが 1 つ以上ある。
  • 作成権限を持つ Data Grid クレデンシャルを持っている。

手順

  1. 以下の例のように、インデックスアノテーションを Protobuf スキーマに追加します。

    package org.infinispan.example;
    
    /* @Indexed */
    message Person {
    
        /* @Basic */
        optional int32 id = 1;
    
        /* @Keyword(projectable = true) */
        required string name = 2;
    
        /* @Keyword(projectable = true) */
        required string surname = 3;
    
        /* @Basic(projectable = true, sortable = true) */
        optional int32 age = 6;
    
    }

    Data Grid CLI で、以下のように --upload= 引数を指定して schema コマンドを使用します。

    schema --upload=person.proto person.proto
  2. ProtoStream エンコーディングを使用する people という名前のキャッシュを作成し、Data Grid が Protobuf スキーマで宣言されたエンティティーに設定します。

    以下のキャッシュは、直前の手順で Person エンティティーをインデックス化します。

    <distributed-cache name="people">
      <encoding media-type="application/x-protostream"/>
      <indexing>
        <indexed-entities>
          <indexed-entity>org.infinispan.example.Person</indexed-entity>
        </indexed-entities>
      </indexing>
    </distributed-cache>

    CLI で、以下のように --file= 引数を指定して create cache コマンドを使用します。

    create cache --file=people.xml people
  3. キャッシュにエントリーを追加します。

    リモートキャッシュをクエリーするには、一部のデータが含まれている必要があります。以下の手順例では、以下の JSON 値を使用するエントリーを作成します。

    PersonOne

    {
      "_type":"org.infinispan.example.Person",
      "id":1,
      "name":"Person",
      "surname":"One",
      "age":44
    }

    PersonTwo

    {
      "_type":"org.infinispan.example.Person",
      "id":2,
      "name":"Person",
      "surname":"Two",
      "age":27
    }

    PersonThree

    {
      "_type":"org.infinispan.example.Person",
      "id":3,
      "name":"Person",
      "surname":"Three",
      "age":35
    }

    CLI で、以下のように put コマンドを使用して、--file= 引数を指定して各エントリーを追加します。

    put --encoding=application/json --file=personone.json personone
    ヒント

    Data Grid Console から、カスタムタイプを使用して JSON 形式で値を追加する際に、Value content type フィールドの Custom Type を選択する必要があります。

  4. リモートキャッシュをクエリーします。

    CLI で、リモートキャッシュのコンテキストで query コマンドを使用します。

    query "from org.infinispan.example.Person p WHERE p.name='Person' ORDER BY p.age ASC"

    クエリーは昇順で Person と一致する名前を持つすべてのエントリーを返します。

関連情報