SQL을 사용하여 캐시에서 그룹 데이터 검색
NCache 쿼리에 제공된 기준을 사용하여 결과 집합을 검색하는 개체 쿼리를 제공합니다. 지정된 기준에 따라 데이터를 검색하려면, NCache SQL 확장을 제공합니다.
특별한 키워드 $Group$
고려 중인 조건이 그룹을 사용하도록 지정하는 데 사용됩니다. 검색 기준이 있는 쿼리는 다음을 사용하여 실행됩니다. ExecuteReader
.
ExecuteReader
서버 측에서 쿼리를 처리한 다음 청크(키와 값을 포함하는 사전)의 결과를 표 형식으로 클라이언트에 전송합니다. ICacheReader
인스턴스 유형.
사전 조건
경고
검색된 그룹이 캐시에 없으면 쿼리가 반환됩니다. Null
값.
그룹에서 데이터 검색
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")
주의 사항
작업이 안전하도록 하려면 에 설명된 대로 응용 프로그램 내에서 잠재적인 예외를 처리하는 것이 좋습니다. 처리 실패.
추가 자료
NCache 그룹에 대한 샘플 애플리케이션 제공 GitHub의.
도 참조
.그물: Alachisoft.NCache.고객 네임 스페이스.
자바 : COM.alachisoft.ncache.고객 네임 스페이스.
Node.js : 캐시 클래스입니다.
파이썬 : ncache.고객 클래스입니다.