SQLクエリを使用してキャッシュ内のグループデータを検索する
NCache は、クエリで指定された条件を使用して結果セットを検索するオブジェクト クエリを提供します。 指定した基準に従ってデータを取得するには、 NCache SQLの拡張機能を提供します。
特別なキーワード $Group$
検討中の条件がグループを使用することを指定するために使用されます。 検索条件を使用したクエリは、を使用して実行されます ExecuteReader
.
ExecuteReader
サーバー側でクエリを処理し、結果をチャンクで (キーと値を含む辞書として) クライアントに表形式で送信します。 ICacheReader
インスタンスのタイプ。
SQL クエリを使用してキャッシュ内のグループ データを検索するための前提条件
警告
検索されたグループがキャッシュにない場合、クエリは Null
の値です。
SQL クエリを使用してグループ内のデータを検索する
また、SQL を使用して、グループに存在するキャッシュ アイテムを検索することもできます。 次のプログラムは、SQL クエリを使用して、グループに存在する顧客 ID を検索します。
// A user wants to see all the West Coast Customers using the SQL query.
// Preconditions: Cache is already connected
// Items with groups exist in the cache
// Custom class is query indexed through the NCache Management Center or config.ncconf
// Create an SQL Query with the specified criteria
// Make sure to use the Fully Qualified Name (FQN)
string query = "SELECT CustomerID,ContactName FROM Alachisoft.NCache.Samples.Data.Customer WHERE $Group$ = ? ";
// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
// Providing parameters for query
queryCommand.Parameters.Add("$Group$", "West Coast Customers");
// Executing the Query
ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand);
// Read results if result set is not empty
if (reader.FieldCount > 0)
{
while (reader.Read())
{
// Get the value of the result set
string customerID = reader.GetValue<string>("CustomerID");
string customerName = reader.GetValue<string>("ContactName");
Console.WriteLine($"Customer '{customerName}' having ID '{customerID}' belongs to West Coast.");
}
}
else
{
Console.WriteLine($"No customers from West Coast found");
}
// A user wants to see all the West Coast Customers using the SQL query.
// Preconditions: Cache is already connected
// Items with groups exist in the cache
// Custom class is query indexed through the NCache Management Center or config.ncconf
// Create an SQL Query with the specified criteria
// Make sure to use the Fully Qualified Name (FQN)
String query = "SELECT customerID,contactName FROM com.alachisoft.ncache.samples.Customer WHERE $Group$ = ?";
// Use QueryCommand for query execution
QueryCommand queryCommand = new QueryCommand(query);
// Providing parameters for query
queryCommand.getParameters().put("$Group$", "West Coast Customers");
// Executing the Query
CacheReader reader = cache.getSearchService().executeReader(queryCommand);
// Read results if result set is not empty
if (reader.getFieldCount() > 0) {
while (reader.read()) {
// Get the value of the result set
String customerID = null;
customerID = reader.getValue("customerID", String.class);
String customerName = null;
customerName = reader.getValue("contactName", String.class);
System.out.println("Customer '" + customerName + "' having ID '" + customerID + "' belongs to West Coast.");
}
}
else {
System.out.println("No customers from West Coast found");
}
// This is an async method
// Preconditions: Cache is already connected
// Items with groups exist in cache
// Custom class is query indexed through the NCache Management Center or config.ncconf
// Search for items with group
// Provide Fully Qualified Name (FQN) of your custom class
var query = "SELECT $Group$ FROM FQN.Customer WHERE CustomerName = ?";
// Use QueryCommand for query execution
var queryCommand = new ncache.QueryCommand(query);
// Providing parameters for query
var parameters = this.map.set("$Group","Important Customers");
queryCommand.setParameters(parameters);
// Executing QueryCommand
var searchService = await this.cache.getSearchService();
var cacheReader = await searchService.executeReader(queryCommand);
// Check if the result set is not empty
if (cacheReader.getFieldCount() > 0)
{
while (cacheReader.read())
{
// Perform operations
var groupName = cacheReader.getValue(1);
}
}
else
{
// Null query result set retrieved
}
# Preconditions: Cache is already connected
# Items with groups exist in cache
# Custom class is query indexed through the the NCache Management Center or config.ncconf
# Search for items with group
# Provide Fully Qualified Name(FQN) of your custom class
query = "SELECT $Group$ FROM FQN.Customer WHERE customer_name = ?"
# Use QueryCommand for query execution
query_command = ncache.QueryCommand(query)
# Providing parameters for query
parameters = {"$Group": "Important Customers"}
query_command.set_parameters(parameters)
# Executing QueryCommand
search_service = cache.get_search_service()
cache_reader = search_service.execute_reader(query_command)
# Check if the result set is not empty
if cache_reader.get_field_count() > 0:
while cache_reader.read():
# Perform operations
group_name = cache_reader.get_value(str, 1)
else:
# None query result set retrieved
print("Query result is None")
Note
操作がフェイルセーフであることを保証するために、で説明されているように、アプリケーション内の潜在的な例外を処理することをお勧めします。 失敗の処理.
その他のリソース
NCache グループのサンプルアプリケーションを提供します GitHubの.
も参照してください
。ネット: Alachisoft.NCache。クライアント 名前空間
Java: comの。alachisoft.ncache。クライアント 名前空間
Node.js: キャッシュ とに提供されます。
Python: ncache。クライアント とに提供されます。