ヘッダーをスキップ
Oracle® Coherenceチュートリアル
リリース3.6
B61373-01
  ドキュメント・ライブラリへ
ライブラリ
製品リストへ
製品
目次
目次
索引
索引

戻る
戻る
 
次へ
次へ
 

6 キャッシュのコールおよび集計の簡略化

前章では、AlwaysFilterLikeFilterEqualsFilterなど様々なフィルタを使用したファイルQueryExample.javaを作成し、キャッシュから情報を取得して、これを集計しました。またこのファイルでは、様々な専用のValueExtractorsChainedExtractorsKeyExtractorsおよびReflectionExtractors)を採用することによってデータに索引を作成しました。その結果、非常に冗長なJava文が作成されました。これらの文は、QueryHelper APIを使用することで簡略化できます。

この章は次の項で構成されています。

6.1 概要

フィルタ/エクストラクタ文、およびCoherenceキャッシュとの相互作用方法を簡略化するため、最新リリースではQueryHelper APIが提供されています。QueryHelpercom.tangosol.util.QueryHelper)は、FilterおよびValueExtractorのインスタンスをビルドできるcreateFilterおよびcreateExtractorファクトリ・メソッド一式を提供するユーティリティ・クラスです。このクラス内のメソッドは、SQL WHERE句を理解している人であればよく知っている形式で豊富なフィルタの作成を指定するStringを受け入れます。

たとえば、次の文はcreateFilter(String s)を使用してフィルタを作成しています。これは、マサチューセッツに住んでいるが別の州で働いている従業員のフィルタを構築します。

..
QueryHelper.createFilter("homeAddress.state = 'MA' and workAddress.state !='MA'")
...

この文は、次のCoherence APIを使用した同等のフィルタ/エクストラクタ文より単純です。

new AndFilter(new EqualsFilter("getHomeAddress.getState", "MA"), 
      new NotEqualsFilter("getWorkAddress.getState", "MA")) 

詳細は、QueryHelper APIのJavadocを参照してください。Coherence問合せ言語内のWHERE句の構文は、『Oracle Coherence開発者ガイド』のCoherence問合せ言語の使用に関する項を参照してください。

6.2 問合せ例の簡略化

この項では、前章で作成したQueryExample.javaファイルの索引付け、キャッシュ・コールおよび集計を簡略化する方法について説明します。

  1. 静的なクラスとしてQueryHelper APIをインポートします。

      import static com.tangosol.util.QueryHelper.*;
    
  2. ChainedExtractorクラス、KeyExtractorクラスおよびReflectionExtractorクラスのインポートをコメント・アウトします。

  3. AlwaysFilterクラス、AndFilterクラス、EqualsFilterクラス、GreaterFilterクラス、LikeFilterクラスおよびNotEqualsFilterクラスのインポートをコメント・アウトします。

  4. cache.addIndex文で、ReflectionExtractorのインスタンスをQueryHelper APIのcreateExtractorに置き換えます。

    表6-1に、ReflectionExtractorのインスタンスと同等なcreateExtractorを一覧表示します。

    表6-1 ReflectionExtractor文と同等なcreateExtractor文

    ReflectionExtractor文 置換される同等のcreateExtractor文

    cache.addIndex(new ReflectionExtractor("getAge"), true, null);

    cache.addIndex(createExtractor("age"), true, null);

    cache.addIndex(new ChainedExtractor(reflectAddrHome, new ReflectionExtractor("getState")), true, null);

    cache.addIndex(createExtractor("homeAddress.state"), false, null);

    cache.addIndex(new ChainedExtractor(new ReflectionExtractor("getWorkAddress"), new ReflectionExtractor("getState")), true, null);

    cache.addIndex(createExtractor("workAddress.state"),false, null);

    cache.addIndex(new ChainedExtractor(reflectAddrHome, new ReflectionExtractor("getCity")), true, null);

    cache.addIndex(createExtractor("homeAddress.city"), true, null);


  5. setResults文の*Filterメソッドのコールを、適切なCoherence問合せ言語でのcreateFilterのコールに置き換えます。

    表6-2に、*Filterのインスタンスと同等なcreateFilterを一覧表示します。

    表6-2 問合せにおける*Filter文と同等なcreateFilter文

    *Filter文 置換される同等のcreateFilter文

    Set setResults = cache.entrySet(new EqualsFilter("getHomeAddress.getState", "MA"));

    Set setResults = cache.entrySet(createFilter("homeAddress.state = 'MA'"));

    Set setResults = cache.entrySet(new AndFilter(new EqualsFilter("getHomeAddress.getState", "MA"), new NotEqualsFilter("getWorkAddress.getState", "MA")));

    setResults = cache.entrySet( createFilter("homeAddress.state is 'MA' and workAddress is not 'MA'"));

    Set setResults = cache.entrySet(new LikeFilter("getHomeAddress.getCity", "S%"));

    Set setResults = cache.entrySet(createFilter("homeAddress.city like 'S%'"));

    Set setResults = cache.entrySet(new GreaterFilter("getAge", nAge));

    // Initialize nAge and aEnv

    final int nAge = 42;

    Object[] aEnv = new Object[] {new Integer(nAge)};

    ...

    Set setResults = cache.entrySet(createFilter("age > ?1",aEnv));

    Set setResults = cache.entrySet(new AndFilter(new LikeFilter(new KeyExtractor("getLastName"), "S%", (char) 0, false), new EqualsFilter("getHomeAddress.getState", "MA")));

    Set setResults = cache.entrySet(createFilter("lastName like 'S%' and homeAddress.state = 'MA'"));


  6. aggregate文の*Filterメソッドのコールを、適切なCoherence問合せ言語でのcreateFilterのコールに置き換えます。

    表6-3に、*Filterのインスタンスと同等なcreateFilterを一覧表示します。

    表6-3 集計における*Filter文と同等なcreateFilter文

    *Filter文 置換される同等のcreateFilter文

    System.out.println("count > " + nAge + ": "+ cache.aggregate(new GreaterFilter("getAge", nAge), new Count()));

    System.out.println("count > " + nAge + ": " + cache.aggregate(createFilter("age > ?1", aEnv), new Count()));

    System.out.println("min age: " + cache.aggregate(AlwaysFilter.INSTANCE, new LongMin("getAge")));

    Filter always = createFilter("true");

    System.out.println("min age: " + cache.aggregate(always, new LongMin("getAge")));

    System.out.println("avg age: " + cache.aggregate(AlwaysFilter.INSTANCE, new DoubleAverage("getAge")));

    System.out.println("avg age: " + cache.aggregate(always, new DoubleAverage("getAge")));

    System.out.println("max age: " + cache.aggregate(AlwaysFilter.INSTANCE, new LongMax("getAge")));

    System.out.println("max age: " + cache.aggregate(always, new LongMax("getAge")));


