Ce contenu n'est pas disponible dans la langue sélectionnée.
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 Copier lienLien copié sur presse-papiers!
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
SerializationContextInitializer
interface in a new class and then add the@AutoProtoSchemaBuilder
annotation.-
Reference the class that includes the
@ProtoField
annotations with theincludeClasses
parameter. -
Define a name for the Protobuf schema that you generate and filesystem path with the
schemaFileName
andschemaFilePath
parameters. Specify the package name for the Protobuf schema with the
schemaPackageName
parameter.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.proto
schema and anRemoteQueryInitializerImpl.java
implementation of the annotatedBook
class.
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
RemoteQueryInitializerImpl
serialization context with a Hot Rod Java client. -
Registers the Protobuf schema,
book.proto
, with Data Grid Server. -
Adds two
Book
instances 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 Copier lienLien copié sur presse-papiers!
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
dependsOn
attribute toCommonTypes.class
to indicate that the generated Protobuf schema can reference and useCommonTypes
types such asBigInteger
andBigDecimal
:@AutoProtoSchemaBuilder(includeClasses = CalculusIndexed.class, dependsOn = CommonTypes.class, schemaFilePath = "/protostream", schemaFileName = "calculus-indexed.proto", schemaPackageName = "lab.indexed") public interface CalculusIndexedSchema extends GeneratedSchema { }
@AutoProtoSchemaBuilder(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 Copier lienLien copié sur presse-papiers!
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
schema
command with the--upload=
argument as follows:schema --upload=person.proto person.proto
schema --upload=person.proto person.proto
Copy 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
Person
entity from the previous step:Copy to Clipboard Copied! Toggle word wrap Toggle overflow From the CLI, use the
create cache
command with the--file=
argument as follows:create cache --file=people.xml people
create cache --file=people.xml people
Copy 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
put
command 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 personone
Copy 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
query
command 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
Person
by age in ascending order.
3.4. Using analyzers with remote caches Copier lienLien copié sur presse-papiers!
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
@Text
annotation to indicate that its value is analyzed. -
Use the
analyzer
attribute to specify the desired analyzer that you want to use for indexing and searching.
Protobuf schema
Java classes
3.4.1. Default analyzer definitions Copier lienLien copié sur presse-papiers!
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 Copier lienLien copié sur presse-papiers!
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
ProgrammaticSearchMappingProvider
API. 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.ProgrammaticSearchMappingProvider
Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Copy your JAR file to the
server/lib
directory of your Data Grid Server installation. - Start Data Grid Server.
ProgrammaticSearchMappingProvider
example