Este contenido no está disponible en el idioma seleccionado.
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 Copiar enlaceEnlace copiado en el portapapeles!
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
and @ProtoDoc
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
and@ProtoDoc
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 caches from Data Grid Console and CLI Copiar enlaceEnlace copiado en el portapapeles!
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.3. Using analyzers with remote caches Copiar enlaceEnlace copiado en el portapapeles!
Analyzers convert input data into terms that you can index and query. You specify analyzer definitions with the @Field
annotation in your Java classes or directly in Protobuf schema.
Procedure
-
Include the
Analyze.YES
attribute to indicate that the property is analyzed. -
Specify the analyzer definition with the
@Analyzer
annotation.
Protobuf schema
Java classes
3.3.1. Default analyzer definitions Copiar enlaceEnlace copiado en el portapapeles!
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 |
These analyzer definitions are based on Apache Lucene and are provided "as-is". For more information about tokenizers, filters, and CharFilters, see the appropriate Lucene documentation.
3.3.2. Creating custom analyzer definitions Copiar enlaceEnlace copiado en el portapapeles!
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