이 콘텐츠는 선택한 언어로 제공되지 않습니다.
Chapter 3. Querying remote caches
You can index and query remote caches on Data Grid Server.
3.1. Querying caches from Hot Rod Java clients 링크 복사링크가 클립보드에 복사되었습니다!
Data Grid lets you programmatically query remote caches from Java clients through the Hot Rod endpoint. This procedure explains how to index query a remote cache that stores Book instances.
Prerequisites
-
Add the ProtoStream processor to your
pom.xml.
Data Grid provides this processor for the @ProtoField annotations so you can generate Protobuf schemas and perform queries.
Procedure
Add indexing annotations to your class, as in the following example:
Book.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Implement the
SerializationContextInitializerinterface in a new class and then add the@ProtoSchemaannotation.-
Reference the class that includes the
@ProtoFieldannotations with theincludeClassesparameter. -
Define a name for the Protobuf schema that you generate and filesystem path with the
schemaFileNameandschemaFilePathparameters. Specify the package name for the Protobuf schema with the
schemaPackageNameparameter.RemoteQueryInitializer.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
-
Reference the class that includes the
Compile your project.
The code examples in this procedure generate a
proto/book.protoschema and anRemoteQueryInitializerImpl.javaimplementation of the annotatedBookclass.
Next steps
Create a remote cache that configures Data Grid to index your entities. For example, the following remote cache indexes the Book entity in the book.proto schema that you generated in the previous step:
The following RemoteQuery class does the following:
-
Registers the
RemoteQueryInitializerImplserialization context with a Hot Rod Java client. -
Registers the Protobuf schema,
book.proto, with Data Grid Server. -
Adds two
Bookinstances to the remote cache. - Performs a full-text query that matches books by keywords in the title.
RemoteQuery.java
3.2. Querying ProtoStream common types 링크 복사링크가 클립보드에 복사되었습니다!
Perform Ickle queries on caches that store data as ProtoStream common types such as BigInteger and BigDecimal.
Procedure
Add indexing annotations to your class, as in the following example:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Set the
dependsOnattribute toCommonTypes.classto indicate that the generated Protobuf schema can reference and useCommonTypestypes such asBigIntegerandBigDecimal:@ProtoSchema(includeClasses = CalculusIndexed.class, dependsOn = CommonTypes.class, schemaFilePath = "/protostream", schemaFileName = "calculus-indexed.proto", schemaPackageName = "lab.indexed") public interface CalculusIndexedSchema extends GeneratedSchema { }@ProtoSchema(includeClasses = CalculusIndexed.class, dependsOn = CommonTypes.class, schemaFilePath = "/protostream", schemaFileName = "calculus-indexed.proto", schemaPackageName = "lab.indexed") public interface CalculusIndexedSchema extends GeneratedSchema { }Copy to Clipboard Copied! Toggle word wrap Toggle overflow Perform queries:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
3.3. Querying caches from Data Grid Console and CLI 링크 복사링크가 클립보드에 복사되었습니다!
Data Grid Console and the Data Grid Command Line Interface (CLI) let you query indexed and non-indexed remote caches. You can also use any HTTP client to index and query caches via the REST API.
This procedure explains how to index and query a remote cache that stores Person instances.
Prerequisites
- Have at least one running Data Grid Server instance.
- Have Data Grid credentials with create permissions.
Procedure
Add indexing annotations to your Protobuf schema, as in the following example:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow From the Data Grid CLI, use the
schemacommand with the--upload=argument as follows:schema --upload=person.proto person.proto
schema --upload=person.proto person.protoCopy to Clipboard Copied! Toggle word wrap Toggle overflow Create a cache named people that uses ProtoStream encoding and configures Data Grid to index entities declared in your Protobuf schema.
The following cache indexes the
Personentity from the previous step:Copy to Clipboard Copied! Toggle word wrap Toggle overflow From the CLI, use the
create cachecommand with the--file=argument as follows:create cache --file=people.xml people
create cache --file=people.xml peopleCopy to Clipboard Copied! Toggle word wrap Toggle overflow Add entries to the cache.
To query a remote cache, it needs to contain some data. For this example procedure, create entries that use the following JSON values:
PersonOne
Copy to Clipboard Copied! Toggle word wrap Toggle overflow PersonTwo
Copy to Clipboard Copied! Toggle word wrap Toggle overflow PersonThree
Copy to Clipboard Copied! Toggle word wrap Toggle overflow From the CLI, use the
putcommand with the--file=argument to add each entry, as follows:put --encoding=application/json --file=personone.json personone
put --encoding=application/json --file=personone.json persononeCopy to Clipboard Copied! Toggle word wrap Toggle overflow TipFrom Data Grid Console, you must select Custom Type for the Value content type field when you add values in JSON format with custom types .
Query your remote cache.
From the CLI, use the
querycommand from the context of the remote cache.query "from org.infinispan.example.Person p WHERE p.name='Person' ORDER BY p.age ASC"
query "from org.infinispan.example.Person p WHERE p.name='Person' ORDER BY p.age ASC"Copy to Clipboard Copied! Toggle word wrap Toggle overflow The query returns all entries with a name that matches
Personby age in ascending order.
3.4. Using analyzers with remote caches 링크 복사링크가 클립보드에 복사되었습니다!
Analyzers convert input data into terms that you can index and query. You specify analyzer definitions with the @Text annotation in your Java classes or directly in Protobuf schema.
Procedure
-
Annotate the property with the
@Textannotation to indicate that its value is analyzed. -
Use the
analyzerattribute to specify the desired analyzer that you want to use for indexing and searching.
Protobuf schema
Java classes
3.4.1. Default analyzer definitions 링크 복사링크가 클립보드에 복사되었습니다!
Data Grid provides a set of default analyzer definitions.
| Definition | Description |
|---|---|
|
| Splits text fields into tokens, treating whitespace and punctuation as delimiters. |
|
| Tokenizes input streams by delimiting at non-letters and then converting all letters to lowercase characters. Whitespace and non-letters are discarded. |
|
| Splits text streams on whitespace and returns sequences of non-whitespace characters as tokens. |
|
| Treats entire text fields as single tokens. |
|
| Stems English words using the Snowball Porter filter. |
|
| Generates n-gram tokens that are 3 grams in size by default. |
|
|
Splits text fields into larger size tokens than the |
|
| Converts all the letters of the text to lowercase characters, the text is not tokenized (normalizer). |
These analyzer definitions are based on Apache Lucene. For more information about tokenizers, filters, and CharFilters, see the Apache Lucene documentation.
3.4.2. Creating custom analyzer definitions 링크 복사링크가 클립보드에 복사되었습니다!
Create custom analyzer definitions and add them to your Data Grid Server installations.
Prerequisites
Stop Data Grid Server if it is running.
Data Grid Server loads classes at startup only.
Procedure
-
Implement the
ProgrammaticSearchMappingProviderAPI. Package your implementation in a JAR with the fully qualified class (FQN) in the following file:
META-INF/services/org.infinispan.query.spi.ProgrammaticSearchMappingProvider
META-INF/services/org.infinispan.query.spi.ProgrammaticSearchMappingProviderCopy to Clipboard Copied! Toggle word wrap Toggle overflow -
Copy your JAR file to the
server/libdirectory of your Data Grid Server installation. - Start Data Grid Server.
ProgrammaticSearchMappingProvider example
3.5. Queries by keys 링크 복사링크가 클립보드에 복사되었습니다!
You can define the key of a cache entry as Indexed type to index the key fields as well the value fields allowing the keys to be used in Ickle queries.
To define an Indexed key, specify the fully qualified name of the ProtocolBuffer message type to use as the key type in the keyEntity attribute of the @Indexed annotation.
This feature is available only with indexed remote queries.
Specify the keyEntity of an indexed entity
Define the key entity and its indexed fields
3.5.1. Key property name 링크 복사링크가 클립보드에 복사되었습니다!
By default, the key fields will be targeted using the property named key.
Use key properties in the Ickle queries
select s.key.column from model.Structure s where s.key.zone = 'z7'
select s.key.column from model.Structure s where s.key.zone = 'z7'
If the value already has a property named key, the definition of the key entity could create a naming conflict with the properties. For this reason, and also in general, it is possible to change the name to assign as a prefix for the property keys changing the attribute keyPropertyName of the @Indexed annotation.
3.5.2. Key include depth 링크 복사링크가 클립보드에 복사되었습니다!
An entity key can have embedded entities. You can limit the depth for the embedded entity fields that are indexed by changing the attribute keyIncludeDepth, which defaults to 3.