コードの置換を終了すると、QueryExample.java例6-1のようになります。

例6-1 編集後のQueryExampleファイル

package com.oracle.handson;
 
import com.tangosol.net.CacheFactory;
import com.tangosol.net.NamedCache;
 
import com.tangosol.util.Filter;
import static com.tangosol.util.QueryHelper.*;
 
import com.tangosol.util.aggregator.Count;
// import com.tangosol.util.extractor.ChainedExtractor;
// import com.tangosol.util.extractor.KeyExtractor;
// import com.tangosol.util.extractor.ReflectionExtractor;
 
// import com.tangosol.util.aggregator.Count;
import com.tangosol.util.aggregator.DoubleAverage;
import com.tangosol.util.aggregator.LongMax;
import com.tangosol.util.aggregator.LongMin;
 
 
// import com.tangosol.util.filter.AlwaysFilter;
// import com.tangosol.util.filter.AndFilter;
// import com.tangosol.util.filter.EqualsFilter;
// import com.tangosol.util.filter.GreaterFilter;
// import com.tangosol.util.filter.LikeFilter;
// import com.tangosol.util.filter.NotEqualsFilter;
 
import java.util.Iterator;
import java.util.Set;
 
 
/**
* QueryExample runs sample queries for contacts.
*
*/
public class QueryExample{
  
    // ----- QueryExample methods ---------------------------------------
 
