前章では、AlwaysFilter
、LikeFilter
、EqualsFilter
など様々なフィルタを使用したファイルQueryExample.java
を作成し、キャッシュから情報を取得して、これを集計しました。またこのファイルでは、様々な専用のValueExtractors
(ChainedExtractors
、KeyExtractors
およびReflectionExtractors
)を採用することによってデータに索引を作成しました。その結果、非常に冗長なJava文が作成されました。これらの文は、QueryHelper
APIを使用することで簡略化できます。
この章は次の項で構成されています。
フィルタ/エクストラクタ文、およびCoherenceキャッシュとの相互作用方法を簡略化するため、最新リリースではQueryHelper
APIが提供されています。QueryHelper
(com.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問合せ言語の使用に関する項を参照してください。
この項では、前章で作成したQueryExample.java
ファイルの索引付け、キャッシュ・コールおよび集計を簡略化する方法について説明します。
静的なクラスとしてQueryHelper
APIをインポートします。
import static com.tangosol.util.QueryHelper.*;
ChainedExtractor
クラス、KeyExtractor
クラスおよびReflectionExtractor
クラスのインポートをコメント・アウトします。
AlwaysFilter
クラス、AndFilter
クラス、EqualsFilter
クラス、GreaterFilter
クラス、LikeFilter
クラスおよびNotEqualsFilter
クラスのインポートをコメント・アウトします。
cache.addIndex
文で、ReflectionExtractor
のインスタンスをQueryHelper APIのcreateExtractor
に置き換えます。
表6-1に、ReflectionExtractor
のインスタンスと同等なcreateExtractor
を一覧表示します。
表6-1 ReflectionExtractor文と同等なcreateExtractor文
ReflectionExtractor文 | 置換される同等のcreateExtractor文 |
---|---|
|
|
|
|
|
|
|
|
setResults
文の*Filter
メソッドのコールを、適切なCoherence問合せ言語でのcreateFilter
のコールに置き換えます。
表6-2に、*Filter
のインスタンスと同等なcreateFilter
を一覧表示します。
表6-2 問合せにおける*Filter文と同等なcreateFilter文
*Filter文 | 置換される同等のcreateFilter文 |
---|---|
|
|
|
|
|
|
|
|
|
|
aggregate
文の*Filter
メソッドのコールを、適切なCoherence問合せ言語でのcreateFilter
のコールに置き換えます。
表6-3に、*Filter
のインスタンスと同等なcreateFilter
を一覧表示します。
表6-3 集計における*Filter文と同等なcreateFilter文
*Filter文 | 置換される同等のcreateFilter文 |
---|---|
|
|
|
|
|
|
|
|
コードの置換を終了すると、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()); } } }
次の手順に従い、問合せ例を再実行します。
稼働しているキャッシュ・サーバーをすべて停止します。
contacts-cache-server.cmd
で、Contactsキャッシュ・サーバーを再起動します。
DataGenerator
ファイル、LoaderExample
ファイルおよびQueryExample
ファイルを実行します。
キャッシュ内のすべての連絡先情報が出力されると、問合せ結果が表示されます。この結果は次の図のようになります。
図6-1は、MA Residentsフィルタの出力を示します。
図6-2は、MA Residents, Work Elsewhereフィルタの出力を示します。
図6-3は、City Begins with Sフィルタの出力を示します。
図6-4は、Last Name Begins with S and State is MAフィルタの出力(空のセット)およびアグリゲータの出力を示します。