Este conteúdo não está disponível no idioma selecionado.
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 o linkLink copiado para a área de transferência!
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@AutoProtoSchemaBuilderannotation.-
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 Copiar o linkLink copiado para a área de transferência!
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:@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 Copiar o linkLink copiado para a área de transferência!
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 Copiar o linkLink copiado para a área de transferência!
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 Copiar o linkLink copiado para a área de transferência!
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 Copiar o linkLink copiado para a área de transferência!
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