キャッシュ内の SQL クエリ データ
NCache は、独自の SQL に似たクエリ メカニズムを使用して、インデックス付きキャッシュ データをクエリする機能を提供します。 指定された基準を満たすキーを検索し、結果セットのキーを返すことができます。 使用できます ExecuteReader()
または「エグゼ」
Note
クエリで使用するプロジェクションに関係なく、結果セットは次の場合にのみキーを返します。 getdata
false に設定されています。
SQL クエリの使用の前提条件
Note
NCache エスケープ シーケンスもサポートしています。 \n
,\b
, \t
、およびワイルドカード *
および ?
みんなのキャラクターとして NCache-サポートされる SQL 演算子。 これらのエスケープ シーケンスの詳細については、以下を参照してください。 NCache SQL のような 構文ガイド.
ExecuteReader を使用してキャッシュ内のデータをクエリする
ExecuteReader
ユーザー指定の基準に基づいてキャッシュに対してクエリを実行します。 クエリ基準を満たすデータ リーダー内のキーと値のペアのリストを返します。 このキーと値のペアには、キャッシュ キーとそのそれぞれの値があります。
キーの SQL クエリ
これは、に基づく基準を満たすキーを返すクエリサンプルです。 商品ID.
重要
のインスタンスを確認してください ICacheReader
リソースをクリーンアップする必要があるため、実行後は常に閉じられます。
// Pre-condition: Cache is already connected
// Items are already present in the cache
// Create a query which will be executed on the dataset
// Use the Fully Qualified Name (FQN) of your own custom class
string query = "SELECT * FROM FQN.Product WHERE ProductID > ?";
// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
// Providing parameters for query
queryCommand.Parameters.Add("ProductID",50000);
// Executing QueryCommand through ICacheReader
ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand, false, 0);
// Check if the result set is not empty
if (reader.FieldCount > 0)
{
while (reader.Read())
{
string result = reader.GetValue<string>(0);
// Perform operations using the retrieved keys
}
}
else
{
// Null query result set retrieved
}
// Pre-condition: Cache is already connected
// Items are already present in the cache
// Create a query that will be executed on the dataset
// Use the Fully Qualified Name (FQN) of your own custom class
String query = "SELECT * FROM FQN.Product WHERE ProductID > ?";
// Use QueryCommand for query execution
QueryCommand queryCommand = new QueryCommand(query);
// Providing parameters for query
HashMap<String, Object> parameters = new HashMap<>();
parameters.put("ProductID", 50000);
queryCommand.getParameters().putAll(parameters);
// Executing QueryCommand through CacheReader
CacheReader cacheReader = cache.getSearchService().executeReader(queryCommand);
// Check if the result set is not empty
if (cacheReader.getFieldCount() > 0) {
while (cacheReader.read()) {
cacheReader.getString(0);
// Perform operations using the retrieved keys
}
}
// This is an async method
// Pre-condition: Cache is already connected
// Items are already present in the cache
// Create a query which will be executed on the dataset
// Use the Fully Qualified Name (FQN) of your own custom class
var query = "SELECT * FROM FQN.Product WHERE ProductID > ?";
// Use QueryCommand for query execution
var queryCommand = new ncache.QueryCommand(query);
//Providing parameters for query
let parameter = new Map();
parameter.set("ProductID", 50000);
queryCommand.setParameters(parameter);
//Executing query command through CacheReader
var searchService = await this.cache.getSearchService();
var reader = await searchService.executeReader(queryCommand, false, 0);
//Check if result set is not empty
if (reader.getFieldCount() > 0)
{
while (reader.read())
{
var result = reader.getValue(0, Number());
// Perform operations using the retrieved keys
}
}
else
{
// Null query results retrieved
}
# Pre-condition: Cache is already connected
# Items are already present in the cache
# Create a query which will be executed on the dataset
query = "SELECT * FROM FQN.Product WHERE product_id > ?"
# Use QueryCommand for query execution
query_command = ncache.QueryCommand(query)
# Providing parameters for query
parameter = {"product_id": 50000}
query_command.set_parameters(parameter)
# Executing query command through CacheReader
search_service = cache.get_search_service()
reader = search_service.execute_reader(query_command)
# Check if result set is not empty
if reader.get_field_count() > 0:
while reader.read():
result = reader.get_value(str, 1)
# Perform operations using the retrieved keys
print(result)
else:
# None query results retrieved
print("Query result is None")
Note
操作がフェイルセーフであることを保証するために、で説明されているように、アプリケーション内の潜在的な例外を処理することをお勧めします。 失敗の処理.
SQL IN 演算子の使用
この SQL に似たクエリ形式を使用してキャッシュをクエリするには、 IN 演算子の後に複数の属性値を指定できます。 IN キーワード。 を取得する例を次に示します。 商品名 および 在庫単位 10、20、または 30 のキャッシュから 在庫単位 ExecuteReader
.
string query = "SELECT ProductName, UnitsInStock From FQN.Product WHERE UnitsInStock IN (?, ?, ?)";
// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
ArrayList unitsArray = new ArrayList();
unitsArray.Add(10);
unitsArray.Add(20);
unitsArray.Add(30);
queryCommand.Parameters.Add("UnitsInStock", unitsArray);
// Executing QueryCommand through ICacheReader
ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand);
// Read results if result set is not empty
if (reader.FieldCount > 0)
{
while (reader.Read())
{
string productName = reader.GetValue<string>("ProductName");
int unitsInStock = reader.GetValue<int>("UnitsInStock");
Console.WriteLine($"Product '{productName}' has {unitsInStock} Units In Stock.");
}
}
// Pre-condition: Cache is already connected
// Items are already present in the cache
// Create a query that will be executed on the dataset
// Use the Fully Qualified Name (FQN) of your own custom class
String query = "SELECT ProductName, UnitsInStock FROM FQN.Product WHERE UnitsInStock IN (?, ?, ?)";
// Use QueryCommand for query execution
QueryCommand queryCommand = new QueryCommand(query);
// Create a list for UnitsInStock values
ArrayList<Integer> unitsArray = new ArrayList<>();
unitsArray.add(10);
unitsArray.add(20);
unitsArray.add(30);
queryCommand.getParameters().put("UnitsInStock", unitsArray);
// Executing QueryCommand through CacheReader
CacheReader cacheReader = cache.getSearchService().executeReader(queryCommand);
// Read results if result set is not empty
if (cacheReader.getFieldCount() > 0) {
while (cacheReader.read()) {
// Get the product name
String productName = null;
productName = cacheReader.getValue("ProductName", String.class);
// Get the units in stock
Integer unitsInStock = null;
unitsInStock = cacheReader.getValue("UnitsInStock", Integer.class);
System.out.println("Product '" + productName + "' has " + unitsInStock + " Units In Stock.");
}
}
var query = "SELECT productName, UnitsInStock From FQN.Product WHERE UnitsInStock IN (?,?,?)";
// Use QueryCommand for query execution
var queryCommand = new ncache.QueryCommand(query);
//Adding Parameters
let units = [10, 15, 20, 25];
let parameter = new Map();
parameter.set("UnitsInStock", units);
queryCommand.setParameters(parameter);
// Executing QueryCommand through CacheReader
var searchService = await this.cache.getSearchService();
var reader = await searchService.executeReader(queryCommand);
// Check if result set is not empty
if (reader.getFieldCount() > 0)
{
while (reader.read())
{
// Get the value of the result set
var ProductName = reader.getValue(1, Number());
var UnitsAvailable = reader.getValue(2, Number());
}
}
else
{
// Null query result set retrieved
}
query = "SELECT product_name, units_in_stock From FQN.Product WHERE units_in_stock IN (10,20,30)"
# Use QueryCommand for query execution
query_command = ncache.QueryCommand(query)
# Executing query command through CacheReader
search_service = cache.get_search_service()
reader = search_service.execute_reader(query_command)
# Check if result set is not empty
if reader.get_field_count() > 0:
while reader.read():
product_name = reader.get_value(str, 1)
units_available = reader.get_value(int, 2)
# Perform operations
print("Name: " + str(product_name))
print("Units: " + str(units_available))
else:
# None query results retrieved
print("Query result is None")
SQL LIKE 演算子の使用
NCache を使用すると、SQL のようなクエリ形式で列内の特定のパターンをクエリできます。 LIKE オペレーター。 で使用される XNUMX つのワイルドカード LIKE 演算子は次のとおりです。
*
:文字列内のXNUMX個以上の文字の代わりに使用されます。
?
:文字列内のXNUMX文字の代わりに使用されます。
以下はキャッシュを検索して取得する例です。 商品名 および 単価 ExecuteReader
.
string query = "SELECT ProductName, UnitPrice From FQN.Product WHERE ProductName LIKE ?";
// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
queryCommand.Parameters.Add("ProductName", "choco*");
// Executing QueryCommand through ICacheReader
ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand);
// Read results if result set is not empty
if (reader.FieldCount > 0)
{
while (reader.Read())
{
string productName = reader.GetValue<string>("ProductName");
double unitPrice = reader.GetValue<double>("UnitPrice");
Console.WriteLine($"Product: {productName}, Unit Price: {unitPrice}");
}
}
else
{
Console.WriteLine($"No product found having a name that starts with 'choco'.");
}
// Pre-condition: Cache is already connected
// Items are already present in the cache
// Create a query that will be executed on the dataset
// Use the Fully Qualified Name (FQN) of your own custom class
String query = "SELECT ProductName, UnitPrice FROM FQN.Product WHERE ProductName LIKE ?";
// Use QueryCommand for query execution
QueryCommand queryCommand = new QueryCommand(query);
// Setting the parameter for the LIKE operator
HashMap<String, Object> parameters = new HashMap<>();
parameters.put("ProductName", "choco*");
queryCommand.getParameters().putAll(parameters);
// Executing QueryCommand through CacheReader
CacheReader cacheReader = cache.getSearchService().executeReader(queryCommand);
// Read results if result set is not empty
if (cacheReader.getFieldCount() > 0) {
while (cacheReader.read()) {
String productName = cacheReader.getValue("ProductName", String.class);
Double unitPrice = cacheReader.getValue("UnitPrice", Double.class);
System.out.println("Product: " + productName + ", Unit Price: " + unitPrice);
}
} else {
System.out.println("No product found having a name that starts with 'choco'.");
}
var query = "SELECT * FROM FQN.Product WHERE ProductName LIKE ? AND Category LIKE ?";
// Use QueryCommand for query execution
var queryCommand = new ncache.QueryCommand(query);
// Providing parameters for query
let parameter1 = new Map();
parameter1.set("ProductName", "P*");
queryCommand.setParameters(parameter1);
let parameter2 = new Map();
parameter2.set("Category", " Beverage?");
queryCommand.setParameters(parameter2);
// Executing QueryCommand through CacheReader
var searchService = await this.cache.getSearchService();
var reader = await searchService.executeReader(queryCommand);
// Check if result set is not empty
if (reader.getFieldCount() > 0)
{
while (reader.read())
{
var result = reader.getValue(1, Number());
// Perform operations
}
}
else
{
// Null query result set retrieved
}
query = "SELECT * FROM FQN.Product WHERE product_name LIKE ? AND category LIKE ?"
# Use QueryCommand for query execution
query_command = ncache.QueryCommand(query)
# Providing parameters for query
parameter1 = {"product_name": "Shamp*"}
parameter2 = {"category": "Househo*"}
query_command.set_parameters(parameter1)
query_command.set_parameters(parameter2)
# Executing query command through CacheReader
search_service = cache.get_search_service()
reader = search_service.execute_reader(query_command)
# Check if result set is not empty
if reader.get_field_count() > 0:
while reader.read():
result = reader.get_value(str, columnname="product_name")
# Perform operations
print(result)
else:
# None query results retrieved
print("Query result is None")
SQL GROUP BY 演算子の使用
NCache を使用して、この SQL に似たクエリ形式を通じて、指定された基準に従ってデータをグループ化する機能を提供します。 グループ化 句。
重要
なお、 グループ化 句は、集計関数なしでは使用できません。
次の例では、 単価 に従ってグループ化します カテゴリー および ワンランク上の.
string query = "SELECT Category, COUNT(*) FROM FQN.Product WHERE UnitPrice < ? Group By Category";
// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
queryCommand.Parameters.Add("UnitPrice", 100.0);
// Executing QueryCommand through ICacheReader
ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand);
// Check if result set is not empty
if (reader.FieldCount > 0)
{
while (reader.Read())
{
// Get the value of the result set
string category = reader.GetValue<string>("Category");
int count = reader.GetValue<int>("COUNT()");
Console.WriteLine($"Category '{category}' has '{count}' affordable products.");
}
}
else
{
Console.WriteLine($"No category contains affordable products.");
}
// Pre-condition: Cache is already connected
// Items are already present in the cache
// Create a query that will be executed on the dataset
// Use the Fully Qualified Name (FQN) of your own custom class
String query = "SELECT Category, COUNT(*) FROM FQN.Product WHERE UnitPrice < ? GROUP BY Category";
// Use QueryCommand for query execution
QueryCommand queryCommand = new QueryCommand(query);
// Setting the parameter
HashMap<String, Object> parameters = new HashMap<>();
parameters.put("UnitPrice", 100.0);
queryCommand.getParameters().putAll(parameters);
// Executing QueryCommand through CacheReader
CacheReader cacheReader = cache.getSearchService().executeReader(queryCommand);
// Read results if result set is not empty
if (cacheReader.getFieldCount() > 0) {
while (cacheReader.read()) {
String productName = cacheReader.getValue("Category", String.class);
Double unitPrice = cacheReader.getValue("COUNT(*)", Double.class);
System.out.println("Category '" + category + "' has '" + count + "' affordable products.");
}
} else {
System.out.println("No category contains affordable products.");
}
var query = "SELECT Category, COUNT(*) FROM FQN.Product WHERE UnitPrice > ? Group By Category";
// Use QueryCommand for query execution
var queryCommand = new ncache.QueryCommand(query);
//Adding Parameters
let parameter = new Map();
parameter.set("UnitPrice", 10);
queryCommand.setParameters(parameter);
// Executing QueryCommand through CacheReader
var searchService = await this.cache.getSearchService();
var reader = await searchService.executeReader(queryCommand);
// Check if result set is not empty
if (reader.getFieldCount() > 0)
{
while (reader.read())
{
// Get the value of the result set
var result = reader.getValue(1, ncache.JsonDataType.Object);
var category = reader.getValue(0, Number());
}
}
else
{
// Null query result set retrieved
}
query = "SELECT category, COUNT(*) FROM FQN.Product WHERE unit_price > 5 Group By category"
# Use QueryCommand for query execution
query_command = ncache.QueryCommand(query)
# Executing query command through CacheReader
search_service = cache.get_search_service()
reader = search_service.execute_reader(query_command)
# Check if result set is not empty
if reader.get_field_count() > 0:
while reader.read():
result = reader.get_value(int, 1)
category = reader.get_value(str, 0)
# Perform operations
print("Count: " + str(result))
print("Category: " + str(category))
else:
# None query results retrieved
print("Query result is None")
SQL ORDER BY 演算子の使用
NCache を使用して、SQL に似たクエリ形式を使用して、指定された基準に従って昇順または降順でデータを並べ替えることができます。 ORDER BY 句。 次の例では、 商品名 および 単価ここで、 商品名 100 より大きい場合は、次を使用して降順に並べ替えます。 ExecuteReader
.
string query = "SELECT ProductName,UnitPrice FROM FQN.Product WHERE UnitPrice < ? ORDER BY UnitPrice DESC";
// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
//changes
queryCommand.Parameters.Add("UnitPrice", 100.0);
// Executing QueryCommand through ICacheReader
ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand);
// Check if result set is not empty
if (reader.FieldCount > 0)
{
while (reader.Read())
{
// Get the value of the result set
string productName = reader.GetValue<string>("ProductName");
string unitPrice = reader.GetValue<string>("UnitPrice");
Console.WriteLine($"Product: {productName}, Unit Price: {unitPrice}");
}
}
else
{
Console.WriteLine($"No products found which are cheaper than 100.");
}
// Pre-condition: Cache is already connected
// Items are already present in the cache
// Create a query that will be executed on the dataset
// Use the Fully Qualified Name (FQN) of your own custom class
String query = "SELECT ProductName, UnitPrice FROM FQN.Product WHERE UnitPrice < ? ORDER BY UnitPrice DESC";
// Use QueryCommand for query execution
QueryCommand queryCommand = new QueryCommand(query);
// Setting the parameter
HashMap<String, Object> parameters = new HashMap<>();
parameters.put("UnitPrice", 100.0);
queryCommand.getParameters().putAll(parameters);
// Executing QueryCommand through CacheReader
CacheReader cacheReader = cache.getSearchService().executeReader(queryCommand);
// Read results if result set is not empty
if (cacheReader.getFieldCount() > 0) {
while (cacheReader.read()) {
String productName = cacheReader.getValue("ProductName", String.class);
Double unitPrice = cacheReader.getValue("UnitPrice", Double.class);
System.out.println("Product: " + productName + ", Unit Price: " + unitPrice);
}
} else {
System.out.println("No products found which are cheaper than 100.");
}
var query = "SELECT * FROM FQN.Product WHERE UnitPrice > ? ORDER BY UnitsInStock DESC";
// Use QueryCommand for query execution
var queryCommand = new ncache.QueryCommand(query);
//Providing parameters for query
let parameter = new Map();
parameter.set("UnitPrice", 100);
queryCommand.setParameters(parameter);
// Executing QueryCommand through CacheReader
var searchService = await this.cache.getSearchService();
var reader = await searchService.executeReader(queryCommand);
// Check if result set is not empty
if (reader.getFieldCount() > 0)
{
while (reader.read())
{
// Get the value of the result set
var result = reader.getValue(1, Number());
}
}
else
{
// Null query result set retrieved
}
query = "SELECT * FROM FQN.Product WHERE unit_price > 100 ORDER BY units_in_stock DESC"
# Use QueryCommand for query execution
query_command = ncache.QueryCommand(query)
# Executing query command through CacheReader
search_service = cache.get_search_service()
reader = search_service.execute_reader(query_command)
# Check if result set is not empty
if reader.get_field_count() > 0:
while reader.read():
result = reader.get_value(int, columnname="units_in_stock")
# Perform operations
print("Units: " + str(result))
else:
# None query results retrieved
print("Query result is None")
SQL 日時演算子の使用
NCache さまざまな形式でパラメータを提供することにより、キャッシュの検索を容易にします。 単一のクエリで多くの関数を使用すると、キャッシュ検索を高速化できます。 DateTime
クエリで使用すると、特定の日付または時刻に関するキャッシュを検索できます。 日付形式の詳細については、を参照してください。 Microsoft DateTime 構造体のドキュメント.
Note
日付を正しい形式で指定していることを確認してください。
次の例では、キャッシュ内のすべての注文を検索するクエリを実行します。 注文日 を使用して指定されたとおりです ExecuteReader
.
string query = "SELECT OrderID FROM FQN.Order WHERE OrderDate = ?";
// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
// Define order date
DateTime orderDate = new DateTime(2022, 01, 01);
// Providing parameters to query
queryCommand.Parameters.Add("OrderDate", orderDate);
// Executing QueryCommand through ICacheReader
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
int orderID = reader.GetValue<int>("OrderID");
Console.WriteLine($"Order: {orderID} was placed on 1st January, 2022.");
}
}
else
{
Console.WriteLine($"No orders were placed on 1st January 2022.");
}
// Pre-condition: Cache is already connected
// Items are already present in the cache
// Create a query that will be executed on the dataset
// Use the Fully Qualified Name (FQN) of your own custom class
String query = "SELECT OrderID FROM FQN.Order WHERE OrderDate = ?";
// Use QueryCommand for query execution
QueryCommand queryCommand = new QueryCommand(query);
// Define order date in Java
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date orderDate;
orderDate = sdf.parse("2022-01-01");
// Providing parameters to query
HashMap<String, Object> parameters = new HashMap<>();
parameters.put("OrderDate", orderDate);
queryCommand.getParameters().putAll(parameters);
// Executing QueryCommand through CacheReader
CacheReader cacheReader = cache.getSearchService().executeReader(queryCommand);
// Read results if result set is not empty
if (cacheReader.getFieldCount() > 0) {
while (cacheReader.read()) {
int orderID = cacheReader.getInt(cacheReader.getOrdinal("OrderID"));
System.out.println("Order: " + orderID + " was placed on 1st January, 2022.");
}
} else {
System.out.println("No orders were placed on 1st January 2022.");
}
// Use the Fully Qualified Name (FQN) of your own custom class
var query = "SELECT * FROM FQN.Order WHERE OrderDate < ?";
// Use QueryCommand for query execution
var queryCommand = new ncache.QueryCommand(query);
// Providing parameters to query
var date = new Date("2021-02-10");
let map = new Map();
var parameters = map.set("orderDate", date);
queryCommand.setParameters(parameters);
// Executing QueryCommand through CacheReader
var searchService = await this.cache.getSearchService();
var reader = await searchService.executeReader(queryCommand);
// Check if the result set is not empty
if (reader.getFieldCount() > 0)
{
while (reader.read())
{
// Get the value of the result set
var result = reader.getValue(1, Number());
// Perform operations
}
}
else
{
// Null query result set retrieved
}
query = "SELECT * FROM FQN.Order WHERE order_date < ?"
# Use QueryCommand for query execution
query_command = ncache.QueryCommand(query)
# Providing parameters for query
parameter = {"order_date": datetime.strptime('Jan 1 2022 1:33PM', '%b %d %Y %I:%M%p')}
query_command.set_parameters(parameter)
# Executing query command through CacheReader
search_service = cache.get_search_service()
reader = search_service.execute_reader(query_command)
# Check if result set is not empty
if reader.get_field_count() > 0:
while reader.read():
order_id = reader.get_value(int, 1)
# Perform operations
print("Order ID: " + str(order_id))
else:
# None query results retrieved
print("Query result is None")
SQL 論理演算子の使用
NCache ユーザーに提供するのは、 そして, OR, NOT 論理演算子を使用すると、この SQL に似たクエリ形式を使用して、クエリ内の演算子の後に複数の条件を指定できます。 以下の例は、次の組み合わせを使用する方法を示しています。 そして および OR を使用した WHERE 句内の単一クエリの演算子 ExecuteReader
.
string query = "SELECT * FROM FQN.Product WHERE (Category = ? AND UnitsInStock > ?) OR (Category = ? AND UnitPrice > ?)";
// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
//Adding Parameters
ArrayList categoryList = new ArrayList();
categoryList.Add("Beverages");
categoryList.Add("Produce");
queryCommand.Parameters.Add("Category", categoryList);
queryCommand.Parameters.Add("UnitsInStock", 100);
queryCommand.Parameters.Add("UnitPrice", 100);
// Executing QueryCommand through ICacheReader
ICacheReader reader = _cache.SearchService.ExecuteReader(queryCommand);
// Check if the result set is not empty
if (reader.FieldCount > 0)
{
while (reader.Read())
{
// Get the value of the result set
string result = reader.GetValue<string>("ProductID");
}
}
else
{
// Null query result set retrieved
}
catch (OperationFailedException ex)
if (ex.ErrorCode == NCacheErrorCodes.INCORRECT_FORMAT)
{
// Make sure that the query format is correct
}
else
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
// Pre-condition: Cache is already connected
// Items are already present in the cache
// Create a query that will be executed on the dataset
// Use the Fully Qualified Name (FQN) of your own custom class
String query = "SELECT * FROM FQN.Product WHERE (Category = ? AND UnitsInStock > ?) OR (Category = ? AND UnitPrice > ?)";
// Use QueryCommand for query execution
QueryCommand queryCommand = new QueryCommand(query);
// Adding Parameters
ArrayList<String> categoryList = new ArrayList<>();
categoryList.add("Beverages");
categoryList.add("Produce");
HashMap<String, Object> parameters = new HashMap<>();
parameters.put("Category1", categoryList.get(0));
parameters.put("Category2", categoryList.get(1));
parameters.put("UnitsInStock", 100);
parameters.put("UnitPrice", 100.0);
queryCommand.getParameters().putAll(parameters);
// Executing QueryCommand through CacheReader
CacheReader cacheReader = cache.getSearchService().executeReader(queryCommand);
// Check if the result set is not empty
if (cacheReader.getFieldCount() > 0) {
while (cacheReader.read()) {
// Get the value of the result set
String result = cacheReader.getString(cacheReader.getOrdinal("ProductID"));
}
} else {
// Null query result set retrieved
}
var query = "SELECT * FROM FQN.Product WHERE (Category = ? AND UnitsInStock > ?) OR (Category = ? AND UnitPrice > ?)";
// Use QueryCommand for query execution
var queryCommand = new ncache.QueryCommand(query);
//Add parameters
let categoryList = Array();
categoryList.push("Produce");
categoryList.push("Beverages");
let parameter = new Map();
parameter.set("Category", categories);
parameter.set("UnitsInStock", 100);
parameter.set("UnitPrice", 100);
queryCommand.setParameters(parameter);
// Executing QueryCommand through CacheReader
var searchService = await this.cache.getSearchService();
var reader = await searchService.executeReader(queryCommand);
// Check if the result set is not empty
if (reader.getFieldCount() > 0)
{
while (reader.read())
{
// Get the value of the result set
var result = reader.getValue(1, Number());
}
}
else
{
// Null query result set retrieved
}
query = "SELECT * FROM FQN.Product WHERE (category = 'Beverages' AND units_in_stock > 0) OR (category = 'Produce' AND unit_price > 100)"
# Use QueryCommand for query execution
query_command = ncache.QueryCommand(query)
# Executing query command through CacheReader
search_service = cache.get_search_service()
reader = search_service.execute_reader(query_command)
# Check if result set is not empty
if reader.get_field_count() > 0:
while reader.read():
product_id = reader.get_value(str, 1)
# Perform operations
print("Product ID: " + str(product_id))
else:
# None query results retrieved
print("Query result is None")
ExecuteScalar を使用してキャッシュ内のデータを検索する
ExecuteScalar
ユーザークエリを実行し、結果セットの最初の行の最初の列を返します。追加の列または行は無視されます。
string query = "SELECT AVG(UnitPrice) FROM FQN.Product WHERE Category = ?";
// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
queryCommand.Parameters.Add("Category", "Beverages");
// Executing QueryCommand through ICacheReader
decimal average = (decimal)cache.SearchService.ExecuteScalar(queryCommand);
Console.WriteLine($"Average price of Beverages is {average}.");
// Create a query that will be executed on the dataset
String query = "SELECT AVG(UnitPrice) FROM FQN.Product WHERE Category = ?";
// Use QueryCommand for query execution
QueryCommand queryCommand = new QueryCommand(query);
queryCommand.getParameters().put("Category", "Beverages");
// Executing QueryCommand through ExecuteScalar
Double average = cache.getSearchService().executeScalar(queryCommand, Double.class);
if (average != null) {
System.out.println("Average price of Beverages is " + average + ".");
} else {
System.out.println("No data found for Beverages.");
}
その他のリソース
NCache SQL クエリのサンプル アプリケーションを提供します。 GitHubの.
も参照してください
。ネット: Alachisoft.NCache.クライアント.サービス 名前空間
Java: comの。alachisoft.ncache.ランタイムキャッシュ 名前空間
Node.js: キャッシュ とに提供されます。
Python: ncache.client.services とに提供されます。