この演習では、QueryHelper
クラスでCoherenceのcreateFilter
およびcreateExtractor
ファクトリ・メソッドを使用することによって、集計およびフィルタを使用するJava式を簡素化する方法について説明します。
第5章「キャッシュへのデータのロード」では、AlwaysFilter
、LikeFilter
およびEqualsFilter
などの様々なフィルタを使用したファイルQueryExample.java
を作成し、キャッシュから情報を取得して集計しました。また、このファイルでは、様々な特化されたValueExtractors
: ChainedExtractors
、KeyExtractors
およびReflectionExtractors
を使用して、データに索引を作成しました。その結果、冗長なJava文がいくつか作成されました。そのような文を簡略化するには、QueryHelper
APIを使用します。
この章には次の項が含まれます:
フィルタ文やエクストラクタ文を簡略化するほか、Coherenceキャッシュとのやり取りを簡略化するために、CoherenceではQueryHelper
APIが用意されています。QueryHelper
(com.tangosol.util.QueryHelper
)はユーティリティ・クラスであり、これによってcreateFilter
およびcreateExtractor
ファクトリ・メソッドのセットが提供され、Filter
とValueExtractor
のインスタンスをビルドできるようになります。このクラス内のメソッドは、Stringデータ・タイプを受け入れるので、SQLのWHERE
句について理解しているユーザーにとっては使い慣れた形式で豊富なフィルタの作成を指定することができます。
たとえば、次の文は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 Fusion Middleware Oracle Coherenceでのアプリケーションの開発』のCoherence問合せ言語の使用に関する項を参照してください。
この項では、前章で作成したQueryExample.java
ファイルの索引付け、キャッシュ・コールおよび集計を簡略化する方法について説明します。
QueryHelper
APIを静的クラスとしてQueryExample.java
ファイルにインポートします。
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 ReflectionExtractors文およびそれと同等な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("key(lastName) like 'S%' and homeAddress.state = 'MA'")); setResults = cache.entrySet(createFilter("key().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()); } } }
問合せサンプルを再実行するには:
実行中のキャッシュ・サーバーがあれば停止します。詳細は、「キャッシュ・サーバーの停止」を参照してください。
ContactsCacheServer
を再起動します。
DataGenerator
ファイル、LoaderExample
ファイルおよびQueryExample
ファイルを実行します。
キャッシュ内のすべての連絡先情報を出力すると、問合せの結果が表示されます。結果は次の例のようになります。
注意:
|
例6-2は、MA Residentsフィルタの出力を示しています。
例6-2 MA Residentsフィルタの出力
...
MA Residents
ConverterEntry{Key="John Hwdrrls", Value="John Hwdrrls
Addresses
Home: 369 Beacon St.
Fetggv, MA 24372
US
Work: Yoyodyne Propulsion Systems
330 Lectroid Rd.
Grover's Mill, NE 84499
US
Telephone Numbers
work: +11 88 331 2307913
home: +11 64 86 2489621
Birth Date: 1976-12-29"}
...
例6-3は、MA Residents, Work Elsewhereフィルタの出力を示しています。
例6-3 MA Residents, Work Elsewhereフィルタの出力
...
MA Residents, Work Elsewhere
ConverterEntry{Key="John Hwdrrls", Value="John Hwdrrls
Addresses
Home: 369 Beacon St.
Fetggv, MA 24372
US
Work: Yoyodyne Propulsion Systems
330 Lectroid Rd.
Grover's Mill, NE 84499
US
Telephone Numbers
work: +11 88 331 2307913
home: +11 64 86 2489621
Birth Date: 1976-12-29"}
...
例6-4は、City Begins with Sフィルタの出力を示しています。
例6-4 City Begins with Sフィルタの出力
...
City Begins with S
ConverterEntry{Key="John Pzek", Value="John Pzek
Addresses
Home: 309 Beacon St.
Saqrgy, OH 81353
US
Work: Yoyodyne Propulsion Systems
330 Lectroid Rd.
Grover's Mill, CT 78117
US
Telephone Numbers
work: +11 28 790 2035988
home: +11 61 470 7634708
Birth Date: 1971-12-31"}
...
例6-5は、42歳を過ぎた従業員のフィルタの出力を示しています。
例6-5 Age Greater than 42フィルタの出力
...
Age > 42
ConverterEntry{Key="John Gddurqqziy", Value="John Gddurqqziy
Addresses
Home: 613 Beacon St.
Cxyskdo, DE 28968
US
Work: Yoyodyne Propulsion Systems
330 Lectroid Rd.
Grover's Mill, SD 07959
US
Telephone Numbers
work: +11 31 768 5136041
home: +11 87 22 3851589
Birth Date: 1958-01-03"}
...
例6-6は、Last Name Begins with SフィルタとState is MAフィルタの出力およびアグリゲータの出力を示しています。
例6-6 StateアグリゲータおよびAgeアグリゲータの出力
Last Name Begins with S and State Is MA
ConverterEntry{Key="John Syaqlojl", Value="John SyaqlojlAddressesHome: 810 Beacon St.Rgtaljwph, MA 07471USWork: 200 Newbury St.Yoyodyne, Ltd.Boston, MA 02116USTelephone Numberswork: +11 37 18 1767648home: +11 98 155 1073866Birth Date: 1974-12-30"} ...count > 42: 446
min age: 22
avg age: 41.126
max age: 61