    public static void main(String[] args) {
      NamedCache cache = CacheFactory.getCache("ContactsCache");
       query(cache);
    }
    /**
    * Perform the example queries
    *
    */
     public static void query(NamedCache cache)
        {
         // Add indexes to make queries more efficient
       // ReflectionExtractor reflectAddrHome =
       //         new ReflectionExtractor("getHomeAddress");
 
        // Add an index for the age
        // cache.addIndex(new ReflectionExtractor("getAge"), true, null);
        cache.addIndex(createExtractor("age"), true, null);
 
        // Add index for state within home address
        // cache.addIndex(new ChainedExtractor(reflectAddrHome,
        //        new ReflectionExtractor("getState")), true, null);
       cache.addIndex(createExtractor("homeAddress.state"), false, null); 
 
        // Add index for state within work address
        // cache.addIndex(new ChainedExtractor(
        //        new ReflectionExtractor("getWorkAddress"),
        //        new ReflectionExtractor("getState")), true, null);
        cache.addIndex(createExtractor("workAddress.state"),false, null); 
 
        // Add index for city within home address
        // cache.addIndex(new ChainedExtractor(reflectAddrHome,
        //        new ReflectionExtractor("getCity")), true, null);
        cache.addIndex(createExtractor("homeAddress.city"), true,  null); 
 
        // Find all contacts who live in Massachusetts
        // Set setResults = cache.entrySet(new EqualsFilter(
        //         "getHomeAddress.getState", "MA"));
           Set setResults = cache.entrySet(createFilter("homeAddress.state = 'MA'")); 
            printResults("MA Residents", setResults); 
 
        // Find all contacts who live in Massachusetts and work elsewhere
        // setResults = cache.entrySet(new AndFilter(
        //        new EqualsFilter("getHomeAddress.getState", "MA"),
        //        new NotEqualsFilter("getWorkAddress.getState", "MA")));
            setResults = cache.entrySet(createFilter("homeAddress.state is 'MA' and workAddress is not 'MA'"));
            printResults("MA Residents, Work Elsewhere", setResults);
 
        // Find all contacts whose city name begins with 'S'
        // setResults = cache.entrySet(new LikeFilter("getHomeAddress.getCity",
        //         "S%"));
           setResults = cache.entrySet(createFilter("homeAddress.city like 'S%'")); 
            printResults("City Begins with S", setResults);
 
        final int nAge = 42;
        Object[] aEnv  =  new Object[] {new Integer(nAge)}; 
        // Find all contacts who are older than nAge
        // setResults = cache.entrySet(new GreaterFilter("getAge", nAge));
           setResults = cache.entrySet(createFilter("age > ?1",aEnv)); 
             printResults("Age > " + nAge, setResults);
 
        // Find all contacts with last name beginning with 'S' that live
        // in Massachusetts. Uses both key and value in the query.
        // setResults = cache.entrySet(new AndFilter(
        //     new LikeFilter(new KeyExtractor("getLastName"), "S%",
        //   (char) 0, false),
        //         new EqualsFilter("getHomeAddress.getState", "MA")));
           setResults = cache.entrySet(createFilter("lastName like 'S%' and homeAddress.state = 'MA'"));
            printResults("Last Name Begins with S and State Is MA", setResults);
        
           // Count contacts who are older than nAge
           // System.out.println("count > " + nAge + ": "+ 
           //  cache.aggregate(new GreaterFilter("getAge", nAge), new Count()));
               System.out.println("count > " + nAge + ": " + cache.aggregate(
                    createFilter("age > ?1", aEnv), new Count()));
              
           // Find minimum age
           // System.out.println("min age: " + cache.aggregate(AlwaysFilter.INSTANCE, new LongMin("getAge")));
             Filter always = createFilter("true");
             System.out.println("min age: " + cache.aggregate(always, new LongMin("getAge")));
              
            // Calculate average age
            // System.out.println("avg age: " + cache.aggregate(AlwaysFilter.INSTANCE, new DoubleAverage("getAge")));
               System.out.println("avg age: " + cache.aggregate(always, new DoubleAverage("getAge")));
              
             // Find maximum age
             // System.out.println("max age: " +
             // cache.aggregate(AlwaysFilter.INSTANCE, new LongMax("getAge")));
               System.out.println("max age: " + cache.aggregate(always, new LongMax("getAge")));

 System.out.println("------QueryLanguageExample completed------");
                    
 
 
        }
 
    /**
    * Print results of the query
    *
    * @param sTitle      the title that describes the results
    * @param setResults  a set of query results
    */
    private static void printResults(String sTitle, Set setResults)
        {
        System.out.println(sTitle);
        for (Iterator iter = setResults.iterator(); iter.hasNext(); )
            {
            System.out.println(iter.next());
            }
        }
    }

6.3 問合せ例の再実行

次の手順に従い、問合せ例を再実行します。

  1. 稼働しているキャッシュ・サーバーをすべて停止します。

  2. contacts-cache-server.cmdで、Contactsキャッシュ・サーバーを再起動します。

  3. DataGeneratorファイル、LoaderExampleファイルおよびQueryExampleファイルを実行します。

  4. キャッシュ内のすべての連絡先情報が出力されると、問合せ結果が表示されます。この結果は次の図のようになります。

図6-1は、MA Residentsフィルタの出力を示します。

図6-1 MA Residentsフィルタの出力

MA Residentsフィルタの出力

図6-2は、MA Residents, Work Elsewhereフィルタの出力を示します。

図6-2 MA Residents, Work Elsewhereフィルタの出力

MA Residents, Work Elsewhereフィルタの出力

図6-3は、City Begins with Sフィルタの出力を示します。

図6-3 City Begins with Sフィルタの出力

City Begins with Sフィルタの出力

図6-4は、Last Name Begins with S and State is MAフィルタの出力(空のセット)およびアグリゲータの出力を示します。

図6-4 StateアグリゲータおよびAgeアグリゲータの出力

フィルタおよびアグリゲータの出力